[Python-checkins] gh-99578: Fix refleak in _imp.create_builtin() (GH-99642)

miss-islington webhook-mailer at python.org
Mon Nov 21 06:45:01 EST 2022


https://github.com/python/cpython/commit/37dbbb208fd9e01890bfb2c741e89359582d5569
commit: 37dbbb208fd9e01890bfb2c741e89359582d5569
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-11-21T03:44:56-08:00
summary:

gh-99578: Fix refleak in _imp.create_builtin() (GH-99642)


Fix a reference bug in _imp.create_builtin() after the creation of
the first sub-interpreter for modules "builtins" and "sys".
(cherry picked from commit cb2ef8b2acbb231c207207d3375b2f8b0077a6ee)

Co-authored-by: Victor Stinner <vstinner at python.org>

files:
A Misc/NEWS.d/next/Core and Builtins/2022-11-21-11-27-14.gh-issue-99578.DcKoBJ.rst
M Lib/test/test_imp.py
M Python/import.c

diff --git a/Lib/test/test_imp.py b/Lib/test/test_imp.py
index 5abe28ef62c8..21fec2ad960e 100644
--- a/Lib/test/test_imp.py
+++ b/Lib/test/test_imp.py
@@ -1,3 +1,4 @@
+import gc
 import importlib
 import importlib.util
 import os
@@ -379,6 +380,35 @@ def test_find_and_load_checked_pyc(self):
         self.assertEqual(mod.x, 42)
 
 
+    @support.cpython_only
+    def test_create_builtin_subinterp(self):
+        # gh-99578: create_builtin() behavior changes after the creation of the
+        # first sub-interpreter. Test both code paths, before and after the
+        # creation of a sub-interpreter. Previously, create_builtin() had
+        # a reference leak after the creation of the first sub-interpreter.
+
+        import builtins
+        create_builtin = support.get_attribute(_imp, "create_builtin")
+        class Spec:
+            name = "builtins"
+        spec = Spec()
+
+        def check_get_builtins():
+            refcnt = sys.getrefcount(builtins)
+            mod = _imp.create_builtin(spec)
+            self.assertIs(mod, builtins)
+            self.assertEqual(sys.getrefcount(builtins), refcnt + 1)
+            # Check that a GC collection doesn't crash
+            gc.collect()
+
+        check_get_builtins()
+
+        ret = support.run_in_subinterp("import builtins")
+        self.assertEqual(ret, 0)
+
+        check_get_builtins()
+
+
 class ReloadTests(unittest.TestCase):
 
     """Very basic tests to make sure that imp.reload() operates just like
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-11-21-11-27-14.gh-issue-99578.DcKoBJ.rst b/Misc/NEWS.d/next/Core and Builtins/2022-11-21-11-27-14.gh-issue-99578.DcKoBJ.rst
new file mode 100644
index 000000000000..9321cef77eed
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-11-21-11-27-14.gh-issue-99578.DcKoBJ.rst	
@@ -0,0 +1,3 @@
+Fix a reference bug in :func:`_imp.create_builtin()` after the creation of the
+first sub-interpreter for modules ``builtins`` and ``sys``. Patch by Victor
+Stinner.
diff --git a/Python/import.c b/Python/import.c
index 58d111705a4d..18ef26f5c2fc 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -1006,7 +1006,8 @@ 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") */
-                return PyImport_AddModuleObject(name);
+                mod = PyImport_AddModuleObject(name);
+                return Py_XNewRef(mod);
             }
 
             mod = (*p->initfunc)();



More information about the Python-checkins mailing list