gh-136003: Execute pre-finalization callbacks in a loop (GH-136004)

This commit is contained in:
Peter Bierma 2025-09-18 08:29:12 -04:00 committed by GitHub
parent d6a6fe2a5b
commit 2191497933
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 281 additions and 43 deletions

View file

@ -22,6 +22,7 @@
from test import support
from test.support import MISSING_C_DOCSTRINGS
from test.support import import_helper
from test.support import script_helper
from test.support import threading_helper
from test.support import warnings_helper
from test.support import requires_limited_api
@ -1641,6 +1642,36 @@ def subthread():
self.assertEqual(actual, int(interpid))
@threading_helper.requires_working_threading()
def test_pending_call_creates_thread(self):
source = """
import _testinternalcapi
import threading
import time
def output():
print(24)
time.sleep(1)
print(42)
def callback():
threading.Thread(target=output).start()
def create_pending_call():
time.sleep(1)
_testinternalcapi.simple_pending_call(callback)
threading.Thread(target=create_pending_call).start()
"""
return_code, stdout, stderr = script_helper.assert_python_ok('-c', textwrap.dedent(source))
self.assertEqual(return_code, 0)
self.assertEqual(stdout, f"24{os.linesep}42{os.linesep}".encode("utf-8"))
self.assertEqual(stderr, b"")
class SubinterpreterTest(unittest.TestCase):
@ -1949,6 +1980,41 @@ def test_module_state_shared_in_global(self):
subinterp_attr_id = os.read(r, 100)
self.assertEqual(main_attr_id, subinterp_attr_id)
@threading_helper.requires_working_threading()
@unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
@requires_subinterpreters
def test_pending_call_creates_thread_subinterpreter(self):
interpreters = import_helper.import_module("concurrent.interpreters")
r, w = os.pipe()
source = f"""if True:
import _testinternalcapi
import threading
import time
import os
def output():
time.sleep(1)
os.write({w}, b"x")
def callback():
threading.Thread(target=output).start()
def create_pending_call():
time.sleep(1)
_testinternalcapi.simple_pending_call(callback)
threading.Thread(target=create_pending_call).start()
"""
interp = interpreters.create()
interp.exec(source)
interp.close()
data = os.read(r, 1)
self.assertEqual(data, b"x")
@requires_subinterpreters
class InterpreterConfigTests(unittest.TestCase):