Skip to content

Commit e13f6dc

Browse files
authored
gh-140131: Fix REPL cursor position on Windows when module completion suggestion line hits console width (GH-140333)
1 parent d4cc553 commit e13f6dc

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
@@ -270,18 +270,13 @@ def __write_changed_line(
270270
self._erase_to_end()
271271

272272
self.__write(newline[x_pos:])
273-
if wlen(newline) == self.width:
274-
# If we wrapped we want to start at the next line
275-
self._move_relative(0, y + 1)
276-
self.posxy = 0, y + 1
277-
else:
278-
self.posxy = wlen(newline), y
273+
self.posxy = min(wlen(newline), self.width - 1), y
279274

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

286281
def _scroll(
287282
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
@@ -12,7 +12,7 @@
1212
import tempfile
1313
from pkgutil import ModuleInfo
1414
from unittest import TestCase, skipUnless, skipIf, SkipTest
15-
from unittest.mock import patch
15+
from unittest.mock import Mock, patch
1616
from test.support import force_not_colorized, make_clean_env, Py_DEBUG
1717
from test.support import has_subprocess_support, SHORT_TIMEOUT, STDLIB_DIR
1818
from test.support.import_helper import import_module
@@ -2105,3 +2105,47 @@ def test_ctrl_d_single_line_end_no_newline(self):
21052105
)
21062106
reader, _ = handle_all_events(events)
21072107
self.assertEqual("hello", "".join(reader.buffer))
2108+
2109+
2110+
@skipUnless(sys.platform == "win32", "windows console only")
2111+
class TestWindowsConsoleEolWrap(TestCase):
2112+
def _make_mock_console(self, width=80):
2113+
from _pyrepl import windows_console as wc
2114+
2115+
console = object.__new__(wc.WindowsConsole)
2116+
2117+
console.width = width
2118+
console.posxy = (0, 0)
2119+
console.screen = [""]
2120+
2121+
console._hide_cursor = Mock()
2122+
console._show_cursor = Mock()
2123+
console._erase_to_end = Mock()
2124+
console._move_relative = Mock()
2125+
console.move_cursor = Mock()
2126+
console._WindowsConsole__write = Mock()
2127+
2128+
return console, wc
2129+
2130+
def test_short_line_sets_posxy_normally(self):
2131+
width = 10
2132+
y = 3
2133+
console, wc = self._make_mock_console(width=width)
2134+
old_line = ""
2135+
new_line = "a" * 3
2136+
wc.WindowsConsole._WindowsConsole__write_changed_line(
2137+
console, y, old_line, new_line, 0
2138+
)
2139+
self.assertEqual(console.posxy, (3, y))
2140+
2141+
def test_exact_width_line_does_not_wrap(self):
2142+
width = 10
2143+
y = 3
2144+
console, wc = self._make_mock_console(width=width)
2145+
old_line = ""
2146+
new_line = "a" * width
2147+
2148+
wc.WindowsConsole._WindowsConsole__write_changed_line(
2149+
console, y, old_line, new_line, 0
2150+
)
2151+
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)