Skip to content

GH-130328: Speedup pasting in legacy console on Windows #133728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
8 changes: 7 additions & 1 deletion Lib/_pyrepl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,13 @@ def do(self) -> None:
r = self.reader
text = self.event * r.get_arg()
r.insert(text)
if r.paste_mode:
data = ""
ev = r.console.getpending()
data += ev.data
if data:
r.insert(data)
r.last_refresh_cache.invalidated = True


class insert_nl(EditCommand):
Expand Down Expand Up @@ -484,7 +491,6 @@ def do(self) -> None:
data = ""
start = time.time()
while done not in data:
self.reader.console.wait(100)
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think this is needed. Works for me without it. For both, Linux and Windows, this will immediately return in case of pending data, and just needlessly waits 100 ms if there is no more data.

ev = self.reader.console.getpending()
data += ev.data
trace(
Expand Down
26 changes: 15 additions & 11 deletions Lib/_pyrepl/windows_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,7 @@ def _getscrollbacksize(self) -> int:

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

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

def _read_input(self) -> INPUT_RECORD | None:
rec = INPUT_RECORD()
read = DWORD()
if not ReadConsoleInput(InHandle, rec, 1, read):
Expand All @@ -431,14 +428,10 @@ def _read_input(self, block: bool = True) -> INPUT_RECORD | None:
return rec

def _read_input_bulk(
self, block: bool, n: int
self, 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())

Expand All @@ -449,8 +442,11 @@ def get_event(self, block: bool = True) -> Event | None:
and there is no event pending, otherwise waits for the
completion of an event."""

if not block and not self.wait(timeout=0):
Copy link
Member Author

Choose a reason for hiding this comment

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

I have moved blocking and waiting logic into get_event like in unix_console.py. IMHO that shouldn't be part of reading and we're in perfect sync with Unix now.

return None

while self.event_queue.empty():
rec = self._read_input(block)
rec = self._read_input()
if rec is None:
return None

Expand Down Expand Up @@ -551,12 +547,20 @@ def getpending(self) -> Event:
if e2:
e.data += e2.data

recs, rec_count = self._read_input_bulk(False, 1024)
recs, rec_count = self._read_input_bulk(1024)
for i in range(rec_count):
rec = recs[i]
# In case of a legacy console, we do not only receive a keydown
Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, there is a lot of duplicated logic from get_event now. Maybe refactor that, so the logic can be reused here?

# event, but also a keyup event - and for uppercase letters
# an additional SHIFT_PRESSED event.
if rec and rec.EventType == KEY_EVENT:
key_event = rec.Event.KeyEvent
if not key_event.bKeyDown:
continue
ch = key_event.uChar.UnicodeChar
if ch == "\x00":
# ignore SHIFT_PRESSED and special keys
continue
if ch == "\r":
ch += "\n"
e.data += ch
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_pyrepl/test_unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
def unix_console(events, **kwargs):
console = UnixConsole()
console.get_event = MagicMock(side_effect=events)
console.getpending = MagicMock(return_value=Event("key", ""))

height = kwargs.get("height", 25)
width = kwargs.get("width", 80)
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_pyrepl/test_windows_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WindowsConsoleTests(TestCase):
def console(self, events, **kwargs) -> Console:
console = WindowsConsole()
console.get_event = MagicMock(side_effect=events)
console.getpending = MagicMock(return_value=Event("key", ""))
console.wait = MagicMock()
console._scroll = MagicMock()
console._hide_cursor = MagicMock()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speedup pasting in ``PyREPL`` on Windows in a legacy console. Patch by Chris
Eibl.
Loading