Skip to content

Commit 73da836

Browse files
authored
[3.14] gh-159617: Fix PyREPL dropping the end of long wrapped lines (GH-159619)
1 parent 5430bc5 commit 73da836

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎Lib/_pyrepl/reader.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,16 @@ def calc_screen(self) -> list[str]:
353353
else:
354354
pre = prompt
355355
prelen = prompt_len
356-
for wrap in range(wrapcount + 1):
356+
while True:
357357
index_to_wrap_before = 0
358358
column = 0
359359
for char_width in char_widths:
360360
if column + char_width + prelen >= self.console.width:
361361
break
362362
index_to_wrap_before += 1
363363
column += char_width
364+
if index_to_wrap_before == 0 and not prelen:
365+
index_to_wrap_before = 1 # force progress
364366
if len(chars) > index_to_wrap_before:
365367
offset += index_to_wrap_before
366368
post = "\\"
@@ -378,6 +380,8 @@ def calc_screen(self) -> list[str]:
378380
char_widths = char_widths[index_to_wrap_before:]
379381
pre = ""
380382
prelen = 0
383+
if not post:
384+
break
381385
self.screeninfo = screeninfo
382386
self.cxy = self.pos2xy()
383387
if self.msg:

‎Lib/test/test_pyrepl/test_reader.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ def test_calc_screen_wrap_three_lines(self):
3535
reader, _ = handle_events_narrow_console(events)
3636
self.assert_screen_equal(reader, f"{9*"a"}\\\n{9*"a"}\\\naa")
3737

38+
def test_calc_screen_wrap_three_lines_last_char(self):
39+
events = code_to_events(19 * "a")
40+
reader, _ = handle_events_narrow_console(events)
41+
self.assert_screen_equal(reader, f"{9*"a"}\\\n{9*"a"}\\\na")
42+
3843
def test_calc_screen_prompt_handling(self):
3944
def prepare_reader_keep_prompts(*args, **kwargs):
4045
reader = prepare_reader(*args, **kwargs)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix the :term:`REPL` dropping the last characters of a long line that wraps
2+
across several rows, which could raise :exc:`IndexError` when moving the
3+
cursor with the arrow keys.

0 commit comments

Comments
 (0)