mirror of
https://github.com/python/cpython.git
synced 2025-10-06 23:21:06 +00:00
bpo-32604: Clean up created subinterpreters before runtime finalization. (gh-5710)
(cherry picked from commit 4c6955e2b0
)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
This commit is contained in:
parent
803e1a5722
commit
3db05a3a9c
5 changed files with 410 additions and 31 deletions
|
@ -152,7 +152,7 @@ class GetCurrentTests(TestBase):
|
|||
interp = interpreters.create()
|
||||
out = _run_output(interp, dedent("""
|
||||
import _xxsubinterpreters as _interpreters
|
||||
print(_interpreters.get_current())
|
||||
print(int(_interpreters.get_current()))
|
||||
"""))
|
||||
cur = int(out.strip())
|
||||
_, expected = interpreters.list_all()
|
||||
|
@ -172,7 +172,7 @@ class GetMainTests(TestBase):
|
|||
interp = interpreters.create()
|
||||
out = _run_output(interp, dedent("""
|
||||
import _xxsubinterpreters as _interpreters
|
||||
print(_interpreters.get_main())
|
||||
print(int(_interpreters.get_main()))
|
||||
"""))
|
||||
main = int(out.strip())
|
||||
self.assertEqual(main, expected)
|
||||
|
@ -196,7 +196,7 @@ class IsRunningTests(TestBase):
|
|||
interp = interpreters.create()
|
||||
out = _run_output(interp, dedent(f"""
|
||||
import _xxsubinterpreters as _interpreters
|
||||
if _interpreters.is_running({interp}):
|
||||
if _interpreters.is_running({int(interp)}):
|
||||
print(True)
|
||||
else:
|
||||
print(False)
|
||||
|
@ -218,6 +218,63 @@ class IsRunningTests(TestBase):
|
|||
interpreters.is_running(-1)
|
||||
|
||||
|
||||
class InterpreterIDTests(TestBase):
|
||||
|
||||
def test_with_int(self):
|
||||
id = interpreters.InterpreterID(10, force=True)
|
||||
|
||||
self.assertEqual(int(id), 10)
|
||||
|
||||
def test_coerce_id(self):
|
||||
id = interpreters.InterpreterID('10', force=True)
|
||||
self.assertEqual(int(id), 10)
|
||||
|
||||
id = interpreters.InterpreterID(10.0, force=True)
|
||||
self.assertEqual(int(id), 10)
|
||||
|
||||
class Int(str):
|
||||
def __init__(self, value):
|
||||
self._value = value
|
||||
def __int__(self):
|
||||
return self._value
|
||||
|
||||
id = interpreters.InterpreterID(Int(10), force=True)
|
||||
self.assertEqual(int(id), 10)
|
||||
|
||||
def test_bad_id(self):
|
||||
for id in [-1, 'spam']:
|
||||
with self.subTest(id):
|
||||
with self.assertRaises(ValueError):
|
||||
interpreters.InterpreterID(id)
|
||||
with self.assertRaises(OverflowError):
|
||||
interpreters.InterpreterID(2**64)
|
||||
with self.assertRaises(TypeError):
|
||||
interpreters.InterpreterID(object())
|
||||
|
||||
def test_does_not_exist(self):
|
||||
id = interpreters.channel_create()
|
||||
with self.assertRaises(RuntimeError):
|
||||
interpreters.InterpreterID(int(id) + 1) # unforced
|
||||
|
||||
def test_repr(self):
|
||||
id = interpreters.InterpreterID(10, force=True)
|
||||
self.assertEqual(repr(id), 'InterpreterID(10)')
|
||||
|
||||
def test_equality(self):
|
||||
id1 = interpreters.create()
|
||||
id2 = interpreters.InterpreterID(int(id1))
|
||||
id3 = interpreters.create()
|
||||
|
||||
self.assertTrue(id1 == id1)
|
||||
self.assertTrue(id1 == id2)
|
||||
self.assertTrue(id1 == int(id1))
|
||||
self.assertFalse(id1 == id3)
|
||||
|
||||
self.assertFalse(id1 != id1)
|
||||
self.assertFalse(id1 != id2)
|
||||
self.assertTrue(id1 != id3)
|
||||
|
||||
|
||||
class CreateTests(TestBase):
|
||||
|
||||
def test_in_main(self):
|
||||
|
@ -256,7 +313,7 @@ class CreateTests(TestBase):
|
|||
out = _run_output(id1, dedent("""
|
||||
import _xxsubinterpreters as _interpreters
|
||||
id = _interpreters.create()
|
||||
print(id)
|
||||
print(int(id))
|
||||
"""))
|
||||
id2 = int(out.strip())
|
||||
|
||||
|
@ -271,7 +328,7 @@ class CreateTests(TestBase):
|
|||
out = _run_output(id1, dedent("""
|
||||
import _xxsubinterpreters as _interpreters
|
||||
id = _interpreters.create()
|
||||
print(id)
|
||||
print(int(id))
|
||||
"""))
|
||||
id2 = int(out.strip())
|
||||
|
||||
|
@ -365,7 +422,7 @@ class DestroyTests(TestBase):
|
|||
script = dedent(f"""
|
||||
import _xxsubinterpreters as _interpreters
|
||||
try:
|
||||
_interpreters.destroy({id})
|
||||
_interpreters.destroy({int(id)})
|
||||
except RuntimeError:
|
||||
pass
|
||||
""")
|
||||
|
@ -377,10 +434,10 @@ class DestroyTests(TestBase):
|
|||
main, = interpreters.list_all()
|
||||
id1 = interpreters.create()
|
||||
id2 = interpreters.create()
|
||||
script = dedent("""
|
||||
script = dedent(f"""
|
||||
import _xxsubinterpreters as _interpreters
|
||||
_interpreters.destroy({})
|
||||
""").format(id2)
|
||||
_interpreters.destroy({int(id2)})
|
||||
""")
|
||||
interpreters.run_string(id1, script)
|
||||
|
||||
self.assertEqual(set(interpreters.list_all()), {main, id1})
|
||||
|
@ -699,11 +756,14 @@ class RunStringTests(TestBase):
|
|||
'spam': 42,
|
||||
})
|
||||
|
||||
# XXX Fix this test!
|
||||
@unittest.skip('blocking forever')
|
||||
def test_still_running_at_exit(self):
|
||||
script = dedent(f"""
|
||||
from textwrap import dedent
|
||||
import threading
|
||||
import _xxsubinterpreters as _interpreters
|
||||
id = _interpreters.create()
|
||||
def f():
|
||||
_interpreters.run_string(id, dedent('''
|
||||
import time
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue