Skip to content

Commit baace7f

Browse files
Carson JonesCarson Jones
authored andcommitted
gh-145886: Fix e.raw += e.raw typo in UnixConsole.getpending
In both variants of getpending(), the raw bytes from queued events were being accumulated incorrectly: e.raw was being added to itself instead of accumulating e2.raw from the dequeued event. This meant the combined event's raw field would always be empty bytes (since it starts as b"" and b"" + b"" is b""). Fix the typo so that e.raw += e2.raw is used, matching the pattern already used for e.data += e2.data on the preceding line. Also add a test that verifies getpending correctly accumulates raw bytes from multiple queued events.
1 parent cd52172 commit baace7f

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

Lib/_pyrepl/unix_console.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ def getpending(self):
542542
while not self.event_queue.empty():
543543
e2 = self.event_queue.get()
544544
e.data += e2.data
545-
e.raw += e.raw
545+
e.raw += e2.raw
546546

547547
amount = struct.unpack("i", ioctl(self.input_fd, FIONREAD, b"\0\0\0\0"))[0]
548548
trace("getpending({a})", a=amount)
@@ -566,7 +566,7 @@ def getpending(self):
566566
while not self.event_queue.empty():
567567
e2 = self.event_queue.get()
568568
e.data += e2.data
569-
e.raw += e.raw
569+
e.raw += e2.raw
570570

571571
amount = 10000
572572
raw = self.__read(amount)

Lib/test/test_pyrepl/test_unix_console.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,30 @@ def test_repl_eio(self):
400400
),
401401
f"Expected EIO/ENXIO error message in stderr: {err}",
402402
)
403+
404+
405+
@unittest.skipIf(sys.platform == "win32", "No Unix console on Windows")
406+
class TestGetPending(TestCase):
407+
def test_getpending_accumulates_raw_from_queued_events(self):
408+
# gh-145886: getpending was adding e.raw to itself instead of e2.raw
409+
console = UnixConsole.__new__(UnixConsole)
410+
console.encoding = "utf-8"
411+
console.input_fd = 0
412+
413+
ev1 = Event("key", "a", b"x")
414+
ev2 = Event("key", "b", b"y")
415+
queue = [ev1, ev2]
416+
417+
mock_eq = MagicMock()
418+
mock_eq.empty = lambda: len(queue) == 0
419+
mock_eq.get = lambda: queue.pop(0)
420+
console.event_queue = mock_eq
421+
422+
# Mock __read to return empty bytes (no additional pending input)
423+
console._UnixConsole__read = lambda n: b""
424+
425+
with patch("_pyrepl.unix_console.ioctl", return_value=b"\0\0\0\0"):
426+
result = console.getpending()
427+
428+
self.assertEqual(result.data, "ab")
429+
self.assertEqual(result.raw, b"xy")

0 commit comments

Comments
 (0)