https://github.com/python/cpython/commit/510fefdc625dd2ed2b6b3975314a59e291b94ae8
commit: 510fefdc625dd2ed2b6b3975314a59e291b94ae8
branch: main
author: donBarbos <[email protected]>
committer: pablogsal <[email protected]>
date: 2025-01-30T19:34:09Z
summary:

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

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2024-11-30-16-13-31.gh-issue-127349.ssYd6n.rst
M Lib/_pyrepl/reader.py
M Lib/test/test_pyrepl/test_reader.py

diff --git a/Lib/_pyrepl/reader.py b/Lib/_pyrepl/reader.py
index 4b0700d069c621..1252847e02b2ea 100644
--- a/Lib/_pyrepl/reader.py
+++ b/Lib/_pyrepl/reader.py
@@ -587,10 +587,11 @@ def setpos_from_xy(self, x: int, y: int) -> None:
     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
diff --git a/Lib/test/test_pyrepl/test_reader.py 
b/Lib/test/test_pyrepl/test_reader.py
index 863ecc61ddd432..27c6d6664eda9e 100644
--- a/Lib/test/test_pyrepl/test_reader.py
+++ b/Lib/test/test_pyrepl/test_reader.py
@@ -4,7 +4,7 @@
 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 @@ def test_key_press_on_tab_press_once(self):
         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))
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2024-11-30-16-13-31.gh-issue-127349.ssYd6n.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-30-16-13-31.gh-issue-127349.ssYd6n.rst
new file mode 100644
index 00000000000000..3c1586b6cbb8e7
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2024-11-30-16-13-31.gh-issue-127349.ssYd6n.rst
@@ -0,0 +1,2 @@
+Fixed the error when resizing terminal in Python REPL. Patch by Semyon
+Moroz.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to