Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-144259: Fix Windows VT EOL wrap by syncing real console cursor#144297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
hyongtao-code wants to merge4 commits intopython:main
base:main
Choose a base branch
Loading
fromhyongtao-code:fix-issue-144259
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletionsLib/_pyrepl/windows_console.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -272,9 +272,22 @@ def __write_changed_line(

self.__write(newline[x_pos:])
if wlen(newline) == self.width:
# If we wrapped we want to start at the next line
self._move_relative(0, y + 1)
self.posxy = 0, y + 1
if self.__vt_support:
info = CONSOLE_SCREEN_BUFFER_INFO()
if not GetConsoleScreenBufferInfo(OutHandle, info):
raise WinError(GetLastError())
win_y = int(info.dwCursorPosition.Y - info.srWindow.Top)
expected = y - self.__offset
if win_y == expected + 1:
# Terminal wrapped to next row.
self.posxy = 0, y + 1
else:
# Terminal did not wrap; cursor stays at end-of-line.
self.posxy = self.width, y
else:
# If we wrapped we want to start at the next line
self._move_relative(0, y + 1)
self.posxy = 0, y + 1
else:
self.posxy = wlen(newline), y

Expand Down
74 changes: 73 additions & 1 deletionLib/test/test_pyrepl/test_pyrepl.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,7 +12,7 @@
import tempfile
from pkgutil import ModuleInfo
from unittest import TestCase, skipUnless, skipIf, SkipTest
from unittest.mock import patch
from unittest.mock importMock,patch
from test.support import force_not_colorized, make_clean_env, Py_DEBUG
from test.support import has_subprocess_support, SHORT_TIMEOUT, STDLIB_DIR
from test.support.import_helper import import_module
Expand DownExpand Up@@ -2105,3 +2105,75 @@ def test_ctrl_d_single_line_end_no_newline(self):
)
reader, _ = handle_all_events(events)
self.assertEqual("hello", "".join(reader.buffer))


@skipUnless(sys.platform == "win32", "Windows console VT behavior only")
class TestWindowsConsoleVtEolWrap(TestCase):
"""
When a line exactly fills the terminal width, VT terminals differ on whether
the cursor immediately wraps to the next row. In VT mode we must synchronize
our logical cursor position with the real console cursor.
"""
def _make_console_like(self, *, width: int, offset: int, vt: bool):
from _pyrepl import windows_console as wc

con = object.__new__(wc.WindowsConsole)

# Minimal state needed by __write_changed_line()
con.width = width
con.screen = []
con.posxy = (0, 0)
setattr(con, "_WindowsConsole__offset", offset)
setattr(con, "_WindowsConsole__vt_support", vt)

# Stub out side-effecting methods used by __write_changed_line()
con._hide_cursor = Mock()
con._erase_to_end = Mock()
con._move_relative = Mock()
con.move_cursor = Mock()
setattr(con, "_WindowsConsole__write", Mock())

return con, wc

def test_vt_exact_width_line_did_wrap(self):
# Terminal wrapped to next row: posxy should become (0, y+1).
width = 10
y = 3
con, wc = self._make_console_like(width=width, offset=0, vt=True)

def fake_gcsbi(_h, info):
info.dwCursorPosition.X = 0
# Visible window top = 0, cursor now on next visible row
info.srWindow.Top = 0
info.dwCursorPosition.Y = y + 1
return True

with patch.object(wc, "GetConsoleScreenBufferInfo", side_effect=fake_gcsbi), \
patch.object(wc, "OutHandle", 1):
old = ""
new = "a" * width
wc.WindowsConsole._WindowsConsole__write_changed_line(con, y, old, new, 0)
self.assertEqual(con.posxy, (0, y + 1))
self.assertNotIn((0, y + 1), [c.args for c in con._move_relative.mock_calls])

def test_vt_exact_width_line_did_not_wrap(self):
# Terminal did NOT wrap yet: posxy should stay at (width, y).
width = 10
y = 3
con, wc = self._make_console_like(width=width, offset=0, vt=True)

def fake_gcsbi(_h, info):
info.dwCursorPosition.X = width
info.srWindow.Top = 0
# Cursor remains on the same visible row
info.dwCursorPosition.Y = y
return True

with patch.object(wc, "GetConsoleScreenBufferInfo", side_effect=fake_gcsbi), \
patch.object(wc, "OutHandle", 1):
old = ""
new = "a" * width
wc.WindowsConsole._WindowsConsole__write_changed_line(con, y, old, new, 0)

self.assertEqual(con.posxy, (width, y))
self.assertNotIn((0, y + 1), [c.args for c in con._move_relative.mock_calls])
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Fix Windows VT REPL cursor desynchronization when a line exactly fills the terminal width.
Loading

[8]ページ先頭

©2009-2026 Movatter.jp