gh-127349: Add check for correct resizing in REPL (#127387)

This commit is contained in:
donBarbos 2025-01-30 19:34:09 +00:00 committed by GitHub
parent 4ca9fc08f8
commit 510fefdc62
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 3 deletions

View file

@ -587,10 +587,11 @@ class Reader:
def pos2xy(self) -> tuple[int, int]:
"""Return the x, y coordinates of position 'pos'."""
# this *is* incomprehensible, yes.
y = 0
p, y = 0, 0
l2: list[int] = []
pos = self.pos
assert 0 <= pos <= len(self.buffer)
if pos == len(self.buffer):
if pos == len(self.buffer) and len(self.screeninfo) > 0:
y = len(self.screeninfo) - 1
p, l2 = self.screeninfo[y]
return p + sum(l2) + l2.count(0), y

View file

@ -4,7 +4,7 @@ import rlcompleter
from unittest import TestCase
from unittest.mock import MagicMock
from .support import handle_all_events, handle_events_narrow_console, code_to_events, prepare_reader
from .support import handle_all_events, handle_events_narrow_console, code_to_events, prepare_reader, prepare_console
from _pyrepl.console import Event
from _pyrepl.reader import Reader
@ -312,3 +312,10 @@ class TestReader(TestCase):
reader, _ = handle_all_events(events, prepare_reader=completing_reader)
self.assert_screen_equals(reader, f"{code}a")
def test_pos2xy_with_no_columns(self):
console = prepare_console([])
reader = prepare_reader(console)
# Simulate a resize to 0 columns
reader.screeninfo = []
self.assertEqual(reader.pos2xy(), (0, 0))

View file

@ -0,0 +1,2 @@
Fixed the error when resizing terminal in Python REPL. Patch by Semyon
Moroz.