Skip to content

Commit 891232f

Browse files
ambvsergey-miryanovtomasr8chris-eibl
authored
[3.13] gh-131878: Fix input of unicode characters with two or more code points in new pyrepl on Windows (gh-131901) (gh-133468)
(cherry picked from commit 0c5151b) Co-authored-by: Sergey Miryanov <[email protected]> Co-authored-by: Tomas R. <[email protected]> Co-authored-by: Chris Eibl <[email protected]>
1 parent 76f52c4 commit 891232f

File tree

4 files changed

+68
-27
lines changed

4 files changed

+68
-27
lines changed

Lib/_pyrepl/base_eventqueue.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,14 @@ def insert(self, event: Event) -> None:
6969
trace('added event {event}', event=event)
7070
self.events.append(event)
7171

72-
def push(self, char: int | bytes | str) -> None:
72+
def push(self, char: int | bytes) -> None:
7373
"""
7474
Processes a character by updating the buffer and handling special key mappings.
7575
"""
76+
assert isinstance(char, (int, bytes))
7677
ord_char = char if isinstance(char, int) else ord(char)
77-
if ord_char > 255:
78-
assert isinstance(char, str)
79-
char = bytes(char.encode(self.encoding, "replace"))
80-
self.buf.extend(char)
81-
else:
82-
char = bytes(bytearray((ord_char,)))
83-
self.buf.append(ord_char)
78+
char = ord_char.to_bytes()
79+
self.buf.append(ord_char)
8480

8581
if char in self.keymap:
8682
if self.keymap is self.compiled_keymap:

Lib/_pyrepl/windows_console.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@ def get_event(self, block: bool = True) -> Event | None:
469469
return None
470470
elif self.__vt_support:
471471
# If virtual terminal is enabled, scanning VT sequences
472-
self.event_queue.push(rec.Event.KeyEvent.uChar.UnicodeChar)
472+
for char in raw_key.encode(self.event_queue.encoding, "replace"):
473+
self.event_queue.push(char)
473474
continue
474475

475476
if key_event.dwControlKeyState & ALT_ACTIVE:

Lib/test/test_pyrepl/test_eventqueue.py

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_push_with_key_in_keymap(self, mock_keymap):
5454
mock_keymap.compile_keymap.return_value = {"a": "b"}
5555
eq = self.make_eventqueue()
5656
eq.keymap = {b"a": "b"}
57-
eq.push("a")
57+
eq.push(b"a")
5858
mock_keymap.compile_keymap.assert_called()
5959
self.assertEqual(eq.events[0].evt, "key")
6060
self.assertEqual(eq.events[0].data, "b")
@@ -64,7 +64,7 @@ def test_push_without_key_in_keymap(self, mock_keymap):
6464
mock_keymap.compile_keymap.return_value = {"a": "b"}
6565
eq = self.make_eventqueue()
6666
eq.keymap = {b"c": "d"}
67-
eq.push("a")
67+
eq.push(b"a")
6868
mock_keymap.compile_keymap.assert_called()
6969
self.assertEqual(eq.events[0].evt, "key")
7070
self.assertEqual(eq.events[0].data, "a")
@@ -74,13 +74,13 @@ def test_push_with_keymap_in_keymap(self, mock_keymap):
7474
mock_keymap.compile_keymap.return_value = {"a": "b"}
7575
eq = self.make_eventqueue()
7676
eq.keymap = {b"a": {b"b": "c"}}
77-
eq.push("a")
77+
eq.push(b"a")
7878
mock_keymap.compile_keymap.assert_called()
7979
self.assertTrue(eq.empty())
80-
eq.push("b")
80+
eq.push(b"b")
8181
self.assertEqual(eq.events[0].evt, "key")
8282
self.assertEqual(eq.events[0].data, "c")
83-
eq.push("d")
83+
eq.push(b"d")
8484
self.assertEqual(eq.events[1].evt, "key")
8585
self.assertEqual(eq.events[1].data, "d")
8686

