summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2022-11-21 11:14:54 (GMT)
committerGitHub <noreply@github.com>2022-11-21 11:14:54 (GMT)
commitcb2ef8b2acbb231c207207d3375b2f8b0077a6ee (patch)
treef08be5cc20b4440b7c55f25e8d195790d42535ce /Lib
parent1cae31d26ba621f6b1f0656ad3d69a0236338bad (diff)
downloadcpython-cb2ef8b2acbb231c207207d3375b2f8b0077a6ee.zip
cpython-cb2ef8b2acbb231c207207d3375b2f8b0077a6ee.tar.gz
cpython-cb2ef8b2acbb231c207207d3375b2f8b0077a6ee.tar.bz2
gh-99578: Fix refleak in _imp.create_builtin() (#99642)
Fix a reference bug in _imp.create_builtin() after the creation of the first sub-interpreter for modules "builtins" and "sys".
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_imp.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/test/test_imp.py b/Lib/test/test_imp.py
index 446e913..80abc72 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
@@ -413,6 +414,35 @@ class ImportTests(unittest.TestCase):
bltin = create_builtin(spec)
self.assertEqual(bltin, builtins)
+ @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