Skip to content

Commit a9044e7

Browse files
danielhollasestyxx
authored andcommitted
pythongh-118877: Fix AssertionError crash in pyrepl (python#118936)
1 parent 361a106 commit a9044e7

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

Lib/_pyrepl/commands.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@
3434

3535
# types
3636
if False:
37-
from .reader import Reader
3837
from .historical_reader import HistoricalReader
39-
from .console import Event
4038

4139

4240
class Command:
@@ -245,7 +243,7 @@ def do(self) -> None:
245243
x, y = r.pos2xy()
246244
new_y = y - 1
247245

248-
if new_y < 0:
246+
if r.bol() == 0:
249247
if r.historyi > 0:
250248
r.select_item(r.historyi - 1)
251249
return

Lib/test/test_pyrepl.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,30 @@ def test_global_namespace_completion(self):
636636
output = multiline_input(reader, namespace)
637637
self.assertEqual(output, "python")
638638

639+
def test_updown_arrow_with_completion_menu(self):
640+
"""Up arrow in the middle of unfinished tab completion when the menu is displayed
641+
should work and trigger going back in history. Down arrow should subsequently
642+
get us back to the incomplete command."""
643+
code = "import os\nos.\t\t"
644+
namespace = {"os": os}
645+
646+
events = itertools.chain(
647+
code_to_events(code),
648+
[
649+
Event(evt='key', data='up', raw=bytearray(b'\x1bOA')),
650+
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")),
651+
],
652+
code_to_events("\n")
653+
)
654+
reader = self.prepare_reader(events, namespace=namespace)
655+
output = multiline_input(reader, namespace)
656+
# This is the first line, nothing to see here
657+
self.assertEqual(output, "import os")
658+
# This is the second line. We pressed up and down arrows
659+
# so we should end up where we were when we initiated tab completion.
660+
output = multiline_input(reader, namespace)
661+
self.assertEqual(output, "os.")
662+
639663

640664
@patch("_pyrepl.curses.tigetstr", lambda x: b"")
641665
class TestUnivEventQueue(TestCase):
@@ -1030,6 +1054,5 @@ def test_up_arrow_after_ctrl_r(self):
10301054
reader, _ = handle_all_events(events)
10311055
self.assert_screen_equals(reader, "")
10321056

1033-
10341057
if __name__ == '__main__':
10351058
unittest.main()

0 commit comments

Comments
 (0)