summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-08-02 20:55:09 (GMT)
committerGitHub <noreply@github.com>2023-08-02 20:55:09 (GMT)
commit017f047183fa33743f7e36c5c360f5c670032be3 (patch)
treea0afb9c5b2019c9638c6b4d37ff4974ad0940230 /Lib
parentbcdd3072316181b49d94567bb648825a07ca9ae1 (diff)
downloadcpython-017f047183fa33743f7e36c5c360f5c670032be3.zip
cpython-017f047183fa33743f7e36c5c360f5c670032be3.tar.gz
cpython-017f047183fa33743f7e36c5c360f5c670032be3.tar.bz2
gh-107471: Fix Refleaks in test_import (gh-107569)
gh-107184 introduced a refleak in test_import.SubinterpImportTests (specifically test_singlephase_check_with_setting_and_override and test_single_init_extension_compat). We fix it here by making sure _testsinglephase is removed from sys.modules whenever we clear the runtime's internal state for the module. The underlying problem is strictly contained in the internal function _PyImport_ClearExtension() (AKA _testinternalcapi.clear_extension()), which is only used in tests. (This also fixes an intermittent segfault introduced in the same place, in test_disallowed_reimport.)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_import/__init__.py3
1 files changed, 2 insertions, 1 deletions
diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py
index 7a3fcc2..163ed82 100644
--- a/Lib/test/test_import/__init__.py
+++ b/Lib/test/test_import/__init__.py
@@ -150,6 +150,7 @@ if _testsinglephase is not None:
def restore__testsinglephase(*, _orig=_testsinglephase):
# We started with the module imported and want to restore
# it to its nominal state.
+ sys.modules.pop('_testsinglephase', None)
_orig._clear_globals()
_testinternalcapi.clear_extension('_testsinglephase', _orig.__file__)
import _testsinglephase
@@ -2125,7 +2126,7 @@ class SinglephaseInitTests(unittest.TestCase):
_interpreters.run_string(interpid, textwrap.dedent(f'''
name = {self.NAME!r}
if name in sys.modules:
- sys.modules[name]._clear_globals()
+ sys.modules.pop(name)._clear_globals()
_testinternalcapi.clear_extension(name, {self.FILE!r})
'''))
_interpreters.destroy(interpid)