]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-99578: Fix refleak in _imp.create_builtin() (GH-99642)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 21 Nov 2022 11:43:23 +0000 (03:43 -0800)
committerGitHub <noreply@github.com>
Mon, 21 Nov 2022 11:43:23 +0000 (03:43 -0800)
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@python.org>
Lib/test/test_imp.py
Misc/NEWS.d/next/Core and Builtins/2022-11-21-11-27-14.gh-issue-99578.DcKoBJ.rst [new file with mode: 0644]
Python/import.c

index d44dc6b49f2935bbb0392e9df3f2bf082fc7dbdf..4bb03908fc2bb27ffb38deff4832fab3d33e8b10 100644 (file)
@@ -1,3 +1,4 @@
+import gc
 import importlib
 import importlib.util
 import os
@@ -383,6 +384,35 @@ class ImportTests(unittest.TestCase):
         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 (file)
index 0000000..9321cef
--- /dev/null
@@ -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.
index ca728c424766c7b31bb5e1c60b9b8bedee49c4a6..07a8b9009290f4f8b61a00c07a3a5ea6636b036a 100644 (file)
@@ -978,7 +978,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 = _PyImport_InitFunc_TrampolineCall(*p->initfunc);
             if (mod == NULL) {