Skip to content

PyREPL bracketed paste issue #156186

Open
Open

Description

@mancellin

Bug report

Bug description:

As expected, when sending code to the REPL within bracketed paste, newlines are always interpreted as actual new lines and not as block execution.
But newlines sent just after closing the bracketed paste are interpreted also as newline in Python 3.14, while they should be interpreted as block execution.
If a small delay is added between closing the bracketed paste and sending the newline, the newline is actually interpreted as block execution.

Originally encountered with Neovim's chansend while sending a block of code to a REPL to execute it, but reproduced below in Python:

import os
import pty
import time
import signal

# Start a REPL
pid, fd = pty.fork()
if pid == 0:
    os.execlp("python", "python")
time.sleep(1)

# Send bracketed code and then newline
os.write(fd, b"\x1b[200~print('Help! I need somebody')\nhelp()\n\x1b[201~")
# time.sleep(0.01)
os.write(fd, b"\n")

# Display output
time.sleep(1)
data = os.read(fd, 4096)
os.kill(pid, signal.SIGTERM)
print(data)

On Python <=3.12, the script errors because bracketed paste is not supported.
On Python 3.13, the help is displayed as expected.
On Python 3.14 and 3.15, it ends up with a line continuation ...: and does not show the content of help(), unless there is a time.sleep between sending the bracketed code and the newline.

When sending to bash, the newlines after the closing of the paste are always interpreted as block execution.

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bug
    on Aug 21, 2026
  2. picnixz commented on Aug 21, 2026

    @picnixz
    Member

    Does it reproduce with PYTHON_BASIC_REPL=1 environment variable?

  3. added
    stdlib
    topic-repl
    on Aug 21, 2026
  4. mancellin commented on Aug 21, 2026

    @mancellin
    ContributorAuthor

    Does it reproduce with PYTHON_BASIC_REPL=1 environment variable?

    As for Python <3.13, the REPL returns a syntax error, because I guess bracketed paste was not supported.

  5. picnixz commented on Aug 21, 2026

    @picnixz
    Member

    Weird, so it's only 3.14+, but in 3.13 we introduced the new REPL. I wonder what changed. Could you maybe git bisect this? this will unfortuntely requires you to build the interpreter (or maybe not but I think you need to because the error might be in the os module or somewhere else).

    You say:

    If a small delay is added between closing the bracketed paste and sending the newline, the newline is actually interpreted as block execution.

    I remember we also had some "timeout" magic to detect some things in the REPL and act upon this kind of timeout (and I remember reviewing a PR about this) but I don't know whether this is the cause.

    cc @pablogsal

  6. added and removed on Aug 21, 2026
  7. pablogsal commented on Aug 22, 2026

    @pablogsal
    Member

    I checked and in 3.14, bracketed paste is handled synchronously by perform_bracketed_paste(). It repeatedly calls console.getpending() until \x1b[201~ appears, but getpending() can also return bytes following that delimiter. The implementation then removes the delimiter and inserts everything else into the editor buffer.

    Consequently, if the following \n is already available together with the closing bracketed-paste marker, it is treated as pasted text. If there is a delay, the read ends at the marker and the later newline goes through the normal event machinery as accept. This explains the 3.13/3.14 difference and why adding a small sleep makes it work.

  8. pablogsal commented on Aug 22, 2026

    @pablogsal
    Member

    The fix should split at the first closing marker, insert only the prefix, and feed the suffix back through the console/event queue so it is interpreted normally. Ideally this should operate on event.raw, both to preserve the bytes following the delimiter and to avoid decoding multibyte characters separately across getpending() calls.

  9. added a commit that references this issue on Sep 11, 2026
  10. added 5 commits that reference this issue on Nov 10, 2026
  11. vasiliyk commented on Nov 20, 2026

    @vasiliyk
    Contributor

    I opened #157270 implementing the fix described above: it splits event.raw at the first closing marker, inserts only the prefix, and feeds the rest back through the console event queue.

    It's a 3.14 regression. The reproducer above works on 3.13 and fails on 3.14 and 3.15, on both macOS and Linux.
    Could someone take a look?

  12. added 3 commits that reference this issue on Nov 26, 2026
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