Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-105922: PyImport_AddModule() uses Py_DECREF() #105998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
vstinner committed Jun 22, 2023
commit cef28f1c62a75b6ba867ecc7aa989f51ea0930e9
11 changes: 1 addition & 10 deletions Python/import.c
Original file line number Diff line number Diff line change
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