Skip to content

Add PyImport_ImportModuleAttr(mod_name, attr_name) helper function #53

Description

@vstinner

Python has a convenient internal _PyImport_GetModuleAttrString(mod_name, attr_name) helper function to import a module and get a module attribute. I propose to make this function public to be able to use it outside Python. I also propose to make its variant _PyImport_GetModuleAttr(mod_name, attr_name) public (which takes objects rather than strings).

API:

PyObject* PyImport_ImportModuleAttr(PyObject *mod_name, PyObject *attr_name)

Import the module mod_name and get its attribute attr_name.

Helper function combining PyImport_Import() and PyObject_GetAttr(). For example, it can raise ImportError if the module is not found, and AttributeError if the attribute doesn't exist.

API:

PyObject* PyImport_ImportModuleAttrString(const char *mod_name, const char *attr_name)

Similar to PyImport_ImportModuleAttr(), but names are UTF-8 encoded strings instead of Python str objects.

For example, PyImport_ImportModuleAttrString("sys", "argv") gets sys.argv. It's similar to from sys import argv in Python.

The function is used by 24 C files in Python.

Links:

Vote:

UPDATE: Rename the functions from PyImport_GetModuleAttr[String]() to PyImport_ImportModuleAttr[String]().

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions