]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-130804: Fix support of typing unicode chars in pyrepl (GH-130805) (GH-133462)
authorŁukasz Langa <lukasz@langa.pl>
Mon, 5 May 2025 19:39:21 +0000 (21:39 +0200)
committerGitHub <noreply@github.com>
Mon, 5 May 2025 19:39:21 +0000 (21:39 +0200)
(cherry picked from commit 7c98b0674daa3e4eb3e8f35afb61a0dba61d1780)

Co-authored-by: Sergey Miryanov <sergey.miryanov@gmail.com>
Lib/_pyrepl/base_eventqueue.py
Lib/test/test_pyrepl/test_eventqueue.py
Misc/NEWS.d/next/Core and Builtins/2025-03-10-21-46-37.gh-issue-130804.0PpcTx.rst [new file with mode: 0644]

index 9cae1db112a838e30c38d3f645c616a4e691e259..e018c4fc18308e31eea009788e5144c73a96c62b 100644 (file)
@@ -69,13 +69,19 @@ class BaseEventQueue:
         trace('added event {event}', event=event)
         self.events.append(event)
 
-    def push(self, char: int | bytes) -> None:
+    def push(self, char: int | bytes | str) -> None:
         """
         Processes a character by updating the buffer and handling special key mappings.
         """
         ord_char = char if isinstance(char, int) else ord(char)
-        char = bytes(bytearray((ord_char,)))
-        self.buf.append(ord_char)
+        if ord_char > 255:
+            assert isinstance(char, str)
+            char = bytes(char.encode(self.encoding, "replace"))
+            self.buf.extend(char)
+        else:
+            char = bytes(bytearray((ord_char,)))
+            self.buf.append(ord_char)
+
         if char in self.keymap:
             if self.keymap is self.compiled_keymap:
                 # sanity check, buffer is empty when a special key comes
index a1bac38fbd43f50550c7e0e3f4ad747f42160fce..b25bdb956b0d144b317d3abd145415354dfbad9e 100644 (file)
@@ -123,6 +123,13 @@ class EventQueueTestBase:
         self.assertEqual(eq.events[2].evt, "key")
         self.assertEqual(eq.events[2].data, "Z")
 
+    def test_push_unicode_character(self):
+        eq = self.make_eventqueue()
+        eq.keymap = {}
+        eq.push("ч")
+        self.assertEqual(eq.events[0].evt, "key")
+        self.assertEqual(eq.events[0].data, "ч")
+
 
 @unittest.skipIf(support.MS_WINDOWS, "No Unix event queue on Windows")
 class TestUnixEventQueue(EventQueueTestBase, unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2025-03-10-21-46-37.gh-issue-130804.0PpcTx.rst b/Misc/NEWS.d/next/Core and Builtins/2025-03-10-21-46-37.gh-issue-130804.0PpcTx.rst
new file mode 100644 (file)
index 0000000..37a9b83
--- /dev/null
@@ -0,0 +1 @@
+Fix support of unicode characters on Windows in the new REPL.