From 31bc36dbd33ee2a98c08f297009d862bd29b62ad Mon Sep 17 00:00:00 2001 From: lazerg Date: Sun, 18 Oct 2026 08:47:25 +0500 Subject: [PATCH 1/3] gh-159658: Fix the REPL mangling input when the prompt is wider than the terminal --- Lib/_pyrepl/layout.py | 2 +- Lib/test/test_pyrepl/test_layout.py | 21 +++++++++++++++---- ...-10-18-03-47-18.gh-issue-159658.7vC6hv.rst | 2 ++ 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-10-18-03-47-18.gh-issue-159658.7vC6hv.rst diff --git a/Lib/_pyrepl/layout.py b/Lib/_pyrepl/layout.py index cff0821cb95f72..74a326d4b6ffda 100644 --- a/Lib/_pyrepl/layout.py +++ b/Lib/_pyrepl/layout.py @@ -215,7 +215,7 @@ def layout_content_lines( index_to_wrap_before += 1 column += char_width - if index_to_wrap_before == 0 and start < total: + if index_to_wrap_before == 0 and not current_prompt_width: index_to_wrap_before = 1 # force progress at_line_end = (start + index_to_wrap_before) >= total diff --git a/Lib/test/test_pyrepl/test_layout.py b/Lib/test/test_pyrepl/test_layout.py index 6825659b772d21..e6ead695d553db 100644 --- a/Lib/test/test_pyrepl/test_layout.py +++ b/Lib/test/test_pyrepl/test_layout.py @@ -229,17 +229,30 @@ def test_line_wraps(self): def test_wrapping_forces_progress(self): # When a single character is wider than available space, force 1 char - prompt = _prompt("P", 1) - body = (ContentFragment("W", 1),) + prompt = _prompt("", 0) + body = (ContentFragment("W", 2),) source = _source("W", has_newline=False) line = ContentLine(source, prompt, body) - # width=2 means prompt(1) + char(1) = 2, which fits (< width would be - # false for width=2 since 1+1 >= 2), so it wraps but forces progress + # width=2 means char(2) >= width, so it wraps but forces progress result = layout_content_lines((line,), 2, 0) self.assertEqual(len(result.wrapped_rows), 1) self.assertEqual(result.wrapped_rows[0].buffer_advance, 1) + def test_prompt_wider_than_terminal(self): + line = _content_line("Hello", prompt=_prompt("a" * 65, 65)) + result = layout_content_lines((line,), 20, 0) + + self.assertEqual(len(result.wrapped_rows), 2) + first, second = result.wrapped_rows + self.assertEqual(first.prompt_width, 65) + self.assertEqual(first.fragments, ()) + self.assertEqual(first.suffix, "\\") + self.assertEqual(first.buffer_advance, 0) + self.assertEqual(second.prompt_width, 0) + self.assertEqual(second.buffer_advance, 6) + self.assertEqual(result.layout_map.pos_to_xy(0), (0, 1)) + def test_layout_map_matches_wrapped_rows(self): line = _content_line("abc") result = layout_content_lines((line,), 80, 0) diff --git a/Misc/NEWS.d/next/Library/2026-10-18-03-47-18.gh-issue-159658.7vC6hv.rst b/Misc/NEWS.d/next/Library/2026-10-18-03-47-18.gh-issue-159658.7vC6hv.rst new file mode 100644 index 00000000000000..3b3952d483700e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-18-03-47-18.gh-issue-159658.7vC6hv.rst @@ -0,0 +1,2 @@ +Fix the :term:`REPL` mangling typed input when :data:`sys.ps1` is wider than +the terminal. From f3cd8f80d3af4fd415fe5eec2ef374bafb5704c2 Mon Sep 17 00:00:00 2001 From: lazerg Date: Sun, 18 Oct 2026 10:21:38 +0500 Subject: [PATCH 2/3] Skip prompt-only rows when moving the cursor up --- Lib/_pyrepl/commands.py | 6 ++++++ Lib/test/test_pyrepl/test_reader.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/Lib/_pyrepl/commands.py b/Lib/_pyrepl/commands.py index cf8b1a6c1e4ec4..cd52d96de1112f 100644 --- a/Lib/_pyrepl/commands.py +++ b/Lib/_pyrepl/commands.py @@ -265,6 +265,12 @@ def do(self) -> None: r.error("start of buffer") return + # Skip prompt-only rows, they hold no buffer positions + row = r.layout.rows[new_y] + while new_y > 0 and not row.char_widths and not row.buffer_advance: + new_y -= 1 + row = r.layout.rows[new_y] + if ( x > ( diff --git a/Lib/test/test_pyrepl/test_reader.py b/Lib/test/test_pyrepl/test_reader.py index 0b32ead357c2c0..57bb56dfead949 100644 --- a/Lib/test/test_pyrepl/test_reader.py +++ b/Lib/test/test_pyrepl/test_reader.py @@ -236,6 +236,22 @@ def test_up_arrow_after_ctrl_r(self): reader, _ = handle_all_events(events) self.assertIn(reader.screen, ([], [""])) + def test_up_arrow_with_prompt_wider_than_terminal(self): + events = itertools.chain( + code_to_events("x\nabc"), + [Event(evt="key", data="up", raw=bytearray(b"\x1bOA"))], + ) + reader, _ = handle_all_events( + events, + prepare_console=functools.partial(prepare_console, width=20), + prepare_reader=functools.partial( + prepare_reader, get_prompt=lambda lineno, cursor_on_line: "a" * 30 + ), + ) + # the prompt-only row above "abc" is skipped, landing on "x" + self.assertEqual(reader.pos, 1) + self.assertEqual(reader.cxy, (1, 1)) + def test_newline_within_block_trailing_whitespace(self): # fmt: off code = ( From b48095933da12447a6540e42a4d36862413bd912 Mon Sep 17 00:00:00 2001 From: lazerg Date: Fri, 27 Nov 2026 08:04:42 +0500 Subject: [PATCH 3/3] Split wide prompts into terminal rows --- Lib/_pyrepl/commands.py | 6 +++ Lib/_pyrepl/layout.py | 62 ++++++++++++++++++++++++++--- Lib/test/test_pyrepl/test_layout.py | 38 +++++++++++++----- Lib/test/test_pyrepl/test_reader.py | 24 ++++++++++- 4 files changed, 114 insertions(+), 16 deletions(-) diff --git a/Lib/_pyrepl/commands.py b/Lib/_pyrepl/commands.py index 558fabe7b86802..530f9a73519b65 100644 --- a/Lib/_pyrepl/commands.py +++ b/Lib/_pyrepl/commands.py @@ -303,6 +303,12 @@ def do(self) -> None: r.debug("end of buffer") return + # Skip prompt-only rows, they hold no buffer positions + row = r.layout.rows[new_y] + while new_y < r.layout.max_row() and not row.char_widths and not row.buffer_advance: + new_y += 1 + row = r.layout.rows[new_y] + if ( x > ( diff --git a/Lib/_pyrepl/layout.py b/Lib/_pyrepl/layout.py index 7cbd582bf9fb38..ddc7f5c2fab7a4 100644 --- a/Lib/_pyrepl/layout.py +++ b/Lib/_pyrepl/layout.py @@ -4,6 +4,7 @@ from typing import Self from .content import ContentFragment, ContentLine +from .utils import ANSI_ESCAPE_SEQUENCE, wlen lazy from .types import CursorXY, ScreenInfoRow @@ -157,6 +158,35 @@ def wrapped_row_end( return end if end > start else start + 1 +def split_prompt(text: str, width: int) -> list[tuple[str, int]]: + """Split *text* into rows of at most *width* visible columns. + + Every row but the last is a full terminal row; the last one holds the + rest of the prompt and may be empty. + """ + rows: list[tuple[str, int]] = [] + row_start = 0 + column = 0 + index = 0 + while index < len(text): + if match := ANSI_ESCAPE_SEQUENCE.match(text, index): + index = match.end() + continue + char_width = wlen(text[index]) + if column + char_width > width: + rows.append((text[row_start:index], column)) + row_start = index + column = 0 + column += char_width + index += 1 + if column == width: + rows.append((text[row_start:index], column)) + row_start = index + column = 0 + rows.append((text[row_start:], column)) + return rows + + def layout_content_lines( lines: tuple[ContentLine, ...], width: int, @@ -192,6 +222,17 @@ def layout_content_lines( prompt_text = line.prompt.text prompt_width = line.prompt.width + if prompt_width >= width: + *prompt_rows, (prompt_text, prompt_width) = split_prompt( + prompt_text, width + ) + for row_text, row_width in prompt_rows: + line_end_offsets.append(offset) + wrapped_rows.append( + WrappedRow(prompt_text=row_text, prompt_width=row_width) + ) + layout_rows.append(LayoutRow(row_width, ())) + body = tuple(line.body) body_widths = tuple(fragment.width for fragment in body) @@ -223,12 +264,23 @@ def layout_content_lines( start = 0 total = len(body) while True: + available_width = width - current_prompt_width - 1 + if current_prompt_width and body_widths[start] > available_width: + # No room left after the prompt, start the body on a new row. + line_end_offsets.append(offset) + wrapped_rows.append( + WrappedRow( + prompt_text=current_prompt, + prompt_width=current_prompt_width, + ) + ) + layout_rows.append(LayoutRow(current_prompt_width, ())) + current_prompt = "" + current_prompt_width = 0 + continue + # Find how many characters fit on this row. - end = wrapped_row_end( - body_widths, - start, - width - current_prompt_width - 1, - ) + end = wrapped_row_end(body_widths, start, available_width) index_to_wrap_before = end - start at_line_end = (start + index_to_wrap_before) >= total diff --git a/Lib/test/test_pyrepl/test_layout.py b/Lib/test/test_pyrepl/test_layout.py index e6ead695d553db..9f053e6fa0f264 100644 --- a/Lib/test/test_pyrepl/test_layout.py +++ b/Lib/test/test_pyrepl/test_layout.py @@ -243,15 +243,35 @@ def test_prompt_wider_than_terminal(self): line = _content_line("Hello", prompt=_prompt("a" * 65, 65)) result = layout_content_lines((line,), 20, 0) - self.assertEqual(len(result.wrapped_rows), 2) - first, second = result.wrapped_rows - self.assertEqual(first.prompt_width, 65) - self.assertEqual(first.fragments, ()) - self.assertEqual(first.suffix, "\\") - self.assertEqual(first.buffer_advance, 0) - self.assertEqual(second.prompt_width, 0) - self.assertEqual(second.buffer_advance, 6) - self.assertEqual(result.layout_map.pos_to_xy(0), (0, 1)) + rows = result.wrapped_rows + self.assertEqual( + [row.prompt_width for row in rows], [20, 20, 20, 5] + ) + self.assertEqual([row.suffix for row in rows], ["", "", "", ""]) + self.assertEqual( + [row.buffer_advance for row in rows], [0, 0, 0, 6] + ) + self.assertTrue(all(row.width <= 20 for row in result.layout_map.rows)) + self.assertEqual(result.layout_map.pos_to_xy(0), (5, 3)) + self.assertEqual(result.layout_map.pos_to_xy(5), (10, 3)) + + def test_prompt_wider_than_terminal_empty_body(self): + line = _content_line("", prompt=_prompt("a" * 40, 40)) + result = layout_content_lines((line,), 20, 0) + + self.assertEqual( + [row.prompt_width for row in result.wrapped_rows], [20, 20, 0] + ) + self.assertEqual(result.layout_map.pos_to_xy(0), (0, 2)) + + def test_prompt_filling_last_row(self): + line = _content_line("Hello", prompt=_prompt("a" * 39, 39)) + result = layout_content_lines((line,), 20, 0) + + rows = result.wrapped_rows + self.assertEqual([row.prompt_width for row in rows], [20, 19, 0]) + self.assertEqual([row.suffix for row in rows], ["", "", ""]) + self.assertEqual(result.layout_map.pos_to_xy(0), (0, 2)) def test_layout_map_matches_wrapped_rows(self): line = _content_line("abc") diff --git a/Lib/test/test_pyrepl/test_reader.py b/Lib/test/test_pyrepl/test_reader.py index d72743db7a5435..f997bf9117acb2 100644 --- a/Lib/test/test_pyrepl/test_reader.py +++ b/Lib/test/test_pyrepl/test_reader.py @@ -274,9 +274,29 @@ def test_up_arrow_with_prompt_wider_than_terminal(self): prepare_reader, get_prompt=lambda lineno, cursor_on_line: "a" * 30 ), ) - # the prompt-only row above "abc" is skipped, landing on "x" + # the prompt rows above "abc" are skipped, landing on "x" self.assertEqual(reader.pos, 1) - self.assertEqual(reader.cxy, (1, 1)) + self.assertEqual(reader.cxy, (11, 1)) + + def test_down_arrow_with_prompt_wider_than_terminal(self): + events = itertools.chain( + code_to_events("xy\nabc"), + [ + Event(evt="key", data="up", raw=bytearray(b"\x1bOA")), + Event(evt="key", data="left", raw=bytearray(b"\x1bOD")), + Event(evt="key", data="down", raw=bytearray(b"\x1bOB")), + ], + ) + reader, _ = handle_all_events( + events, + prepare_console=functools.partial(prepare_console, width=20), + prepare_reader=functools.partial( + prepare_reader, get_prompt=lambda lineno, cursor_on_line: "a" * 30 + ), + ) + # the prompt rows above "abc" are skipped, keeping the column + self.assertEqual(reader.pos, 4) + self.assertEqual(reader.cxy, (11, 3)) def test_newline_within_block_trailing_whitespace(self): # fmt: off