]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105922: Use PyImport_AddModuleRef() function (#105999)
authorVictor Stinner <vstinner@python.org>
Thu, 22 Jun 2023 22:04:39 +0000 (00:04 +0200)
committerGitHub <noreply@github.com>
Thu, 22 Jun 2023 22:04:39 +0000 (00:04 +0200)
Replace PyImport_AddModuleObject() + Py_XNewRef() with
PyImport_AddModuleRef() to get directly a strong reference.

Python/import.c
Python/pythonrun.c

index 9b1ad87b84f6499c4fa2efd06177c571c33d7468..05da9506cc0b647fbbb33a5725cd319e65678d9e 100644 (file)
@@ -1382,8 +1382,7 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
         if (_PyUnicode_EqualToASCIIString(name, p->name)) {
             if (p->initfunc == NULL) {
                 /* Cannot re-init internal module ("sys" or "builtins") */
-                mod = PyImport_AddModuleObject(name);
-                return Py_XNewRef(mod);
+                return import_add_module(tstate, name);
             }
             mod = _PyImport_InitFunc_TrampolineCall(*p->initfunc);
             if (mod == NULL) {
index af82fa491511c10b42f37e8afa303e12aa3d9f8c..2986622a8b0dcfd3ec169abfd8195bdbb1d2d1c3 100644 (file)
@@ -276,7 +276,7 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
         return parse_res;
     }
 
-    PyObject *main_module = Py_XNewRef(PyImport_AddModuleObject(&_Py_ID(__main__)));
+    PyObject *main_module = PyImport_AddModuleRef("__main__");
     if (main_module == NULL) {
         _PyArena_Free(arena);
         return -1;