Skip to content

Commit 0c5151b

Browse files
sergey-miryanovtomasr8chris-eibl
authored
pythongh-131878: Fix input of unicode characters with two or more code points in new pyrepl on Windows (pythongh-131901)
Co-authored-by: Tomas R. <[email protected]> Co-authored-by: Chris Eibl <[email protected]>
1 parent d6078ed commit 0c5151b

File tree

4 files changed

+68
-27
lines changed

4 files changed

+68
-27
lines changed

Lib/_pyrepl/base_eventqueue.py

+4-8
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,8 @@ def get_event(self, block: bool = True) -> Event | None:
485485
return None
486486
elif self.__vt_support:
487487
# If virtual terminal is enabled, scanning VT sequences
488-
self.event_queue.push(rec.Event.KeyEvent.uChar.UnicodeChar)
488+
for char in raw_key.encode(self.event_queue.encoding, "replace"):
489+
self.event_queue.push(char)
489490
continue
490491

491492
if key_event.dwControlKeyState & ALT_ACTIVE:

Lib/test/test_pyrepl/test_eventqueue.py

+60-18
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_push_with_key_in_keymap(self, mock_keymap):
5353
mock_keymap.compile_keymap.return_value = {"a": "b"}
5454
eq = self.make_eventqueue()
5555
eq.keymap = {b"a": "b"}
56-
eq.push("a")
56+
eq.push(b"a")
5757
mock_keymap.compile_keymap.assert_called()
5858
self.assertEqual(eq.events[0].evt, "key")
5959
self.assertEqual(eq.events[0].data, "b")
@@ -63,7 +63,7 @@ def test_push_without_key_in_keymap(self, mock_keymap):
6363
mock_keymap.compile_keymap.return_value = {"a": "b"}
6464
eq = self.make_eventqueue()
6565
eq.keymap = {b"c": "d"}
66-
eq.push("a")
66+
eq.push(b"a")
6767
mock_keymap.compile_keymap.assert_called()
6868
self.assertEqual(eq.events[0].evt, "key")
6969
self.assertEqual(eq.events[0].data, "a")
@@ -73,13 +73,13 @@ def test_push_with_keymap_in_keymap(self, mock_keymap):
7373
mock_keymap.compile_keymap.return_value = {"a": "b"}
7474
eq = self.make_eventqueue()
7575
eq.keymap = {b"a": {b"b": "c"}}
76-
eq.push("a")
76+
eq.push(b"a")
7777
mock_keymap.compile_keymap.assert_called()
7878
self.assertTrue(eq.empty())
79-
eq.push("b")
79+
eq.push(b"b")
8080
self.assertEqual(eq.events[0].evt, "key")
8181
self.assertEqual(eq.events[0].data, "c")
82-
eq.push("d")
82+
eq.push(b"d")
8383
self.assertEqual(eq.events[1].evt, "key")
8484
self.assertEqual(eq.events[1].data, "d")
8585

@@ -88,32 +88,32 @@ def test_push_with_keymap_in_keymap_and_escape(self, mock_keymap):
8888
mock_keymap.compile_keymap.return_value = {"a": "b"}
8989
eq = self.make_eventqueue()
9090
eq.keymap = {b"a": {b"b": "c"}}
91-
eq.push("a")
91+
eq.push(b"a")
9292
mock_keymap.compile_keymap.assert_called()
9393
self.assertTrue(eq.empty())
9494
eq.flush_buf()
95-
eq.push("\033")
95+
eq.push(b"\033")
9696
self.assertEqual(eq.events[0].evt, "key")
9797
self.assertEqual(eq.events[0].data, "\033")
98-
eq.push("b")
98+
eq.push(b"b")
9999
self.assertEqual(eq.events[1].evt, "key")
100100
self.assertEqual(eq.events[1].data, "b")
101101

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

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

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

132174

133175
@unittest.skipIf(support.MS_WINDOWS, "No Unix event queue on Windows")
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)