@@ -89,32 +89,32 @@ def test_push_with_keymap_in_keymap_and_escape(self, mock_keymap):
8989
mock_keymap.compile_keymap.return_value = {"a": "b"}
9090
eq = self.make_eventqueue()
9191
eq.keymap = {b"a": {b"b": "c"}}
92-
eq.push("a")
92+
eq.push(b"a")
9393
mock_keymap.compile_keymap.assert_called()
9494
self.assertTrue(eq.empty())
9595
eq.flush_buf()
96-
eq.push("\033")
96+
eq.push(b"\033")
9797
self.assertEqual(eq.events[0].evt, "key")
9898
self.assertEqual(eq.events[0].data, "\033")
99-
eq.push("b")
99+
eq.push(b"b")
100100
self.assertEqual(eq.events[1].evt, "key")
101101
self.assertEqual(eq.events[1].data, "b")
102102

103103
def test_push_special_key(self):
104104
eq = self.make_eventqueue()
105105
eq.keymap = {}
106-
eq.push("\x1b")
107-
eq.push("[")
108-
eq.push("A")
106+
eq.push(b"\x1b")
107+
eq.push(b"[")
108+
eq.push(b"A")
109109
self.assertEqual(eq.events[0].evt, "key")
110110
self.assertEqual(eq.events[0].data, "\x1b")
111111

112112
def test_push_unrecognized_escape_sequence(self):
113113
eq = self.make_eventqueue()
114114
eq.keymap = {}
115-
eq.push("\x1b")
116-
eq.push("[")
117-
eq.push("Z")
115+
eq.push(b"\x1b")
116+
eq.push(b"[")
117+
eq.push(b"Z")
118118
self.assertEqual(len(eq.events), 3)
119119
self.assertEqual(eq.events[0].evt, "key")
120120
self.assertEqual(eq.events[0].data, "\x1b")
@@ -123,12 +123,54 @@ def test_push_unrecognized_escape_sequence(self):
123123
self.assertEqual(eq.events[2].evt, "key")
124124
self.assertEqual(eq.events[2].data, "Z")
125125

126-
def test_push_unicode_character(self):
126+
def test_push_unicode_character_as_str(self):
127127
eq = self.make_eventqueue()
128128
eq.keymap = {}
129-
eq.push("ч")
130-
self.assertEqual(eq.events[0].evt, "key")
131-
self.assertEqual(eq.events[0].data, "ч")
129+
with self.assertRaises(AssertionError):
130+
eq.push("ч")
131+
with self.assertRaises(AssertionError):
132+
eq.push("ñ")
133+
134+
def test_push_unicode_character_two_bytes(self):
135+
eq = self.make_eventqueue()
136+
eq.keymap = {}
137+
138+
encoded = "ч".encode(eq.encoding, "replace")
139+
self.assertEqual(len(encoded), 2)
140+
141+
eq.push(encoded[0])
142+
e = eq.get()
143+
self.assertIsNone(e)
144+
145+
eq.push(encoded[1])
146+
e = eq.get()
147+
self.assertEqual(e.evt, "key")
148+
self.assertEqual(e.data, "ч")
149+
150+
def test_push_single_chars_and_unicode_character_as_str(self):
151+
eq = self.make_eventqueue()
152+
eq.keymap = {}
153+
154+
def _event(evt, data, raw=None):
155+
r = raw if raw is not None else data.encode(eq.encoding)
156+
e = Event(evt, data, r)
157+
return e
158+
159+
def _push(keys):
160+
for k in keys:
161+
eq.push(k)
162+
163+
self.assertIsInstance("ñ", str)
164+
165+
# If an exception happens during push, the existing events must be
166+
# preserved and we can continue to push.
167+
_push(b"b")
168+
with self.assertRaises(AssertionError):
169+
_push("ñ")
170+
_push(b"a")
171+
172+
self.assertEqual(eq.get(), _event("key", "b"))
173+
self.assertEqual(eq.get(), _event("key", "a"))
132174

133175

134176
@unittest.skipIf(support.MS_WINDOWS, "No Unix event queue on Windows")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix support of unicode characters with two or more codepoints on Windows in
2+
the new REPL.

0 commit comments

Comments
 (0)