8000 gh-118877: Fix AssertionError crash in pyrepl (#118936) · python/cpython@c0d81b2 · GitHub
[go: up one dir, main page]

Skip to content

Commit c0d81b2

Browse files
authored
gh-118877: Fix AssertionError crash in pyrepl (#118936)
1 parent 7e1a130 commit c0d81b2

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

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
@@ -607,6 +607,30 @@ def test_global_namespace_completion(self):
607607
output = multiline_input(reader, namespace)
608608
self.assertEqual(output, "python")
609609

610+
def test_updown_arrow_with_completion_menu(self):
611+
"""Up arrow in the middle of unfinished tab completion when the menu is displayed
612+
should work and trigger going back in history. Down arrow should subsequently
613+
get us back to the incomplete command."""
614+
code = "import os\nos.\t\t"
615+
namespace = {"os": os}
616+
617+
events = itertools.chain(
618+
code_to_events(code),
619+
[
620+
Event(evt='key', data='up', raw=bytearray(b'\x1bOA')),
621+
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")),
622+
],
623+
code_to_events("\n")
624+
)
625+
reader = self.prepare_reader(events, namespace=namespace)
626+
output = multiline_input(reader, namespace)
627+
# This is the first line, nothing to see here
628+
self.assertEqual(output, "import os")
629+
# This is the second line. We pressed up and down arrows
630+
# so we should end up where we were when we initiated tab completion.
631+
output = multiline_input(reader, namespace)
632+
self.assertEqual(output, "os.")
633+
610634

611635
@patch("_pyrepl.curses.tigetstr", lambda x: b"")
612636
class TestUnivEventQueue(TestCase):
@@ -1001,6 +1025,5 @@ def test_up_arrow_after_ctrl_r(self):
10011025
reader, _ = handle_all_events(events)
10021026
self.assert_screen_equals(reader, "")
10031027

1004-
10051028
if __name__ == '__main__':
10061029
unittest.main()

0 commit comments

Comments
 (0)
0