Skip to content
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: 6 additions & 2 deletions Lib/_pyrepl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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)


Expand Down
1 change: 1 addition & 0 deletions Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions Lib/test/test_pyrepl/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,19 +480,20 @@ 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))
reader = prepare_reader(console, **reader_kwargs)
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")
Expand All @@ -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")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:mod:`_pyrepl`: Fix a crash when a failed ``yank-pop`` command is followed by another ``yank-pop`` command.
Loading