Repository navigation
Description
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
Activity
picnixz commentedon Aug 21, 2026
Does it reproduce with
PYTHON_BASIC_REPL=1environment variable?mancellin commentedon Aug 21, 2026
As for Python <3.13, the REPL returns a syntax error, because I guess bracketed paste was not supported.
picnixz commentedon Aug 21, 2026
Weird, so it's only 3.14+, but in 3.13 we introduced the new REPL. I wonder what changed. Could you maybe
git bisectthis? 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:
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
pablogsal commentedon Aug 22, 2026
I checked and in 3.14, bracketed paste is handled synchronously by
perform_bracketed_paste(). It repeatedly callsconsole.getpending()until\x1b[201~appears, butgetpending()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
\nis 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 asaccept. This explains the 3.13/3.14 difference and why adding a small sleep makes it work.pablogsal commentedon Aug 22, 2026
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 acrossgetpending()calls.Use pythongh-156186 instead of python#156186 in test comment
pythongh-156186: Join UTF-16 surrogate pairs on Windows
pythongh-156186: Test for Windows
Merge remote-tracking branch 'upstream/main' into pythongh-156186-bra…
Merge branch 'main' into pythongh-156186-bracketed-paste
pythongh-156186: Read again when a surrogate pair is split between reads
vasiliyk commentedon Nov 20, 2026
I opened #157270 implementing the fix described above: it splits
event.rawat 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?
pythongh-156186: Return queued input from getpending() before reading
Merge remote-tracking branch 'upstream/main' into pythongh-156186-bra…
pythongh-156186: Patch _winapi.SetConsoleMode in the two-paste test