Skip to content

Commit 5f320d1

Browse files
committed
gh-128911: Add PyImport_GetModuleAttrString() function
Remove "pycore_import.h" includes, no longer needed.
1 parent 313b96e commit 5f320d1

32 files changed

+66
-47
lines changed

Doc/c-api/import.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,17 @@ Importing Modules
325325
If Python is initialized multiple times, :c:func:`PyImport_AppendInittab` or
326326
:c:func:`PyImport_ExtendInittab` must be called before each Python
327327
initialization.
328+
329+
330+
.. c:function:: PyObject* PyImport_GetModuleAttrString(const char *mod_name, const char *attr_name)
331+
332+
Import the module *mod_name* and get its attribute *attr_name*.
333+
334+
Names must be UTF-8 encoded strings.
335+
336+
Helper function combining :c:func:`PyImport_Import` and
337+
:c:func:`PyObject_GetAttr`. For example, it can raise :exc:`ImportError` if
338+
the module is not found, and :exc:`AttributeError` if the attribute doesn't
339+
exist.
340+
341+
.. versionadded:: 3.14

Doc/data/refcounts.dat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,3 +3052,7 @@ _Py_c_quot:Py_complex:divisor::
30523052
_Py_c_sum:Py_complex:::
30533053
_Py_c_sum:Py_complex:left::
30543054
_Py_c_sum:Py_complex:right::
3055+
3056+
PyImport_GetModuleAttrString:PyObject*::+1:
3057+
PyImport_GetModuleAttrString:const char *:mod_name::
3058+
PyImport_GetModuleAttrString:const char *:attr_name::

Doc/whatsnew/3.14.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,10 @@ 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
1305+
and get an attribute of the module.
1306+
(Contributed by Victor Stinner in :gh:`128911`.)
1307+
13041308

13051309
Porting to Python 3.14
13061310
----------------------

Include/cpython/import.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ struct _frozen {
2323
collection of frozen modules: */
2424

2525
PyAPI_DATA(const struct _frozen *) PyImport_FrozenModules;
26+
27+
PyAPI_FUNC(PyObject*) PyImport_GetModuleAttrString(
28+
const char *mod_name,
29+
const char *attr_name);

Include/internal/pycore_import.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ extern int _PyImport_FixupBuiltin(
3434
// Export for many shared extensions, like '_json'
3535
PyAPI_FUNC(PyObject*) _PyImport_GetModuleAttr(PyObject *, PyObject *);
3636

37-
// Export for many shared extensions, like '_datetime'
38-
PyAPI_FUNC(PyObject*) _PyImport_GetModuleAttrString(const char *, const char *);
39-
4037

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

Modules/_ctypes/callbacks.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
492492
if (context == NULL)
493493
context = PyUnicode_InternFromString("_ctypes.DllGetClassObject");
494494

495-
func = _PyImport_GetModuleAttrString("ctypes", "DllGetClassObject");
495+
func = PyImport_GetModuleAttrString("ctypes", "DllGetClassObject");
496496
if (!func) {
497497
PyErr_WriteUnraisable(context ? context : Py_None);
498498
/* There has been a warning before about this already */

Modules/_ctypes/stgdict.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct
257257
goto error;
258258
}
259259

260-
PyObject *layout_func = _PyImport_GetModuleAttrString("ctypes._layout",
260+
PyObject *layout_func = PyImport_GetModuleAttrString("ctypes._layout",
261261
"get_layout");
262262
if (!layout_func) {
263263
goto error;

Modules/_cursesmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ _PyCursesCheckFunction(int called, const char *funcname)
226226
if (called == TRUE) {
227227
return 1;
228228
}
229-
PyObject *exc = _PyImport_GetModuleAttrString("_curses", "error");
229+
PyObject *exc = PyImport_GetModuleAttrString("_curses", "error");
230230
if (exc != NULL) {
231231
PyErr_Format(exc, "must call %s() first", funcname);
232232
Py_DECREF(exc);

Modules/_datetimemodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,7 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
18391839
assert(object && format && timetuple);
18401840
assert(PyUnicode_Check(format));
18411841

1842-
PyObject *strftime = _PyImport_GetModuleAttrString("time", "strftime");
1842+
PyObject *strftime = PyImport_GetModuleAttrString("time", "strftime");
18431843
if (strftime == NULL) {
18441844
return NULL;
18451845
}
@@ -2021,7 +2021,7 @@ static PyObject *
20212021
time_time(void)
20222022
{
20232023
PyObject *result = NULL;
2024-
PyObject *time = _PyImport_GetModuleAttrString("time", "time");
2024+
PyObject *time = PyImport_GetModuleAttrString("time", "time");
20252025

20262026
if (time != NULL) {
20272027
result = PyObject_CallNoArgs(time);
@@ -2039,7 +2039,7 @@ build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag)
20392039
PyObject *struct_time;
20402040
PyObject *result;
20412041

2042-
struct_time = _PyImport_GetModuleAttrString("time", "struct_time");
2042+
struct_time = PyImport_GetModuleAttrString("time", "struct_time");
20432043
if (struct_time == NULL) {
20442044
return NULL;
20452045
}

0 commit comments

Comments
 (0)