Skip to content

Commit f76b1cd

Browse files
committed
Add also PyImport_GetModuleAttr()
1 parent 5f320d1 commit f76b1cd

File tree

8 files changed

+21
-12
lines changed

8 files changed

+21
-12
lines changed

Doc/c-api/import.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,22 @@ Importing Modules
327327
initialization.
328328
329329
330-
.. c:function:: PyObject* PyImport_GetModuleAttrString(const char *mod_name, const char *attr_name)
330+
.. c:function:: PyObject* PyImport_GetModuleAttr(PyObject *mod_name, PyObject *attr_name)
331331
332332
Import the module *mod_name* and get its attribute *attr_name*.
333333
334-
Names must be UTF-8 encoded strings.
334+
Names must be Python :class:`str` objects.
335335
336336
Helper function combining :c:func:`PyImport_Import` and
337337
:c:func:`PyObject_GetAttr`. For example, it can raise :exc:`ImportError` if
338338
the module is not found, and :exc:`AttributeError` if the attribute doesn't
339339
exist.
340340
341341
.. versionadded:: 3.14
342+
343+
.. c:function:: PyObject* PyImport_GetModuleAttrString(const char *mod_name, const char *attr_name)
344+
345+
Similar to :c:func:`PyImport_GetModuleAttr`, but names are UTF-8 encoded
346+
strings instead of Python :class:`str` objects.
347+
348+
.. versionadded:: 3.14

Doc/whatsnew/3.14.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,8 @@ New features
13011301
bit-packing Python version numbers.
13021302
(Contributed by Petr Viktorin in :gh:`128629`.)
13031303

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

Include/cpython/import.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ struct _frozen {
2424

2525
PyAPI_DATA(const struct _frozen *) PyImport_FrozenModules;
2626

27+
PyAPI_FUNC(PyObject*) PyImport_GetModuleAttr(
28+
PyObject *mod_name,
29+
PyObject *attr_name);
2730
PyAPI_FUNC(PyObject*) PyImport_GetModuleAttrString(
2831
const char *mod_name,
2932
const char *attr_name);

Include/internal/pycore_import.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ extern int _PyImport_FixupBuiltin(
3131
PyObject *modules
3232
);
3333

34-
// Export for many shared extensions, like '_json'
35-
PyAPI_FUNC(PyObject*) _PyImport_GetModuleAttr(PyObject *, PyObject *);
36-
3734

3835
struct _import_runtime_state {
3936
/* The builtin modules (defined in config.c). */
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
Add :c:func:`PyImport_GetModuleAttrString` helper function to import a
2-
module and get an attribute of the module. Patch by Victor Stinner.
1+
Add :c:func:`PyImport_GetModuleAttr` and :c:func:`PyImport_GetModuleAttrString`
2+
helper functions to import a module and get an attribute of the module. Patch
3+
by Victor Stinner.

Modules/_json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ raise_errmsg(const char *msg, PyObject *s, Py_ssize_t end)
302302
/* Use JSONDecodeError exception to raise a nice looking ValueError subclass */
303303
_Py_DECLARE_STR(json_decoder, "json.decoder");
304304
PyObject *JSONDecodeError =
305-
_PyImport_GetModuleAttr(&_Py_STR(json_decoder), &_Py_ID(JSONDecodeError));
305+
PyImport_GetModuleAttr(&_Py_STR(json_decoder), &_Py_ID(JSONDecodeError));
306306
if (JSONDecodeError == NULL) {
307307
return;
308308
}

Python/import.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4174,7 +4174,7 @@ _PyImport_FiniExternal(PyInterpreterState *interp)
41744174
/******************/
41754175

41764176
PyObject *
4177-
_PyImport_GetModuleAttr(PyObject *modname, PyObject *attrname)
4177+
PyImport_GetModuleAttr(PyObject *modname, PyObject *attrname)
41784178
{
41794179
PyObject *mod = PyImport_Import(modname);
41804180
if (mod == NULL) {
@@ -4197,7 +4197,7 @@ PyImport_GetModuleAttrString(const char *modname, const char *attrname)
41974197
Py_DECREF(pmodname);
41984198
return NULL;
41994199
}
4200-
PyObject *result = _PyImport_GetModuleAttr(pmodname, pattrname);
4200+
PyObject *result = PyImport_GetModuleAttr(pmodname, pattrname);
42014201
Py_DECREF(pattrname);
42024202
Py_DECREF(pmodname);
42034203
return result;

Python/pylifecycle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2678,7 +2678,7 @@ create_stdio(const PyConfig *config, PyObject* io,
26782678

26792679
#ifdef HAVE_WINDOWS_CONSOLE_IO
26802680
/* Windows console IO is always UTF-8 encoded */
2681-
PyTypeObject *winconsoleio_type = (PyTypeObject *)_PyImport_GetModuleAttr(
2681+
PyTypeObject *winconsoleio_type = (PyTypeObject *)PyImport_GetModuleAttr(
26822682
&_Py_ID(_io), &_Py_ID(_WindowsConsoleIO));
26832683
if (winconsoleio_type == NULL) {
26842684
goto error;

0 commit comments

Comments
 (0)