mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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 cb2ef8b2ac
)
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
bb4c091583
commit
9dda9020ab
3 changed files with 35 additions and 1 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue