Skip to content

gh-152907: Restore cooked output flags around the input hook in the new REPL#153389

Open
harjothkhara wants to merge 1 commit into
python:mainfrom
harjothkhara:gh-152907-pyrepl-inputhook-opost
Open

gh-152907: Restore cooked output flags around the input hook in the new REPL#153389
harjothkhara wants to merge 1 commit into
python:mainfrom
harjothkhara:gh-152907-pyrepl-inputhook-opost

Conversation

@harjothkhara

@harjothkhara harjothkhara commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

The problem: While the new REPL waits for input, it puts the terminal in raw mode, which turns off the usual \n\r\n translation. It also runs input hooks during that wait — these are what GUI toolkits (Tkinter, Qt, …) use to keep their event loops alive at the prompt. So when a hook prints something, each line starts where the previous one ended instead of at the left margin, and the text "staircases" down the screen. The old readline REPL didn't do this, and PYTHON_BASIC_REPL=1 still works fine.

The fix: The REPL needs raw mode for drawing its own output, but the input hook runs at a moment when it isn't drawing. So this turns the newline translation back on just around the hook call, then returns to raw mode. Only the output flags are touched, so keystrokes are still not echoed at the prompt.

Checked with a pty that captures the exact bytes a hook writes:

before:  line1\nline2\nline3\n
after:   line1\r\nline2\r\nline3\r\n

There's a new test that fails without the fix, and ./python -m test test_pyrepl passes.

I used AI assistance for this and have reviewed the change.

Refs #152907

… the new REPL

pyrepl clears OPOST for its own cursor rendering but calls PyOS_InputHook
from inside the raw-mode read loop, so output written by an input hook
(GUI toolkit event loops, and any warning/traceback/print they emit) is
emitted with bare '\n' and no '\r'.  Restore the terminal's saved output
flags around the hook call and re-enter raw mode afterwards; only oflag is
toggled so ECHO/ICANON stay off at the prompt.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant