From 6befa1828a50276f99220d6f9dc740b24ea7a4e1 Mon Sep 17 00:00:00 2001 From: Vasiliy Kiryanov Date: Thu, 10 Sep 2026 09:45:39 -0400 Subject: [PATCH 1/7] gh-156186: Fix PyREPL treating input after a bracketed paste as pasted text --- Lib/_pyrepl/commands.py | 14 +++++++++----- Lib/_pyrepl/windows_console.py | 5 ++++- Lib/test/test_pyrepl/test_pyrepl.py | 14 ++++++++++++++ .../2026-09-10-09-43-11.gh-issue-156186.yeOh4o.rst | 2 ++ 4 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-09-10-09-43-11.gh-issue-156186.yeOh4o.rst diff --git a/Lib/_pyrepl/commands.py b/Lib/_pyrepl/commands.py index cf8b1a6c1e4ec4d..ec712253672fe97 100644 --- a/Lib/_pyrepl/commands.py +++ b/Lib/_pyrepl/commands.py @@ -504,15 +504,19 @@ def do(self) -> None: class perform_bracketed_paste(Command): def do(self) -> None: - done = "\x1b[201~" - data = "" + done = b"\x1b[201~" + data = b"" start = time.time() while done not in data: ev = self.reader.console.getpending() - data += ev.data + data += ev.raw trace( - "bracketed pasting of {l} chars done in {s:.2f}s", + "bracketed pasting of {l} bytes done in {s:.2f}s", l=len(data), s=time.time() - start, ) - self.reader.insert(data.replace(done, "")) + pasted, _, rest = data.partition(done) + self.reader.insert(pasted.decode(self.reader.console.encoding, "replace")) + + for byte in rest: + self.reader.console.push_char(byte) diff --git a/Lib/_pyrepl/windows_console.py b/Lib/_pyrepl/windows_console.py index 65eacf90fde685d..bbd9b48e21116f4 100644 --- a/Lib/_pyrepl/windows_console.py +++ b/Lib/_pyrepl/windows_console.py @@ -627,7 +627,8 @@ def push_char(self, char: int | bytes) -> None: """ Push a character to the console event queue. """ - raise NotImplementedError("push_char not supported on Windows") + trace("push char {char!r}", char=char) + self.event_queue.push(char) def beep(self) -> None: self.__write("\x07") @@ -671,6 +672,7 @@ def getpending(self) -> Event: e2 = self.event_queue.get() if e2: e.data += e2.data + e.raw += e2.raw recs, rec_count = self._read_input_bulk(1024) for i in range(rec_count): @@ -689,6 +691,7 @@ def getpending(self) -> Event: if ch == "\r": ch = "\n" e.data += ch + e.raw += ch.encode(self.event_queue.encoding, "replace") return e def wait_for_event(self, timeout: float | None) -> bool: diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index 7cc178f19df6f9d..f5d131aa3e63e55 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -2202,6 +2202,20 @@ def test_python_basic_repl(self): self.assertNotIn("Exception", output) self.assertNotIn("Traceback", output) + def test_bracketed_paste_newline_after_end_marker(self): + # A newline arriving in the same read as the closing marker must + # execute the pasted block instead of being inserted as text. + # See #156186. + env = os.environ.copy() + commands = ("\x1b[200~x = 1\nprint(f'^{x=}')\n\x1b[201~" + "\n" + "exit()\n") + output, exit_code = self.run_repl(commands, env=env, skip=True) + self.assertEqual(exit_code, 0) + self.assertIn("^x=1", output) + self.assertNotIn("Exception", output) + self.assertNotIn("Traceback", output) + @force_not_colorized def test_no_pyrepl_source_in_exc(self): # Avoid using _pyrepl/__main__.py in traceback reports diff --git a/Misc/NEWS.d/next/Library/2026-09-10-09-43-11.gh-issue-156186.yeOh4o.rst b/Misc/NEWS.d/next/Library/2026-09-10-09-43-11.gh-issue-156186.yeOh4o.rst new file mode 100644 index 000000000000000..3df5568b444ee03 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-10-09-43-11.gh-issue-156186.yeOh4o.rst @@ -0,0 +1,2 @@ +Fix PyREPL so that a newline sent right after a pasted block runs the block, +instead of being added to the pasted text. From 006f06f25d8fa40e9640ee7164e70cda1c3d9486 Mon Sep 17 00:00:00 2001 From: Vasiliy Kiryanov Date: Fri, 11 Sep 2026 08:54:25 -0400 Subject: [PATCH 2/7] Use gh-156186 instead of #156186 in test comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maurycy Pawłowski-Wieroński --- Lib/test/test_pyrepl/test_pyrepl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index f5d131aa3e63e55..bcfc76b52f39c6e 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -2205,7 +2205,7 @@ def test_python_basic_repl(self): def test_bracketed_paste_newline_after_end_marker(self): # A newline arriving in the same read as the closing marker must # execute the pasted block instead of being inserted as text. - # See #156186. + # See gh-156186. env = os.environ.copy() commands = ("\x1b[200~x = 1\nprint(f'^{x=}')\n\x1b[201~" "\n" From 9ff5507b89f7c2979e0644376489f167b85512ea Mon Sep 17 00:00:00 2001 From: Vasiliy Kiryanov Date: Tue, 10 Nov 2026 10:45:58 -0500 Subject: [PATCH 3/7] gh-156186: Join UTF-16 surrogate pairs on Windows --- Lib/_pyrepl/windows_console.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Lib/_pyrepl/windows_console.py b/Lib/_pyrepl/windows_console.py index bbd9b48e21116f4..076019cb32405f2 100644 --- a/Lib/_pyrepl/windows_console.py +++ b/Lib/_pyrepl/windows_console.py @@ -674,6 +674,7 @@ def getpending(self) -> Event: e.data += e2.data e.raw += e2.raw + text = "" recs, rec_count = self._read_input_bulk(1024) for i in range(rec_count): rec = recs[i] @@ -690,8 +691,13 @@ def getpending(self) -> Event: continue if ch == "\r": ch = "\n" - e.data += ch - e.raw += ch.encode(self.event_queue.encoding, "replace") + text += ch + # Each record on Windows holds a UTF-16 code unit. + # A surrogate pair must be joined before encoding, + # an unpaired half becomes U+FFFD. + text = text.encode("utf-16-le", "surrogatepass").decode("utf-16-le", "replace") + e.data += text + e.raw += text.encode(self.event_queue.encoding, "replace") return e def wait_for_event(self, timeout: float | None) -> bool: From 93154c5426cd593f8d7399496c8f5c34485f7db9 Mon Sep 17 00:00:00 2001 From: Vasiliy Kiryanov Date: Tue, 10 Nov 2026 10:46:26 -0500 Subject: [PATCH 4/7] gh-156186: Test for Windows --- Lib/test/test_pyrepl/test_windows_console.py | 25 ++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Lib/test/test_pyrepl/test_windows_console.py b/Lib/test/test_pyrepl/test_windows_console.py index 32c4255aa6b1904..7b6996bb3304934 100644 --- a/Lib/test/test_pyrepl/test_windows_console.py +++ b/Lib/test/test_pyrepl/test_windows_console.py @@ -436,6 +436,14 @@ def get_event(self, input_records, **kwargs) -> Console: event = self.console.get_event(block=False) return event + def get_pending(self, input_records) -> Event: + self.console = WindowsConsole(encoding='utf-8') + recs = (len(input_records) * wc.INPUT_RECORD)(*input_records) + self.mock = MagicMock(return_value=(recs, len(input_records))) + self.console._read_input_bulk = self.mock + event = self.console.getpending() + return event + def get_input_record(self, unicode_char, vcode=0, control=0): return wc.INPUT_RECORD( wc.KEY_EVENT, @@ -648,6 +656,23 @@ def test_wait_not_empty(self): self.assertTrue(console.wait(0.0)) self.assertEqual(console.wait_for_event.call_count, 0) + def test_getpending_raw(self): + ir_list = [ + self.get_input_record("a"), + self.get_input_record("\ud83d"), + self.get_input_record("\ude00"), + self.get_input_record("\r") + ] + event = self.get_pending(ir_list) + self.assertEqual(event.data, "a😀\n") + self.assertEqual(event.raw, b"a\xf0\x9f\x98\x80\n") + + def test_push_char(self): + self.console = WindowsConsole(encoding='utf-8') + self.console.push_char(ord("\n")) + event = self.console.get_event(block=False) + self.assertEqual(event.data, "\n") + if __name__ == "__main__": unittest.main() From cc5b5dbee528753f4e9362d9a25a6a8af15baa32 Mon Sep 17 00:00:00 2001 From: Vasiliy Kiryanov Date: Sun, 15 Nov 2026 19:48:58 -0500 Subject: [PATCH 5/7] gh-156186: Read again when a surrogate pair is split between reads --- Lib/_pyrepl/windows_console.py | 38 +++++++++++--------- Lib/test/test_pyrepl/test_windows_console.py | 23 ++++++++++-- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/Lib/_pyrepl/windows_console.py b/Lib/_pyrepl/windows_console.py index c6de72fa8dc373e..964843a2ab63aa1 100644 --- a/Lib/_pyrepl/windows_console.py +++ b/Lib/_pyrepl/windows_console.py @@ -677,23 +677,27 @@ def getpending(self) -> Event: e.raw += e2.raw text = "" - 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 - # 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" - text += ch + while True: + 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 + # 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" + text += ch + # Read again while the text ends with a high surrogate. + if not "\ud800" <= text[-1:] <= "\udbff": + break # Each record on Windows holds a UTF-16 code unit. # A surrogate pair must be joined before encoding, # an unpaired half becomes U+FFFD. diff --git a/Lib/test/test_pyrepl/test_windows_console.py b/Lib/test/test_pyrepl/test_windows_console.py index 7b6996bb3304934..df778beb22e6196 100644 --- a/Lib/test/test_pyrepl/test_windows_console.py +++ b/Lib/test/test_pyrepl/test_windows_console.py @@ -436,10 +436,13 @@ def get_event(self, input_records, **kwargs) -> Console: event = self.console.get_event(block=False) return event - def get_pending(self, input_records) -> Event: + def get_pending(self, *reads) -> Event: self.console = WindowsConsole(encoding='utf-8') - recs = (len(input_records) * wc.INPUT_RECORD)(*input_records) - self.mock = MagicMock(return_value=(recs, len(input_records))) + recs = [] + for read in reads: + rec = (len(read) * wc.INPUT_RECORD)(*read) + recs.append((rec, len(rec))) + self.mock = MagicMock(side_effect=recs) self.console._read_input_bulk = self.mock event = self.console.getpending() return event @@ -667,6 +670,20 @@ def test_getpending_raw(self): self.assertEqual(event.data, "a😀\n") self.assertEqual(event.raw, b"a\xf0\x9f\x98\x80\n") + def test_getpending_split_surrogate_pair(self): + ir_list_one = [ + self.get_input_record("a"), + self.get_input_record("\ud83d"), + ] + ir_list_two = [ + self.get_input_record("\ude00"), + self.get_input_record("\r") + ] + event = self.get_pending(ir_list_one, ir_list_two) + self.assertEqual(event.data, "a😀\n") + self.assertEqual(event.raw, b"a\xf0\x9f\x98\x80\n") + self.assertEqual(self.mock.call_count, 2) + def test_push_char(self): self.console = WindowsConsole(encoding='utf-8') self.console.push_char(ord("\n")) From e6cf8808312760ca8ece00b93f51cf3203d8824b Mon Sep 17 00:00:00 2001 From: Vasiliy Kiryanov Date: Thu, 26 Nov 2026 13:11:12 -0500 Subject: [PATCH 6/7] gh-156186: Return queued input from getpending() before reading --- Lib/_pyrepl/windows_console.py | 5 +++ Lib/test/test_pyrepl/test_windows_console.py | 32 ++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/Lib/_pyrepl/windows_console.py b/Lib/_pyrepl/windows_console.py index 964843a2ab63aa1..f90fb05e48eec2a 100644 --- a/Lib/_pyrepl/windows_console.py +++ b/Lib/_pyrepl/windows_console.py @@ -676,6 +676,11 @@ def getpending(self) -> Event: e.data += e2.data e.raw += e2.raw + # _read_input_bulk() blocks until the console has input, + # so return the queued input without reading. + if e.raw: + return e + text = "" while True: recs, rec_count = self._read_input_bulk(1024) diff --git a/Lib/test/test_pyrepl/test_windows_console.py b/Lib/test/test_pyrepl/test_windows_console.py index df778beb22e6196..af49ab58dff8bbd 100644 --- a/Lib/test/test_pyrepl/test_windows_console.py +++ b/Lib/test/test_pyrepl/test_windows_console.py @@ -690,6 +690,38 @@ def test_push_char(self): event = self.console.get_event(block=False) self.assertEqual(event.data, "\n") + def test_bracketed_paste_two_pastes_in_one_read(self): + # Two complete pastes arrive in a single console read. The second + # one is already queued, so it must be submitted without another + # read, which would block on a real console. + # See gh-156186. + + self.console = WindowsConsole(encoding='utf-8') + self.console._WindowsConsole__vt_support = True + self.console._WindowsConsole__original_input_mode = 0 + self.console.wait = MagicMock(return_value=True) + self.console._hide_cursor = MagicMock() + self.console._show_cursor = MagicMock() + self.console.out = MagicMock() + self.console.getheightwidth = MagicMock(return_value=(25, 80)) + + # start marker + start_marker_records = [self.get_input_record(x) for x in "\x1b[200~"] + + # body 1, end marker \x1b[201~, \r, start marker \x1b[200~, body 2, end marker, \r + bulk_records = [self.get_input_record(x) for x in "a\x1b[201~\r\x1b[200~b\x1b[201~\r"] + bulk_array = (len(bulk_records) * wc.INPUT_RECORD)(*bulk_records) + bulk_reads = [(bulk_array, len(bulk_array))] + + self.console._read_input = MagicMock(side_effect=start_marker_records) + self.console._read_input_bulk = MagicMock(side_effect=bulk_reads) + + reader = default_prepare_reader(self.console, paste_mode=False) + with patch.object(wc, "SetConsoleMode", return_value=1): + self.assertEqual(reader.readline(), "a") + self.assertEqual(reader.readline(), "b") + self.assertEqual(self.console._read_input_bulk.call_count, 1) + if __name__ == "__main__": unittest.main() From c35e24e1f7593351e5092032b0daee688d9745fe Mon Sep 17 00:00:00 2001 From: Vasiliy Kiryanov Date: Thu, 26 Nov 2026 13:45:35 -0500 Subject: [PATCH 7/7] gh-156186: Patch _winapi.SetConsoleMode in the two-paste test --- Lib/test/test_pyrepl/test_windows_console.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_pyrepl/test_windows_console.py b/Lib/test/test_pyrepl/test_windows_console.py index cf33e6c1cebc8fc..194a4406d0f2a67 100644 --- a/Lib/test/test_pyrepl/test_windows_console.py +++ b/Lib/test/test_pyrepl/test_windows_console.py @@ -717,7 +717,7 @@ def test_bracketed_paste_two_pastes_in_one_read(self): self.console._read_input_bulk = MagicMock(side_effect=bulk_reads) reader = default_prepare_reader(self.console, paste_mode=False) - with patch.object(wc, "SetConsoleMode", return_value=1): + with patch.object(wc._winapi, "SetConsoleMode"): self.assertEqual(reader.readline(), "a") self.assertEqual(reader.readline(), "b") self.assertEqual(self.console._read_input_bulk.call_count, 1)