From 833d44fd1a8d635bea715bb5b45839cd7a7e790a Mon Sep 17 00:00:00 2001 From: Claudio Jolowicz Date: Wed, 3 Mar 2021 00:01:33 +0100 Subject: [PATCH] Fix spurious interrupts in Windows tests --- tests/test_command.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/tests/test_command.py b/tests/test_command.py index d4b7f9fb..03ef79cf 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -207,6 +207,31 @@ def marker(tmp_path): return tmp_path / "marker" +def interrupt_process(proc): + """Send SIGINT or CTRL_C_EVENT to the process.""" + if platform.system() == "Windows": + # https://stackoverflow.com/a/35792192 + import threading + + handler = signal.getsignal(signal.SIGINT) + event = threading.Event() + + def handler_set_event(signum, frame): + event.set() + return handler(signum, frame) + + signal.signal(signal.SIGINT, handler_set_event) + + try: + os.kill(0, signal.CTRL_C_EVENT) + while not event.is_set(): + pass + finally: + signal.signal(signal.SIGINT, handler) + else: + proc.send_signal(signal.SIGINT) + + @pytest.fixture def command_with_keyboard_interrupt(monkeypatch, marker): """Monkeypatch Popen.communicate to raise KeyboardInterrupt.""" @@ -224,9 +249,7 @@ def wrapper(proc, *args, **kwargs): time.sleep(0.05) # Send a real keyboard interrupt to the child. - proc.send_signal( - signal.CTRL_C_EVENT if platform.system() == "Windows" else signal.SIGINT - ) + interrupt_process(proc) # Fake a keyboard interrupt in the parent. raise KeyboardInterrupt