]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-139262: Prevent swallowing REPL input on Windows (GH-139263) (GH-143363)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 2 Jan 2026 15:44:01 +0000 (16:44 +0100)
committerGitHub <noreply@github.com>
Fri, 2 Jan 2026 15:44:01 +0000 (16:44 +0100)
(cherry picked from commit ef6f92a2a63fd83d62d9a431b2c3133a9a069ea0)

Co-authored-by: Chris Eibl <138194463+chris-eibl@users.noreply.github.com>
Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Lib/_pyrepl/windows_console.py
Lib/test/test_pyrepl/test_windows_console.py
Misc/NEWS.d/next/Library/2025-09-23-16-41-21.gh-issue-139262.RO0E98.rst [new file with mode: 0644]

index 6361e732dc1be5928f5e58ff3ac65cdf0fcd43bd..6d2a5cbb77405063589e5e4d70102dc6d9491da8 100644 (file)
@@ -527,7 +527,7 @@ class WindowsConsole(Console):
         processed."""
         return Event("key", "", b"")
 
-    def wait(self, timeout: float | None) -> bool:
+    def wait_for_event(self, timeout: float | None) -> bool:
         """Wait for an event."""
         # Poor man's Windows select loop
         start_time = time.time()
@@ -538,6 +538,15 @@ class WindowsConsole(Console):
                 return False
             time.sleep(0.01)
 
+    def wait(self, timeout: float | None) -> bool:
+        """
+        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")
 
index adf714d2ba46294c0dbaadc0d7da7f98f1bce764..bb727e5df4f324a7dccd261c31cc677b854c365c 100644 (file)
@@ -576,6 +576,32 @@ class WindowsConsoleGetEventTests(TestCase):
                          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()
diff --git a/Misc/NEWS.d/next/Library/2025-09-23-16-41-21.gh-issue-139262.RO0E98.rst b/Misc/NEWS.d/next/Library/2025-09-23-16-41-21.gh-issue-139262.RO0E98.rst
new file mode 100644 (file)
index 0000000..628fa0e
--- /dev/null
@@ -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.