Skip to content

Commit 925f3a2

Browse files
Apply Chris Eibl suggestionы and use only int and bytes in eventqueue.push
1 parent b1fa88d commit 925f3a2

File tree

2 files changed

+44
-45
lines changed

2 files changed

+44
-45
lines changed

Lib/_pyrepl/base_eventqueue.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +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-
77-
if isinstance(char, bytes):
78-
self.buf.extend(char)
79-
else:
80-
81-
ord_char = char if isinstance(char, int) else ord(char)
82-
if ord_char > 255:
83-
assert isinstance(char, str)
84-
char = bytes(char.encode(self.encoding, "replace"))
85-
self.buf.extend(char)
86-
else:
87-
char = bytes([ord_char])
88-
self.buf.append(ord_char)
76+
assert isinstance(char, (int, bytes))
77+
ord_char = char if isinstance(char, int) else ord(char)
78+
char = bytes((ord_char,))
79+
self.buf.append(ord_char)
8980

9081
if char in self.keymap:
9182
if self.keymap is self.compiled_keymap:
@@ -113,8 +104,7 @@ def push(self, char: int | bytes | str) -> None:
113104
try:
114105
decoded = bytes(self.buf).decode(self.encoding)
115106
except UnicodeError:
116-
self.flush_buf()
117-
raise
107+
return
118108
else:
119109
self.insert(Event('key', decoded, self.flush_buf()))
120110
finally:

Lib/test/test_pyrepl/test_eventqueue.py

Lines changed: 38 additions & 29 deletions
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,23 +122,31 @@ 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("ñ")
131132

132133
def test_push_unicode_character_as_bytes(self):
133134
eq = self.make_eventqueue()
134135
eq.keymap = {}
135136

136-
eq.push("ч".encode(eq.encoding, "replace"))
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])
137145
e = eq.get()
138146
self.assertEqual(e.evt, "key")
139147
self.assertEqual(e.data, "ч")
140148

141-
def test_push_long_unicode_character_as_bytes(self):
149+
def test_push_unicode_character_as_bytes_in_paste_mode(self):
142150
eq = self.make_eventqueue()
143151
eq.keymap = {}
144152

@@ -151,9 +159,9 @@ def _push(keys):
151159
for k in keys:
152160
eq.push(k)
153161

154-
_push("\x1b[200")
155-
eq.push("ñ".encode(eq.encoding, "replace"))
156-
_push("\x1b[201")
162+
_push(b"\x1b[200")
163+
_push("ñ".encode(eq.encoding, "replace"))
164+
_push(b"\x1b[201")
157165

158166
self.assertEqual(eq.get(), _event("key", "\x1b"))
159167
self.assertEqual(eq.get(), _event("key", "["))
@@ -169,7 +177,7 @@ def _push(keys):
169177
self.assertEqual(eq.get(), _event("key", "0"))
170178
self.assertEqual(eq.get(), _event("key", "1"))
171179

172-
def test_push_long_unicode_character(self):
180+
def test_push_unicode_character_as_str_in_paste_mode(self):
173181
eq = self.make_eventqueue()
174182
eq.keymap = {}
175183

@@ -182,11 +190,12 @@ def _push(keys):
182190
for k in keys:
183191
eq.push(k)
184192

185-
_push("\x1b[200")
186-
msg = "'utf-8' codec can't decode byte 0xf1 in position 0: unexpected end of data"
187-
with self.assertRaisesRegex(UnicodeDecodeError, msg):
188-
eq.push("ñ")
189-
_push("\x1b[201")
193+
self.assertIsInstance("ñ", str)
194+
195+
_push(b"\x1b[200")
196+
with self.assertRaises(AssertionError):
197+
_push("ñ")
198+
_push(b"\x1b[201")
190199

191200
self.assertEqual(eq.get(), _event("key", "\x1b"))
192201
self.assertEqual(eq.get(), _event("key", "["))

0 commit comments

Comments
 (0)