Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,12 @@ def calc_screen(self) -> list[str]:
if self.last_refresh_cache.valid(self):
offset, num_common_lines = self.last_refresh_cache.get_cached_location(self)

screen = self.last_refresh_cache.screen
screen = self.last_refresh_cache.screen.copy()
del screen[num_common_lines:]

screeninfo = self.last_refresh_cache.screeninfo
screeninfo = self.last_refresh_cache.screeninfo.copy()
del screeninfo[num_common_lines:]

last_refresh_line_end_offsets = self.last_refresh_cache.line_end_offsets
del last_refresh_line_end_offsets[num_common_lines:]

pos = self.pos
pos -= offset

Expand Down Expand Up @@ -338,7 +335,6 @@ def calc_screen(self) -> list[str]:
prompt = self.get_prompt(ln, line_len >= pos >= 0)
while "\n" in prompt:
pre_prompt, _, prompt = prompt.partition("\n")
last_refresh_line_end_offsets.append(offset)
screen.append(pre_prompt)
screeninfo.append((0, []))
pos -= line_len + 1
Expand All @@ -347,7 +343,6 @@ def calc_screen(self) -> list[str]:
wrapcount = (sum(char_widths) + prompt_len) // self.console.width
if wrapcount == 0 or not char_widths:
offset += line_len + 1 # Takes all of the line plus the newline
last_refresh_line_end_offsets.append(offset)
screen.append(prompt + "".join(chars))
screeninfo.append((prompt_len, char_widths))
else:
Expand All @@ -369,7 +364,6 @@ def calc_screen(self) -> list[str]:
offset += index_to_wrap_before + 1 # Takes the newline
post = ""
after = []
last_refresh_line_end_offsets.append(offset)
render = pre + "".join(chars[:index_to_wrap_before]) + post
render_widths = char_widths[:index_to_wrap_before] + after
screen.append(render)
Expand Down
27 changes: 27 additions & 0 deletions Lib/test/test_pyrepl/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,33 @@ def test_setpos_from_xy_for_non_printing_char(self):
reader.setpos_from_xy(8, 0)
self.assertEqual(reader.pos, 7)

def test_prompt_with_newline_no_duplicate(self):
# gh-127068: prompts with newlines should not duplicate
# The bug is that screen = cache.screen creates a reference, not a copy,
from unittest.mock import MagicMock

console = MagicMock()
console.height = 100
console.width = 80

reader = prepare_reader(console)

screen = reader.calc_screen()
reader.last_refresh_cache.update_cache(reader, screen, reader.screeninfo)

cache_screen_before = reader.last_refresh_cache.screen
original_cache_content = cache_screen_before.copy()

reader.insert("h")
screen = reader.calc_screen()

self.assertEqual(
cache_screen_before, original_cache_content,
f"Cache was modified during calc_screen! "
f"Original: {original_cache_content}, Now: {cache_screen_before}"
)


@force_colorized_test_class
class TestReaderInColor(ScreenEqualMixin, TestCase):
def test_syntax_highlighting_basic(self):
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_pyrepl/test_windows_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,7 @@ def test_multiline_ctrl_z(self):
Event(evt="key", data='\x1a', raw=bytearray(b'\x1a')),
],
)
reader, con = self.handle_events_narrow(events)
self.assertEqual(reader.cxy, (2, 3))
reader, con = self.handle_events_narrow(events) # make sure can handele it.
con.restore()


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix an issue in default REPL where prompts containing newlines would be duplicated
on each keypress.
Loading