Skip to content

Commit 283783c

Browse files
committed
gh-105922: PyImport_AddModule() uses Py_DECREF()
Rewrite PyImport_AddModule() to simply call Py_DECREF(), rather than creating a weak reference, to get a borrowed reference to the module. In the documentation, add a link to sys.modules to explicit which "modules" are checked.
1 parent c38da1e commit 283783c

File tree

2 files changed

+3
-12
lines changed

2 files changed

+3
-12
lines changed

Doc/c-api/import.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ Importing Modules
103103
Return the module object corresponding to a module name.
104104
105105
The *name* argument may be of the form ``package.module``. First check the
106-
modules dictionary if there's one there, and if not, create a new one and
107-
insert it in the modules dictionary.
106+
:data:`sys.modules` dictionary if there's one there, and if not, create a
107+
new one and insert it in the :data:`sys.modules` dictionary.
108108
109109
Return a :term:`strong reference` to the module on success. Return ``NULL``
110110
with an exception set on failure.

Python/import.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,16 +372,7 @@ PyImport_AddModuleObject(PyObject *name)
372372
if (!mod) {
373373
return NULL;
374374
}
375-
376-
// gh-86160: PyImport_AddModuleObject() returns a borrowed reference
377-
PyObject *ref = PyWeakref_NewRef(mod, NULL);
378-
Py_DECREF(mod);
379-
if (ref == NULL) {
380-
return NULL;
381-
}
382-
383-
mod = PyWeakref_GetObject(ref);
384-
Py_DECREF(ref);
375+
Py_DECREF(mod); // sys.modules holds a strong reference
385376
return mod; /* borrowed reference */
386377
}
387378

0 commit comments

Comments
 (0)