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) 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 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") 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.