gh-141968: Use take_bytes to simplify and remove copy from pyrepl BaseEventQueue (#149852)

This commit is contained in:
Lukas Geiger
2026-07-11 16:59:37 +01:00
committed by GitHub
parent 931cfdf8ed
commit 65afd6579f
2 changed files with 5 additions and 19 deletions
+4 -12
View File
@@ -54,14 +54,6 @@ class BaseEventQueue:
"""
return not self.events
def flush_buf(self) -> bytearray:
"""
Flushes the buffer and returns its contents.
"""
old = self.buf
self.buf = bytearray()
return old
def insert(self, event: Event) -> None:
"""
Inserts an event into the queue.
@@ -87,7 +79,7 @@ class BaseEventQueue:
if isinstance(k, dict):
self.keymap = k
else:
self.insert(Event('key', k, bytes(self.flush_buf())))
self.insert(Event('key', k, self.buf.take_bytes())) # type: ignore[attr-defined]
self.keymap = self.compiled_keymap
elif self.buf and self.buf[0] == 27: # escape
@@ -97,14 +89,14 @@ class BaseEventQueue:
trace('unrecognized escape sequence, propagating...')
self.keymap = self.compiled_keymap
self.insert(Event('key', '\033', b'\033'))
for _c in self.flush_buf()[1:]:
for _c in self.buf.take_bytes()[1:]: # type: ignore[attr-defined]
self.push(_c)
else:
try:
decoded = bytes(self.buf).decode(self.encoding)
decoded = self.buf.decode(self.encoding)
except UnicodeError:
return
else:
self.insert(Event('key', decoded, bytes(self.flush_buf())))
self.insert(Event('key', decoded, self.buf.take_bytes())) # type: ignore[attr-defined]
self.keymap = self.compiled_keymap
+1 -7
View File
@@ -38,12 +38,6 @@ class EventQueueTestBase:
eq.insert(Event("key", "a", b"a"))
self.assertFalse(eq.empty())
def test_flush_buf(self):
eq = self.make_eventqueue()
eq.buf.extend(b"test")
self.assertEqual(eq.flush_buf(), b"test")
self.assertEqual(eq.buf, bytearray())
def test_insert(self):
eq = self.make_eventqueue()
event = Event("key", "a", b"a")
@@ -93,7 +87,7 @@ class EventQueueTestBase:
eq.push(b"a")
mock_keymap.compile_keymap.assert_called()
self.assertTrue(eq.empty())
eq.flush_buf()
eq.buf.resize(0)
eq.push(b"\033")
self.assertEqual(eq.events[0].evt, "key")
self.assertEqual(eq.events[0].data, "\033")