Skip to content
Closed
43 changes: 38 additions & 5 deletions Lib/_pyrepl/windows_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def __init__(self, err: int | None, descr: str | None = None) -> None:
if TYPE_CHECKING:
from typing import IO

INPUT_BUFFER_LEN = 10 * 1024

# Virtual-Key Codes: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
VK_MAP: dict[int, str] = {
0x23: "end", # VK_END
Expand Down Expand Up @@ -165,6 +167,10 @@ def __init__(
# Console I/O is redirected, fallback...
self.out = None

self.input_buffer = (INPUT_RECORD * INPUT_BUFFER_LEN)()
self.input_buffer_pos = 0
self.input_buffer_count = 0

def refresh(self, screen: list[str], c_xy: tuple[int, int]) -> None:
"""
Refresh the console screen.
Expand Down Expand Up @@ -415,16 +421,27 @@ def _getscrollbacksize(self) -> int:

return info.srWindow.Bottom # type: ignore[no-any-return]

def more_in_buffer(self) -> bool:
return self.input_buffer_pos < self.input_buffer_count - 1

def _read_input(self, block: bool = True) -> INPUT_RECORD | None:
if not block and not self.wait(timeout=0):
return None

rec = INPUT_RECORD()
if self.more_in_buffer():
self.input_buffer_pos += 1
return self.input_buffer[self.input_buffer_pos]

# read next chunk
self.input_buffer_pos = 0
self.input_buffer_count = 0
read = DWORD()
if not ReadConsoleInput(InHandle, rec, 1, read):

if not ReadConsoleInput(InHandle, self.input_buffer, INPUT_BUFFER_LEN, read):
raise WinError(GetLastError())

return rec
self.input_buffer_count = read.value
return self.input_buffer[0]

def get_event(self, block: bool = True) -> Event | None:
"""Return an Event instance. Returns None if |block| is false
Expand Down Expand Up @@ -521,9 +538,15 @@ def forgetinput(self) -> None:
def getpending(self) -> Event:
"""Return the characters that have been typed but not yet
processed."""
return Event("key", "", b"")
e = Event("key", "", b"")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've just copy-pasted this from

def getpending(self):
"""
Get pending events from the console event queue.
Returns:
- Event: Pending event from the event queue.
"""
e = Event("key", "", b"")
while not self.event_queue.empty():
e2 = self.event_queue.get()
e.data += e2.data
e.raw += e.raw
amount = 10000
raw = self.__read(amount)
data = str(raw, self.encoding, "replace")
e.data += data
e.raw += raw
return e

and dropped the reading of the next chunk, since IMHO this does not fit the doc string.

mypy fires here

Lib/_pyrepl/windows_console.py:545: error: Item "None" of "Event | None" has no
attribute "data"  [union-attr]
                e.data += e2.data
                          ^~~~~~~
Found 1 error in 1 file (checked 25 source files)

and is indeed correct. Have to dig in why we do not see this in case of unix_console.py ...


while not self.event_queue.empty():
e2 = self.event_queue.get()
e.data += e2.data

def wait(self, timeout: float | None) -> bool:
return e

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

def wait(self, timeout: float | None = None) -> bool:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Full in sync with unix_console.py now :)

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

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

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

Expand Down