Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Lib/_pyrepl/windows_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ def getpending(self) -> Event:
e.data += ch
return e

def wait(self, timeout: float | None) -> bool:
def wait_for_event(self, timeout: float | None) -> bool:
"""Wait for an event."""
if timeout is None:
timeout = INFINITE
Expand All @@ -579,6 +579,15 @@ def wait(self, timeout: float | None) -> bool:
return False
return True

def wait(self, timeout: float | None) -> bool:
Copy link
Member Author

@chris-eibl chris-eibl Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is easy and brings us in sync with unix_console.py

def wait(self, timeout: float | None = None) -> bool:
"""
Wait for events on the console.
"""
return (
not self.event_queue.empty()
or bool(self.pollob.poll(timeout))
)
.

"""
Wait for events on the console.
"""
return (
not self.event_queue.empty()
or self.wait_for_event(timeout)
)

def repaint(self) -> None:
raise NotImplementedError("No repaint support")

Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_pyrepl/test_windows_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,32 @@ def test_up_vt(self):
Event(evt='key', data='up', raw=bytearray(b'\x1b[A')))
self.assertEqual(self.mock.call_count, 3)

# All tests above assume that there is always keyboard data to read,
# because for simplicity we just use
# self.console.wait = MagicMock(return_value=True)
def test_wait_empty(self):
console = WindowsConsole(encoding='utf-8')
console.wait_for_event = MagicMock(return_value=True)
self.assertTrue(console.event_queue.empty())
timeout = 2.0
self.assertTrue(console.wait(timeout))
self.assertEqual(console.wait_for_event.call_count, 1)
self.assertEqual(console.wait_for_event.mock_calls[0], call(timeout))

timeout = 1.1
console.wait_for_event = MagicMock(return_value=False)
self.assertFalse(console.wait(timeout))
self.assertEqual(console.wait_for_event.call_count, 1)
self.assertEqual(console.wait_for_event.mock_calls[0], call(timeout))

def test_wait_not_empty(self):
console = WindowsConsole(encoding='utf-8')
console.wait_for_event = MagicMock(return_value=True)
console.event_queue.push(b"a")
self.assertFalse(console.event_queue.empty())
self.assertTrue(console.wait(0.0))
self.assertEqual(console.wait_for_event.call_count, 0)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Some keystrokes can be swallowed in the new ``PyREPL`` on Windows,
especially when used together with the ALT key. Fix by Chris Eibl.
Loading