summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_interpreters/utils.py
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2024-04-11 23:23:25 (GMT)
committerGitHub <noreply@github.com>2024-04-11 23:23:25 (GMT)
commitfd259fdabe95329768eb3e90039c77888c130a4c (patch)
treef4e564a993f0bf702c884ba3e96bdbb6e968fccb /Lib/test/test_interpreters/utils.py
parentfd2bab9d287ef0879568662d4fedeae0a0c61d43 (diff)
downloadcpython-fd259fdabe95329768eb3e90039c77888c130a4c.zip
cpython-fd259fdabe95329768eb3e90039c77888c130a4c.tar.gz
cpython-fd259fdabe95329768eb3e90039c77888c130a4c.tar.bz2
gh-76785: Handle Legacy Interpreters Properly (gh-117490)
This is similar to the situation with threading._DummyThread. The methods (incl. __del__()) of interpreters.Interpreter objects must be careful with interpreters not created by interpreters.create(). The simplest thing to start with is to disable any method that modifies or runs in the interpreter. As part of this, the runtime keeps track of where an interpreter was created. We also handle interpreter "refcounts" properly.
Diffstat (limited to 'Lib/test/test_interpreters/utils.py')
-rw-r--r--Lib/test/test_interpreters/utils.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/Lib/test/test_interpreters/utils.py b/Lib/test/test_interpreters/utils.py
index d921794..08768c0 100644
--- a/Lib/test/test_interpreters/utils.py
+++ b/Lib/test/test_interpreters/utils.py
@@ -509,6 +509,14 @@ class TestBase(unittest.TestCase):
else:
return text
+ def interp_exists(self, interpid):
+ try:
+ _interpreters.whence(interpid)
+ except _interpreters.InterpreterNotFoundError:
+ return False
+ else:
+ return True
+
@requires_test_modules
@contextlib.contextmanager
def interpreter_from_capi(self, config=None, whence=None):
@@ -545,7 +553,12 @@ class TestBase(unittest.TestCase):
@contextlib.contextmanager
def interpreter_obj_from_capi(self, config='legacy'):
with self.interpreter_from_capi(config) as interpid:
- yield interpreters.Interpreter(interpid), interpid
+ interp = interpreters.Interpreter(
+ interpid,
+ _whence=_interpreters.WHENCE_CAPI,
+ _ownsref=False,
+ )
+ yield interp, interpid
@contextlib.contextmanager
def capturing(self, script):