Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 67ba547

Browse files
vladimaasvetlov
authored andcommitted
bpo-23057: Use 'raise' to emulate ctrl-c in proactor tests (#11274)
1 parent 31ec52a commit 67ba547

3 files changed

Lines changed: 24 additions & 76 deletions

File tree

Lib/asyncio/windows_events.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,12 @@ def run_forever(self):
315315
super().run_forever()
316316
finally:
317317
if self._self_reading_future is not None:
318+
ov = self._self_reading_future._ov
318319
self._self_reading_future.cancel()
320+
# self_reading_future was just cancelled so it will never be signalled
321+
# Unregister it otherwise IocpProactor.close will wait for it forever
322+
if ov is not None:
323+
self._proactor._unregister(ov)
319324
self._self_reading_future = None
320325

321326
async def create_pipe_connection(self, protocol_factory, address):

Lib/test/test_asyncio/test_ctrl_c_in_proactor_loop_helper.py

Lines changed: 0 additions & 63 deletions
This file was deleted.

Lib/test/test_asyncio/test_windows_events.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import sys
55
import subprocess
66
import time
7+
import threading
78
import unittest
89
from unittest import mock
910

1011
if sys.platform != 'win32':
1112
raise unittest.SkipTest('Windows only')
1213

1314
import _overlapped
15+
import _testcapi
1416
import _winapi
1517

1618
import asyncio
@@ -38,20 +40,24 @@ def data_received(self, data):
3840

3941

4042
class ProactorLoopCtrlC(test_utils.TestCase):
43+
4144
def test_ctrl_c(self):
42-
from .test_ctrl_c_in_proactor_loop_helper import __file__ as f
43-
44-
# ctrl-c will be sent to all processes that share the same console
45-
# in order to isolate the effect of raising ctrl-c we'll create
46-
# a process with a new console
47-
flags = subprocess.CREATE_NEW_CONSOLE
48-
with spawn_python(f, creationflags=flags) as p:
49-
try:
50-
exit_code = p.wait(timeout=5)
51-
self.assertEqual(exit_code, 255)
52-
except:
53-
p.kill()
54-
raise
45+
46+
def SIGINT_after_delay():
47+
time.sleep(1)
48+
_testcapi.raise_signal(signal.SIGINT)
49+
50+
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
51+
l = asyncio.get_event_loop()
52+
try:
53+
t = threading.Thread(target=SIGINT_after_delay)
54+
t.start()
55+
l.run_forever()
56+
self.fail("should not fall through 'run_forever'")
57+
except KeyboardInterrupt:
58+
pass
59+
finally:
60+
l.close()
5561

5662

5763
class ProactorTests(test_utils.TestCase):

0 commit comments

Comments
 (0)