Skip to content

Add PyType_Lookup() function #83

@vstinner

Description

@vstinner

There is a private _PyType_Lookup() function which is used by 27 projects of the PyPI top 15,000 projects.

I propose to make the function public.

API: int PyType_Lookup(PyTypeObject *type, PyObject *name, PyObject **attr)

  • Look for a type attribute through the type MRO. Do not invoke descriptors or metaclass __getattr__().
  • name must be a str.
    • If found, set *attr to a strong reference and return 1.
    • If not found, set *attr to NULL and return 0.
    • On error, set an exception and return -1.
  • Python pseudo-code (return AttributeError if not found):
       def PyType_Lookup(type, name):
           if not isinstance(name, str):
               raise TypeError
           for klass in type.mro():
               try:
                   return klass.__dict__[name]
               except KeyError:
                   pass
           # not found
           return AttributeError

The private PyObject* _PyType_Lookup(PyTypeObject *type, PyObject *name) API returns a borrowed reference, whereas the proposed public API returns a strong reference, and it returns NULL if not found or on error, whereas the public API is not ambiguous (no need to check PyErr_Occurred()).

See:

Vote:

Metadata

Metadata

Assignees

No one assigned

    Labels

    voteThe WG is voting (or has voted)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions