Skip to content

Commit e6cf880

Browse files
committed
gh-156186: Return queued input from getpending() before reading
1 parent cc5b5db commit e6cf880

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎Lib/_pyrepl/windows_console.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,11 @@ def getpending(self) -> Event:
676676
e.data += e2.data
677677
e.raw += e2.raw
678678

679+
# _read_input_bulk() blocks until the console has input,
680+
# so return the queued input without reading.
681+
if e.raw:
682+
return e
683+
679684
text = ""
680685
while True:
681686
recs, rec_count = self._read_input_bulk(1024)

‎Lib/test/test_pyrepl/test_windows_console.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,38 @@ def test_push_char(self):
690690
event = self.console.get_event(block=False)
691691
self.assertEqual(event.data, "\n")
692692

693+
def test_bracketed_paste_two_pastes_in_one_read(self):
694+
# Two complete pastes arrive in a single console read. The second
695+
# one is already queued, so it must be submitted without another
696+
# read, which would block on a real console.
697+
# See gh-156186.
698+
699+
self.console = WindowsConsole(encoding='utf-8')
700+
self.console._WindowsConsole__vt_support = True
701+
self.console._WindowsConsole__original_input_mode = 0
702+
self.console.wait = MagicMock(return_value=True)
703+
self.console._hide_cursor = MagicMock()
704+
self.console._show_cursor = MagicMock()
705+
self.console.out = MagicMock()
706+
self.console.getheightwidth = MagicMock(return_value=(25, 80))
707+
708+
# start marker
709+
start_marker_records = [self.get_input_record(x) for x in "\x1b[200~"]
710+
711+
# body 1, end marker \x1b[201~, \r, start marker \x1b[200~, body 2, end marker, \r
712+
bulk_records = [self.get_input_record(x) for x in "a\x1b[201~\r\x1b[200~b\x1b[201~\r"]
713+
bulk_array = (len(bulk_records) * wc.INPUT_RECORD)(*bulk_records)
714+
bulk_reads = [(bulk_array, len(bulk_array))]
715+
716+
self.console._read_input = MagicMock(side_effect=start_marker_records)
717+
self.console._read_input_bulk = MagicMock(side_effect=bulk_reads)
718+
719+
reader = default_prepare_reader(self.console, paste_mode=False)
720+
with patch.object(wc, "SetConsoleMode", return_value=1):
721+
self.assertEqual(reader.readline(), "a")
722+
self.assertEqual(reader.readline(), "b")
723+
self.assertEqual(self.console._read_input_bulk.call_count, 1)
724+
693725

694726
if __name__ == "__main__":
695727
unittest.main()

0 commit comments

Comments
 (0)