Repository navigation
Description
Bug description
In the new REPL (_pyrepl), M-y (yank-pop, Alt-y / ESC y) reports
previous command was not a yank when the previous command was not a yank.
However, the failed command still leaves Reader.last_command set to
yank_pop. A second M-y therefore passes the is_yank() check even though
nothing was yanked. yank_pop then assumes that the last
len(kill_ring[-1]) characters before point are the text it inserted,
computes a negative pos, and the next redraw trips the assertion in
pos2xy().
Reproducer
On a fresh REPL (tested with 3.13.18, 3.14.10 and 3.15.2, no PYTHONSTARTUP,
no third-party packages):
a C-a C-k # kill ring: ["a"]
bb C-a C-k # kill ring: ["a", "bb"], cursor at 0
M-y # "previous command was not a yank"
M-y # AssertionError
C-a = Ctrl-A, C-k = Ctrl-K, M-y = Alt-y (or ESC then y).
Crash condition: at the second M-y, pos < len(kill_ring[-1]).
With the cursor at 0, the newest kill must be longer than the previous entry
(as above). If pos >= len(kill_ring[-1]), the assertion does not fire, but
yank_pop still replaces unrelated text before point and rotates the kill
ring.
With PYREPL_TRACE enabled, the log shows two consecutive
('yank-pop', ...) commands, then the assertion.
Traceback (3.15.2)
Traceback (most recent call last):
File ".../_pyrepl/readline.py", line 407, in multiline_input
return reader.readline()
File ".../_pyrepl/reader.py", line 1021, in readline
self.handle1()
File ".../_pyrepl/reader.py", line 1004, in handle1
self.do_cmd(cmd)
File ".../_pyrepl/reader.py", line 932, in do_cmd
self.update_screen()
File ".../_pyrepl/reader.py", line 889, in update_screen
self.refresh()
File ".../_pyrepl/reader.py", line 895, in refresh
base_screen = self.calc_screen()
File ".../_pyrepl/reader.py", line 456, in calc_screen
self.cxy = self.pos2xy()
File ".../_pyrepl/reader.py", line 766, in pos2xy
assert 0 <= self.pos <= len(self.buffer)
AssertionError
Expected behavior
The second M-y should also report previous command was not a yank and
leave the buffer, cursor and kill ring unchanged.
Actual behavior
pos becomes negative and the REPL prints the traceback above. The
exception is caught by simple_interact.py, so the session continues with a
cleared buffer.
Root cause
Lib/_pyrepl/commands.py::yank_pop correctly bails out:
if not is_yank(r.last_command):
r.error("previous command was not a yank")
returnBut Lib/_pyrepl/reader.py::do_cmd updates last_command unconditionally
after every non-digit_arg command, including commands that returned early
through their error path:
command.do()
...
self.update_screen()
if command_type is not commands.digit_arg:
self.last_command = command_typeOn the second M-y, with pos == 0 and kill_ring == [["a"], ["b", "b"]]:
repl = len(kill_ring[-1]) == 2- the ring is rotated;
t = ["a"] b[0 - 2 : 0] = tinserts at the wrong placepos = 0 - 2 + 1 == -1pos2xy()asserts0 <= pos <= len(buffer)
A real yank cannot hit this because yank advances pos by exactly the
length of the inserted entry, so pos >= repl; the bug requires the fake
yank state created by the failed M-y.
Suggested fix
yank_pop should not infer the previously yanked region from
kill_ring[-1]. Either:
- record the yank start/length explicitly (e.g. on
Reader), set it in
yank/yank_pop, and clear it on any other command; or - do not update
last_commandwhen a command reports an error / returns
without doing anything.
As a minimal crash guard, yank_pop can reject r.pos < repl and clamp
start = max(0, r.pos - repl), but that still leaves the silent
text-replacement case for r.pos >= repl.
CPython versions tested on
3.13.18, 3.14.10, 3.15.2 on Linux x86_64. On 2026-11-18, main still
contains the same yank_pop and do_cmd code, so this is not fixed there
either.
Operating systems tested on
Linux (x86_64), TERM=xterm-256color.
Related
- New REPL crashes on an empty DELETE_WORD_BACKWARDS (^W) followed by CLEAR_TO_START (^K) #131430 / gh-131430: Fix crashes on an empty DELETE_WORD_BACKWARDS (^W) followed by CLEAR_TO_START (^K) #131452: empty kill ring crash (
C-wthenC-k), same
family (a command that did nothing still updateslast_command), but a
different traceback. The proposedif is_kill(...) and r.kill_ring:guard
fixes thatIndexErroronly; it does not fix thisyank_popbug.
(C-uthenC-kon an empty line reproduces New REPL crashes on an empty DELETE_WORD_BACKWARDS (^W) followed by CLEAR_TO_START (^K) #131430 as well.) - _pyrepl: AssertionError in pos2xy when recalling long multi-line wide-character history #159459: a different
pos2xyassertion (wide characters in a narrow
terminal), fixed on 3.15 by the screen-layout refactor in gh-148105: _pyrepl: switch console refresh to structured rendered screens #146584; not
a duplicate of this one.
Activity
edvilme commentedon Nov 18, 2026
Hi. If people are not planning to already, would love to take a look :)