Skip to content

Commit

Permalink
pythongh-121499: Fix multi-line history rendering in the REPL
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Galindo <[email protected]>
  • Loading branch information
pablogsal committed Jul 9, 2024
1 parent 8d0cafd commit f589b69
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions Lib/_pyrepl/historical_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ def select_item(self, i: int) -> None:
self.historyi = i
self.pos = len(self.buffer)
self.dirty = True
self.last_refresh_cache.invalidated = True

def get_item(self, i: int) -> str:
if i != len(self.history):
Expand Down
6 changes: 6 additions & 0 deletions Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ class RefreshCache:
pos: int = field(init=False)
cxy: tuple[int, int] = field(init=False)
dimensions: tuple[int, int] = field(init=False)
invalidated: bool = False

def update_cache(self,
reader: Reader,
Expand All @@ -266,14 +267,19 @@ def update_cache(self,
self.pos = reader.pos
self.cxy = reader.cxy
self.dimensions = reader.console.width, reader.console.height
self.invalidated = False

def valid(self, reader: Reader) -> bool:
if self.invalidated:
return False
dimensions = reader.console.width, reader.console.height
dimensions_changed = dimensions != self.dimensions
paste_changed = reader.in_bracketed_paste != self.in_bracketed_paste
return not (dimensions_changed or paste_changed)

def get_cached_location(self, reader: Reader) -> tuple[int, int]:
if self.invalidated:
raise RuntimeError("Trying to use an invalid cache")
offset = 0
earliest_common_pos = min(reader.pos, self.pos)
num_common_lines = len(self.line_end_offsets)
Expand Down
5 changes: 2 additions & 3 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,8 @@ def register_readline():

def write_history():
try:
# _pyrepl.__main__ is executed as the __main__ module
from __main__ import CAN_USE_PYREPL
except ImportError:
from _pyrepl.main import CAN_USE_PYREPL
except ImportError as e:
CAN_USE_PYREPL = False

try:
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_pyrepl/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ def code_to_events(code: str):
yield Event(evt="key", data=c, raw=bytearray(c.encode("utf-8")))


def clean_screen(screen: Iterable[str]):
"""Cleans color and console characters out of a screen output.
This is useful for screen testing, it increases the test readability since
it strips out all the unreadable side of the screen.
"""
return '\n'.join(screen).replace(
'\x1b[1;35m>>>\x1b[0m', '').replace('\x1b[1;35m...\x1b[0m', '').strip()


def prepare_reader(console: Console, **kwargs):
config = ReadlineConfig(readline_completer=kwargs.pop("readline_completer", None))
reader = ReadlineAlikeReader(console=console, config=config)
Expand Down
38 changes: 38 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
more_lines,
multiline_input,
code_to_events,
clean_screen
)
from _pyrepl.console import Event
from _pyrepl.readline import ReadlineAlikeReader, ReadlineConfig
Expand Down Expand Up @@ -486,6 +487,7 @@ def test_basic(self):

output = multiline_input(reader)
self.assertEqual(output, "1+1")
self.assertEqual(clean_screen(reader.screen), "1+1")

def test_multiline_edit(self):
events = itertools.chain(
Expand Down Expand Up @@ -515,8 +517,10 @@ def test_multiline_edit(self):

output = multiline_input(reader)
self.assertEqual(output, "def f():\n ...\n ")
self.assertEqual(clean_screen(reader.screen), "def f():\n ...")
output = multiline_input(reader)
self.assertEqual(output, "def g():\n pass\n ")
self.assertEqual(clean_screen(reader.screen), "def g():\n pass")

def test_history_navigation_with_up_arrow(self):
events = itertools.chain(
Expand All @@ -535,12 +539,40 @@ def test_history_navigation_with_up_arrow(self):

output = multiline_input(reader)
self.assertEqual(output, "1+1")
self.assertEqual(clean_screen(reader.screen), "1+1")
output = multiline_input(reader)
self.assertEqual(output, "2+2")
self.assertEqual(clean_screen(reader.screen), "2+2")
output = multiline_input(reader)
self.assertEqual(output, "2+2")
self.assertEqual(clean_screen(reader.screen), "2+2")
output = multiline_input(reader)
self.assertEqual(output, "1+1")
self.assertEqual(clean_screen(reader.screen), "1+1")

def test_history_with_multiline_entries(self):
code = "def foo():\nx = 1\ny = 2\nz = 3\n\ndef bar():\nreturn 42\n\n"
events = list(itertools.chain(
code_to_events(code),
[
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")),
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")),
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")),
Event(evt="key", data="\n", raw=bytearray(b"\n")),
Event(evt="key", data="\n", raw=bytearray(b"\n")),
]
))

reader = self.prepare_reader(events)
output = multiline_input(reader)
output = multiline_input(reader)
output = multiline_input(reader)
self.assertEqual(
clean_screen(reader.screen),
'def foo():\n x = 1\n y = 2\n z = 3'
)
self.assertEqual(output, "def foo():\n x = 1\n y = 2\n z = 3\n ")


def test_history_navigation_with_down_arrow(self):
events = itertools.chain(
Expand All @@ -558,6 +590,7 @@ def test_history_navigation_with_down_arrow(self):

output = multiline_input(reader)
self.assertEqual(output, "1+1")
self.assertEqual(clean_screen(reader.screen), "1+1")

def test_history_search(self):
events = itertools.chain(
Expand All @@ -574,18 +607,23 @@ def test_history_search(self):

output = multiline_input(reader)
self.assertEqual(output, "1+1")
self.assertEqual(clean_screen(reader.screen), "1+1")
output = multiline_input(reader)
self.assertEqual(output, "2+2")
self.assertEqual(clean_screen(reader.screen), "2+2")
output = multiline_input(reader)
self.assertEqual(output, "3+3")
self.assertEqual(clean_screen(reader.screen), "3+3")
output = multiline_input(reader)
self.assertEqual(output, "1+1")
self.assertEqual(clean_screen(reader.screen), "1+1")

def test_control_character(self):
events = code_to_events("c\x1d\n")
reader = self.prepare_reader(events)
output = multiline_input(reader)
self.assertEqual(output, "c\x1d")
self.assertEqual(clean_screen(reader.screen), "c")


class TestPyReplCompleter(TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a bug affecting how multi-line history was being rendered in the new
REPL after interacting with the new screen cache. Patch by Pablo Galindo

0 comments on commit f589b69

Please sign in to comment.