Skip to content

Commit 3ae8cc8

Browse files
ambvtanloong
andauthored
[3.13] gh-140131: Fix REPL cursor position on Windows when module completion suggestion line hits console width (GH-140333) (GH-145872)
(cherry picked from commit e13f6dc) Co-authored-by: Tan Long <tanloong@foxmail.com>
1 parent e989361 commit 3ae8cc8

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

Lib/_pyrepl/windows_console.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,13 @@ def __write_changed_line(
273273
self._erase_to_end()
274274

275275
self.__write(newline[x_pos:])
276-
if wlen(newline) == self.width:
277-
# If we wrapped we want to start at the next line
278-
self._move_relative(0, y + 1)
279-
self.posxy = 0, y + 1
280-
else:
281-
self.posxy = wlen(newline), y
276+
self.posxy = min(wlen(newline), self.width - 1), y
282277

283-
if "\x1b" in newline or y != self.posxy[1] or '\x1a' in newline:
284-
# ANSI escape characters are present, so we can't assume
285-
# anything about the position of the cursor. Moving the cursor
286-
# to the left margin should work to get to a known position.
287-
self.move_cursor(0, y)
278+
if "\x1b" in newline or y != self.posxy[1] or '\x1a' in newline:
279+
# ANSI escape characters are present, so we can't assume
280+
# anything about the position of the cursor. Moving the cursor
281+
# to the left margin should work to get to a known position.
282+
self.move_cursor(0, y)
288283

289284
def _scroll(
290285
self, top: int, bottom: int, left: int | None = None, right: int | None = None

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import sys
1010
import tempfile
1111
from unittest import TestCase, skipUnless, skipIf
12-
from unittest.mock import patch
12+
from unittest.mock import Mock, patch
1313
from test.support import force_not_colorized, make_clean_env, Py_DEBUG
1414
from test.support import SHORT_TIMEOUT, STDLIB_DIR
1515
from test.support.import_helper import import_module
@@ -1579,3 +1579,47 @@ def test_ctrl_d_single_line_end_no_newline(self):
15791579
)
15801580
reader, _ = handle_all_events(events)
15811581
self.assertEqual("hello", "".join(reader.buffer))
1582+
1583+
1584+
@skipUnless(sys.platform == "win32", "windows console only")
1585+
class TestWindowsConsoleEolWrap(TestCase):
1586+
def _make_mock_console(self, width=80):
1587+
from _pyrepl import windows_console as wc
1588+
1589+
console = object.__new__(wc.WindowsConsole)
1590+
1591+
console.width = width
1592+
console.posxy = (0, 0)
1593+
console.screen = [""]
1594+
1595+
console._hide_cursor = Mock()
1596+
console._show_cursor = Mock()
1597+
console._erase_to_end = Mock()
1598+
console._move_relative = Mock()
1599+
console.move_cursor = Mock()
1600+
console._WindowsConsole__write = Mock()
1601+
1602+
return console, wc
1603+
1604+
def test_short_line_sets_posxy_normally(self):
1605+
width = 10
1606+
y = 3
1607+
console, wc = self._make_mock_console(width=width)
1608+
old_line = ""
1609+
new_line = "a" * 3
1610+
wc.WindowsConsole._WindowsConsole__write_changed_line(
1611+
console, y, old_line, new_line, 0
1612+
)
1613+
self.assertEqual(console.posxy, (3, y))
1614+
1615+
def test_exact_width_line_does_not_wrap(self):
1616+
width = 10
1617+
y = 3
1618+
console, wc = self._make_mock_console(width=width)
1619+
old_line = ""
1620+
new_line = "a" * width
1621+
1622+
wc.WindowsConsole._WindowsConsole__write_changed_line(
1623+
console, y, old_line, new_line, 0
1624+
)
1625+
self.assertEqual(console.posxy, (width - 1, y))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix inconsistent display of long multiline pasted content in the REPL.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix REPL cursor position on Windows when module completion suggestion line
2+
hits console width.

0 commit comments

Comments
 (0)