From 26d76162f9e91899bb68e88c34ccbe5a87980b7c Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Wed, 18 Nov 2026 16:37:46 -0800 Subject: [PATCH 1/4] gh-162197: Fix yank-pop tracking in PyREPL Failed `yank-pop` commands are still recorded as valid `yank-pop` operations. So the next `yank-pop` command attempts to replace text that had not been inserted. This causes a negative cursor position calculation (and hence a traceback). Instead, it should also report `previous command was not a yank` and return. --- Lib/_pyrepl/commands.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Lib/_pyrepl/commands.py b/Lib/_pyrepl/commands.py index a3b541525aee103..ef20b80a228767d 100644 --- a/Lib/_pyrepl/commands.py +++ b/Lib/_pyrepl/commands.py @@ -194,17 +194,20 @@ def do(self) -> None: if not r.kill_ring: r.error("nothing to yank") return + start = r.pos r.insert(r.kill_ring[-1]) - + r.yank_span = (start, r.pos) class yank_pop(YankCommand): def do(self) -> None: r = self.reader b = r.buffer if not r.kill_ring: + r.yank_span = None r.error("nothing to yank") return - if not is_yank(r.last_command): + if not is_yank(r.last_command) or r.yank_span is None: + r.yank_span = None r.error("previous command was not a yank") return repl = len(r.kill_ring[-1]) @@ -213,6 +216,7 @@ def do(self) -> None: start = r.pos - repl b[r.pos - repl : r.pos] = t r.pos = r.pos - repl + len(t) + r.yank_span = (start, r.pos) r.invalidate_buffer(start) From 83a838cfb9951c3a5662711fefc3d7d5af837aa5 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Wed, 18 Nov 2026 16:51:59 -0800 Subject: [PATCH 2/4] Add yank_span declaration to Reader --- Lib/_pyrepl/reader.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/_pyrepl/reader.py b/Lib/_pyrepl/reader.py index d552214f322d76d..4880d0e36b24663 100644 --- a/Lib/_pyrepl/reader.py +++ b/Lib/_pyrepl/reader.py @@ -308,6 +308,7 @@ class Reader: gen_colors: Callable[[str], Iterator[ColorSpan]] = gen_colors threading_hook: Callback | None = None invalidation: RefreshInvalidation = field(init=False) + yank_span: tuple[int, int] | None = None ## cached metadata to speed up screen refreshes @dataclass From ff1d2b05cd2220df0e100888e7524f26a816cfc6 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Wed, 18 Nov 2026 16:57:08 -0800 Subject: [PATCH 3/4] Add test --- Lib/test/test_pyrepl/test_reader.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_pyrepl/test_reader.py b/Lib/test/test_pyrepl/test_reader.py index fd9900d9a32b3d2..b78aa2dea304331 100644 --- a/Lib/test/test_pyrepl/test_reader.py +++ b/Lib/test/test_pyrepl/test_reader.py @@ -480,6 +480,7 @@ def _test_error_message( *, command: str | None = None, binding: str | None = None, + error_count: int = 1, **reader_kwargs, ): console = prepare_console(code_to_events(keys)) @@ -487,12 +488,12 @@ def _test_error_message( if command is not None: reader.bind(binding or keys, command) - while not reader.msg: + while console.beep.call_count < error_count: reader.handle1() self.assertEqual(reader.msg, f"! {expected}") self.assertEqual(reader.screen[-1], f"! {expected}") - console.beep.assert_called_once() + self.assertEqual(console.beep.call_count, error_count) def test_invalid_key_message(self): self._test_error_message("\x1bo", "no command is bound to this key") @@ -516,6 +517,13 @@ def test_yank_pop_after_other_command_message(self): "previous command was not a yank", ) + def test_yank_pop_after_failed_yank_pop_message(self): + self._test_error_message( + "x\x01\x0b\x1by\x1by", + "previous command was not a yank", + error_count=2, + ) + def test_start_of_history_message(self): self._test_error_message("\x10", "start of history list") From 5bcc727f17290bcebfa7ff1a6c5c09b9d40480f9 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 19 Nov 2026 01:04:41 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-11-19-01-04-39.gh-issue-162197.fc-GGH.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-19-01-04-39.gh-issue-162197.fc-GGH.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-19-01-04-39.gh-issue-162197.fc-GGH.rst b/Misc/NEWS.d/next/Library/2026-11-19-01-04-39.gh-issue-162197.fc-GGH.rst new file mode 100644 index 000000000000000..3bfd5006f7b5f0c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-19-01-04-39.gh-issue-162197.fc-GGH.rst @@ -0,0 +1 @@ +:mod:`_pyrepl`: Fix a crash when a failed ``yank-pop`` command is followed by another ``yank-pop`` command.