summaryrefslogtreecommitdiffstats
path: root/Lib/_pyrepl/windows_console.py
diff options
context:
space:
mode:
authorƁukasz Langa <lukasz@langa.pl>2025-05-02 18:22:31 (GMT)
committerGitHub <noreply@github.com>2025-05-02 18:22:31 (GMT)
commitfac41f56d4b6b858cb52b40529855cce85cdbdcc (patch)
tree70490d6d77240385c4ca99281c7e5333261e89dd /Lib/_pyrepl/windows_console.py
parentbfcbb28223b733b9cb88f152a059a9e1416f3467 (diff)
downloadcpython-fac41f56d4b6b858cb52b40529855cce85cdbdcc.zip
cpython-fac41f56d4b6b858cb52b40529855cce85cdbdcc.tar.gz
cpython-fac41f56d4b6b858cb52b40529855cce85cdbdcc.tar.bz2
gh-131507: Add support for syntax highlighting in PyREPL (GH-133247)
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Diffstat (limited to 'Lib/_pyrepl/windows_console.py')
-rw-r--r--Lib/_pyrepl/windows_console.py32
1 files changed, 31 insertions, 1 deletions
diff --git a/Lib/_pyrepl/windows_console.py b/Lib/_pyrepl/windows_console.py
index 17942c8..77985e5 100644
--- a/Lib/_pyrepl/windows_console.py
+++ b/Lib/_pyrepl/windows_console.py
@@ -426,6 +426,20 @@ class WindowsConsole(Console):
return rec
+ def _read_input_bulk(
+ self, block: bool, n: int
+ ) -> tuple[ctypes.Array[INPUT_RECORD], int]:
+ rec = (n * INPUT_RECORD)()
+ read = DWORD()
+
+ if not block and not self.wait(timeout=0):
+ return rec, 0
+
+ if not ReadConsoleInput(InHandle, rec, n, read):
+ raise WinError(GetLastError())
+
+ return rec, read.value
+
def get_event(self, block: bool = True) -> Event | None:
"""Return an Event instance. Returns None if |block| is false
and there is no event pending, otherwise waits for the
@@ -521,7 +535,23 @@ class WindowsConsole(Console):
def getpending(self) -> Event:
"""Return the characters that have been typed but not yet
processed."""
- return Event("key", "", b"")
+ e = Event("key", "", b"")
+
+ while not self.event_queue.empty():
+ e2 = self.event_queue.get()
+ if e2:
+ e.data += e2.data
+
+ recs, rec_count = self._read_input_bulk(False, 1024)
+ for i in range(rec_count):
+ rec = recs[i]
+ if rec and rec.EventType == KEY_EVENT:
+ key_event = rec.Event.KeyEvent
+ ch = key_event.uChar.UnicodeChar
+ if ch == "\r":
+ ch += "\n"
+ e.data += ch
+ return e
def wait(self, timeout: float | None) -> bool:
"""Wait for an event."""