Skip to content

Commit 61eb4db

Browse files
committed
Add a test case.
Also fix the existing test cases that were broken.
1 parent bbb2e37 commit 61eb4db

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

Lib/test/test_interpreters/test_api.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,7 @@ def test_new_config(self):
18551855
allow_threads=True,
18561856
allow_daemon_threads=False,
18571857
check_multi_interp_extensions=True,
1858+
can_handle_signals=True,
18581859
gil='own',
18591860
),
18601861
'legacy': types.SimpleNamespace(
@@ -1864,6 +1865,7 @@ def test_new_config(self):
18641865
allow_threads=True,
18651866
allow_daemon_threads=True,
18661867
check_multi_interp_extensions=bool(Py_GIL_DISABLED),
1868+
can_handle_signals=False,
18671869
gil='shared',
18681870
),
18691871
'empty': types.SimpleNamespace(
@@ -1873,6 +1875,7 @@ def test_new_config(self):
18731875
allow_threads=False,
18741876
allow_daemon_threads=False,
18751877
check_multi_interp_extensions=False,
1878+
can_handle_signals=False,
18761879
gil='default',
18771880
),
18781881
}
@@ -2134,6 +2137,7 @@ def test_get_config(self):
21342137
with self.subTest('main'):
21352138
expected = _interpreters.new_config('legacy')
21362139
expected.gil = 'own'
2140+
expected.can_handle_signals = True
21372141
if Py_GIL_DISABLED:
21382142
expected.check_multi_interp_extensions = False
21392143
interpid, *_ = _interpreters.get_main()
@@ -2360,6 +2364,37 @@ def test_set___main___attrs(self):
23602364
)
23612365
self.assertEqual(rc, 0)
23622366

2367+
def test_interpreter_handles_signals(self):
2368+
import subprocess
2369+
import sys
2370+
import signal
2371+
2372+
interp_source = """if True:
2373+
import sys
2374+
import time
2375+
2376+
sys.stdout.write('x')
2377+
sys.stdout.flush()
2378+
time.sleep(10)
2379+
print("should never happen", flush=True)
2380+
"""
2381+
2382+
source = f"""if True:
2383+
from concurrent import interpreters
2384+
2385+
interp = interpreters.create()
2386+
interp.exec('''{interp_source}''')
2387+
"""
2388+
2389+
proc = subprocess.Popen([sys.executable, '-c', source],
2390+
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
2391+
stderr=subprocess.PIPE, close_fds=True)
2392+
self.assertEqual(proc.stdout.read(1), b'x')
2393+
proc.send_signal(signal.SIGINT)
2394+
stdout, stderr = proc.communicate()
2395+
self.assertEqual(stdout, b"")
2396+
self.assertIn(b"KeyboardInterrupt", stderr)
2397+
23632398

23642399
if __name__ == '__main__':
23652400
# Test needs to be a package, so we can do relative imports.

Python/interpconfig.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ _PyInterpreterConfig_InitFromState(PyInterpreterConfig *config,
262262
.allow_threads = FLAG(THREADS),
263263
.allow_daemon_threads = FLAG(DAEMON_THREADS),
264264
.check_multi_interp_extensions = FLAG(MULTI_INTERP_EXTENSIONS),
265+
.can_handle_signals = FLAG(CAN_HANDLE_SIGNALS),
265266
#undef FLAG
266267
.gil = interp->ceval.own_gil
267268
? PyInterpreterConfig_OWN_GIL

0 commit comments

Comments
 (0)