Skip to content

Commit

Permalink
Fix it for Windows (I hope)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroIntensity committed Oct 27, 2024
1 parent bd9f754 commit 75e93dd
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Lib/test/test_interpreters/test_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,27 +185,30 @@ def test_gh_109793(self):
self.assertEqual(proc.returncode, 1)

@support.requires_subprocess()
@unittest.skipIf(sys.platform == "win32", "SIGINT does not work on Windows")
def test_interrupt_thread_with_interpreter(self):
# See GH-126016: Subinterpreters that are created
# in another thread might not have their thread states
# cleaned up if the user interrupted joining with CTRL+C.
import subprocess
import signal

flags = subprocess.CREATE_NEW_PROCESS_GROUP if sys.platform == "win32" else 0
with subprocess.Popen([
sys.executable, "-c",
"import threading, _interpreters\n"
"def run_interp(): _interpreters.run_string(_interpreters.create(),"
"'import time; print(1, flush=True); time.sleep(5)')\n"
"threading.Thread(target=run_interp).start()"
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) as proc:
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, creationflags=flags) as proc:
with proc.stdout:
# Make sure that the thread has actually started
self.assertEqual(proc.stdout.read(1), "1")

# Send a KeyboardInterrupt to the process
proc.send_signal(signal.SIGINT)
if sys.platform == "win32":
proc.send_signal(signal.CTRL_C_EVENT)
else:
proc.send_signal(signal.SIGINT)
with proc.stderr:
self.assertIn("KeyboardInterrupt", proc.stderr.read())
proc.wait()
Expand Down

0 comments on commit 75e93dd

Please sign in to comment.