Skip to content

Commit 7c98b06

Browse files
gh-130804: Fix support of typing unicode chars in pyrepl (#130805)
1 parent 6ab5c4a commit 7c98b06

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

Lib/_pyrepl/base_eventqueue.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,19 @@ 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) -> None:
72+
def push(self, char: int | bytes | str) -> None:
7373
"""
7474
Processes a character by updating the buffer and handling special key mappings.
7575
"""
7676
ord_char = char if isinstance(char, int) else ord(char)
77-
char = bytes(bytearray((ord_char,)))
78-
self.buf.append(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)
84+
7985
if char in self.keymap:
8086
if self.keymap is self.compiled_keymap:
8187
# sanity check, buffer is empty when a special key comes

Lib/test/test_pyrepl/test_eventqueue.py

+7
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ 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):
127+
eq = self.make_eventqueue()
128+
eq.keymap = {}
129+
eq.push("ч")
130+
self.assertEqual(eq.events[0].evt, "key")
131+
self.assertEqual(eq.events[0].data, "ч")
132+
126133

127134
@unittest.skipIf(support.MS_WINDOWS, "No Unix event queue on Windows")
128135
class TestUnixEventQueue(EventQueueTestBase, unittest.TestCase):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix support of unicode characters on Windows in the new REPL.

0 commit comments

Comments
 (0)