Skip to content

Commit

Permalink
Add also PyImport_GetModuleAttr()
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Jan 16, 2025
1 parent 5f320d1 commit f76b1cd
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 12 deletions.
11 changes: 9 additions & 2 deletions Doc/c-api/import.rst
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,22 @@ Importing Modules
initialization.
.. c:function:: PyObject* PyImport_GetModuleAttrString(const char *mod_name, const char *attr_name)
.. c:function:: PyObject* PyImport_GetModuleAttr(PyObject *mod_name, PyObject *attr_name)
Import the module *mod_name* and get its attribute *attr_name*.
Names must be UTF-8 encoded strings.
Names must be Python :class:`str` objects.
Helper function combining :c:func:`PyImport_Import` and
:c:func:`PyObject_GetAttr`. For example, it can raise :exc:`ImportError` if
the module is not found, and :exc:`AttributeError` if the attribute doesn't
exist.
.. versionadded:: 3.14
.. c:function:: PyObject* PyImport_GetModuleAttrString(const char *mod_name, const char *attr_name)
Similar to :c:func:`PyImport_GetModuleAttr`, but names are UTF-8 encoded
strings instead of Python :class:`str` objects.
.. versionadded:: 3.14
3 changes: 2 additions & 1 deletion Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,8 @@ New features
bit-packing Python version numbers.
(Contributed by Petr Viktorin in :gh:`128629`.)

* Add :c:func:`PyImport_GetModuleAttrString` helper function to import a module
* Add :c:func:`PyImport_GetModuleAttr` and
:c:func:`PyImport_GetModuleAttrString` helper functions to import a module
and get an attribute of the module.
(Contributed by Victor Stinner in :gh:`128911`.)

Expand Down
3 changes: 3 additions & 0 deletions Include/cpython/import.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ struct _frozen {

PyAPI_DATA(const struct _frozen *) PyImport_FrozenModules;

PyAPI_FUNC(PyObject*) PyImport_GetModuleAttr(
PyObject *mod_name,
PyObject *attr_name);
PyAPI_FUNC(PyObject*) PyImport_GetModuleAttrString(
const char *mod_name,
const char *attr_name);
3 changes: 0 additions & 3 deletions Include/internal/pycore_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ extern int _PyImport_FixupBuiltin(
PyObject *modules
);

// Export for many shared extensions, like '_json'
PyAPI_FUNC(PyObject*) _PyImport_GetModuleAttr(PyObject *, PyObject *);


struct _import_runtime_state {
/* The builtin modules (defined in config.c). */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Add :c:func:`PyImport_GetModuleAttrString` helper function to import a
module and get an attribute of the module. Patch by Victor Stinner.
Add :c:func:`PyImport_GetModuleAttr` and :c:func:`PyImport_GetModuleAttrString`
helper functions to import a module and get an attribute of the module. Patch
by Victor Stinner.
2 changes: 1 addition & 1 deletion Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ raise_errmsg(const char *msg, PyObject *s, Py_ssize_t end)
/* Use JSONDecodeError exception to raise a nice looking ValueError subclass */
_Py_DECLARE_STR(json_decoder, "json.decoder");
PyObject *JSONDecodeError =
_PyImport_GetModuleAttr(&_Py_STR(json_decoder), &_Py_ID(JSONDecodeError));
PyImport_GetModuleAttr(&_Py_STR(json_decoder), &_Py_ID(JSONDecodeError));
if (JSONDecodeError == NULL) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -4174,7 +4174,7 @@ _PyImport_FiniExternal(PyInterpreterState *interp)
/******************/

PyObject *
_PyImport_GetModuleAttr(PyObject *modname, PyObject *attrname)
PyImport_GetModuleAttr(PyObject *modname, PyObject *attrname)
{
PyObject *mod = PyImport_Import(modname);
if (mod == NULL) {
Expand All @@ -4197,7 +4197,7 @@ PyImport_GetModuleAttrString(const char *modname, const char *attrname)
Py_DECREF(pmodname);
return NULL;
}
PyObject *result = _PyImport_GetModuleAttr(pmodname, pattrname);
PyObject *result = PyImport_GetModuleAttr(pmodname, pattrname);
Py_DECREF(pattrname);
Py_DECREF(pmodname);
return result;
Expand Down
2 changes: 1 addition & 1 deletion Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2678,7 +2678,7 @@ create_stdio(const PyConfig *config, PyObject* io,

#ifdef HAVE_WINDOWS_CONSOLE_IO
/* Windows console IO is always UTF-8 encoded */
PyTypeObject *winconsoleio_type = (PyTypeObject *)_PyImport_GetModuleAttr(
PyTypeObject *winconsoleio_type = (PyTypeObject *)PyImport_GetModuleAttr(
&_Py_ID(_io), &_Py_ID(_WindowsConsoleIO));
if (winconsoleio_type == NULL) {
goto error;
Expand Down

0 comments on commit f76b1cd

Please sign in to comment.