Skip to content

_pyrepl: AssertionError in pos2xy after a failed M-y followed by M-y #162197

Open

Description

@CAESIUS-TIM

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

But 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_type

On 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] = t inserts at the wrong place
  • pos = 0 - 2 + 1 == -1
  • pos2xy() asserts 0 <= 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_command when 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

Linked PRs

Activity

  1. added
    type-bug
    topic-repl
    stdlib
    on Nov 18, 2026
  2. edvilme commented on Nov 18, 2026

    @edvilme
    Contributor

    Hi. If people are not planning to already, would love to take a look :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibtopic-repltype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions