8000 gh-119896: Fix CTRL-Z behavior in the new REPL on Windows (GH-122217) · python/cpython@d1a1bca · GitHub
[go: up one dir, main page]

Skip to content

Commit d1a1bca

Browse files
authored
gh-119896: Fix CTRL-Z behavior in the new REPL on Windows (GH-122217)
1 parent d27a53f commit d1a1bca

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

Lib/_pyrepl/reader.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
from __future__ import annotations
2323

24+
import sys
25+
2426
from contextlib import contextmanager
2527
from dataclasses import dataclass, field, fields
2628
import unicodedata
@@ -52,7 +54,10 @@ def disp_str(buffer: str) -> tuple[str, list[int]]:
5254
b: list[int] = []
5355
s: list[str] = []
5456
for c in buffer:
55-
if ord(c) < 128:
57+
if c == '\x1a':
58+
s.append(c)
59+
b.append(2)
60+
elif ord(c) < 128:
5661
s.append(c)
5762
b.append(1)
5863
elif unicodedata.category(c).startswith("C"):
@@ -110,7 +115,7 @@ def make_default_commands() -> dict[CommandName, type[Command]]:
110115
(r"\C-w", "unix-word-rubout"),
111116
(r"\C-x\C-u", "upcase-region"),
112117
(r"\C-y", "yank"),
113-
(r"\C-z", "suspend"),
118+
*(() if sys.platform == "win32" else ((r"\C-z", "suspend"), )),
114119
(r"\M-b", "backward-word"),
115120
(r"\M-c", "capitalize-word"),
116121
(r"\M-d", "kill-word"),

Lib/_pyrepl/simple_interact.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def _clear_screen():
7676
"copyright": _sitebuiltins._Printer('copyright', sys.copyright),
7777
"help": "help",
7878
"clear": _clear_screen,
79+
"\x1a": _sitebuiltins.Quitter('\x1a', ''),
7980
}
8081

8182

Lib/_pyrepl/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ def wlen(s: str) -> int:
2121
length = sum(str_width(i) for i in s)
2222
# remove lengths of any escape sequences
2323
sequence = ANSI_ESCAPE_SEQUENCE.findall(s)
24-
return length - sum(len(i) for i in sequence)
24+
ctrl_z_cnt = s.count('\x1a')
25+
return length - sum(len(i) for i in sequence) + ctrl_z_cnt

Lib/_pyrepl/windows_console.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def __write_changed_line(
253253
else:
254254
self.__posxy = wlen(newline), y
255255

256-
if "\x1b" in newline or y != self.__posxy[1]:
256+
if "\x1b" in newline or y != self.__posxy[1] or '\x1a' in newline:
257257
# ANSI escape characters are present, so we can't assume
258258
# anything about the position of the cursor. Moving the cursor
259259
# to the left margin should work to get to a known position.
@@ -291,6 +291,9 @@ def _disable_blinking(self):
291291
self.__write("\x1b[?12l")
292292

293293
def __write(self, text: str) -> None:
294+
if "\x1a" in text:
295+
text = ''.join(["^Z" if x == '\x1a' else x for x in text])
296+
294297
if self.out is not None:
295298
self.out.write(text.encode(self.encoding, "replace"))
296299
self.out.flush()

0 commit comments

Comments
 (0)
0