Skip to content

Commit e9a5fc6

Browse files
committed
fix occasionally swalled keystrokes on Windows
1 parent 8cfd7b4 commit e9a5fc6

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

Lib/_pyrepl/windows_console.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ def getpending(self) -> Event:
566566
e.data += ch
567567
return e
568568

569-
def wait(self, timeout: float | None) -> bool:
569+
def wait_for_event(self, timeout: float | None) -> bool:
570570
"""Wait for an event."""
571571
if timeout is None:
572572
timeout = INFINITE
@@ -579,6 +579,15 @@ def wait(self, timeout: float | None) -> bool:
579579
return False
580580
return True
581581

582+
def wait(self, timeout: float | None) -> bool:
583+
"""
584+
Wait for events on the console.
585+
"""
586+
return (
587+
not self.event_queue.empty()
588+
or self.wait_for_event(timeout)
589+
)
590+
582591
def repaint(self) -> None:
583592
raise NotImplementedError("No repaint support")
584593

Lib/test/test_pyrepl/test_windows_console.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,32 @@ def test_up_vt(self):
576576
Event(evt='key', data='up', raw=bytearray(b'\x1b[A')))
577577
self.assertEqual(self.mock.call_count, 3)
578578

579+
# All tests above assume that there is always keyboard data to read,
580+
# because for simplicity we just use
581+
# self.console.wait = MagicMock(return_value=True)
582+
def test_wait_empty(self):
583+
console = WindowsConsole(encoding='utf-8')
584+
console.wait_for_event = MagicMock(return_value=True)
585+
self.assertTrue(console.event_queue.empty())
586+
timeout = 2.0
587+
self.assertTrue(console.wait(timeout))
588+
self.assertEqual(console.wait_for_event.call_count, 1)
589+
self.assertEqual(console.wait_for_event.mock_calls[0], call(timeout))
590+
591+
timeout = 1.1
592+
console.wait_for_event = MagicMock(return_value=False)
593+
self.assertFalse(console.wait(timeout))
594+
self.assertEqual(console.wait_for_event.call_count, 1)
595+
self.assertEqual(console.wait_for_event.mock_calls[0], call(timeout))
596+
597+
def test_wait_not_empty(self):
598+
console = WindowsConsole(encoding='utf-8')
599+
console.wait_for_event = MagicMock(return_value=True)
600+
console.event_queue.push(b"a")
601+
self.assertFalse(console.event_queue.empty())
602+
self.assertTrue(console.wait(0.0))
603+
self.assertEqual(console.wait_for_event.call_count, 0)
604+
579605

580606
if __name__ == "__main__":
581607
unittest.main()

0 commit comments

Comments
 (0)