Skip to content

Commit 29b0dc9

Browse files
committed
Add a test case.
1 parent 926e45d commit 29b0dc9

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

Lib/test/test_interpreters/test_api.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import contextlib
22
import os
33
import pickle
4+
import signal
45
import sys
56
from textwrap import dedent
67
import threading
@@ -11,7 +12,7 @@
1112
from test.support import os_helper
1213
from test.support import script_helper
1314
from test.support import import_helper
14-
from test.support.script_helper import assert_python_ok
15+
from test.support.script_helper import assert_python_ok, spawn_python
1516
# Raise SkipTest if subinterpreters not supported.
1617
_interpreters = import_helper.import_module('_interpreters')
1718
from concurrent import interpreters
@@ -434,6 +435,32 @@ def test_cleanup_in_repl(self):
434435
self.assertIn(b"remaining subinterpreters", stdout)
435436
self.assertNotIn(b"Traceback", stdout)
436437

438+
@support.requires_subprocess()
439+
@unittest.skipIf(os.name == 'nt', "signals don't work well on windows")
440+
def test_keyboard_interrupt_in_thread_running_interp(self):
441+
import subprocess
442+
source = f"""if True:
443+
from concurrent import interpreters
444+
from threading import Thread
445+
446+
def test():
447+
import time
448+
print('a', flush=True, end='')
449+
time.sleep(10)
450+
451+
interp = interpreters.create()
452+
interp.call_in_thread(test)
453+
"""
454+
455+
with spawn_python("-c", source, stderr=subprocess.PIPE) as proc:
456+
self.assertEqual(proc.stdout.read(1), b'a')
457+
proc.send_signal(signal.SIGINT)
458+
proc.stderr.flush()
459+
error = proc.stderr.read()
460+
self.assertIn(b"KeyboardInterrupt", error)
461+
retcode = proc.wait()
462+
self.assertEqual(retcode, 0)
463+
437464

438465

439466
class TestInterpreterIsRunning(TestBase):

0 commit comments

Comments
 (0)