Skip to content

Commit

Permalink
pythongh-105922: PyImport_AddModule() uses Py_DECREF()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vstinner committed Jun 22, 2023
1 parent c38da1e commit 283783c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Doc/c-api/import.rst
Expand Up @@ -103,8 +103,8 @@ Importing Modules
Return the module object corresponding to a module name.
The *name* argument may be of the form ``package.module``. First check the
modules dictionary if there's one there, and if not, create a new one and
insert it in the modules dictionary.
:data:`sys.modules` dictionary if there's one there, and if not, create a
new one and insert it in the :data:`sys.modules` dictionary.
Return a :term:`strong reference` to the module on success. Return ``NULL``
with an exception set on failure.
Expand Down
11 changes: 1 addition & 10 deletions Python/import.c
Expand Up @@ -372,16 +372,7 @@ PyImport_AddModuleObject(PyObject *name)
if (!mod) {
return NULL;
}

// gh-86160: PyImport_AddModuleObject() returns a borrowed reference
PyObject *ref = PyWeakref_NewRef(mod, NULL);
Py_DECREF(mod);
if (ref == NULL) {
return NULL;
}

mod = PyWeakref_GetObject(ref);
Py_DECREF(ref);
Py_DECREF(mod); // sys.modules holds a strong reference
return mod; /* borrowed reference */
}

Expand Down

0 comments on commit 283783c

Please sign in to comment.