fix: unreader.unread() now prepends data to buffer

The unread method was incorrectly appending data to the end of the
buffer instead of prepending it to the beginning. This caused issues
when reading partial data and then unreading it.

Closes #2915
Closes #2346
This commit is contained in:
Benoit Chesneau 2026-01-23 11:00:09 +01:00
parent 8e75b3aba3
commit 56abeaf105
2 changed files with 11 additions and 1 deletions

View File

@ -49,8 +49,10 @@ class Unreader:
return data[:size]
def unread(self, data):
self.buf.seek(0, os.SEEK_END)
rest = self.buf.getvalue()
self.buf = io.BytesIO()
self.buf.write(data)
self.buf.write(rest)
class SocketUnreader(Unreader):

View File

@ -137,6 +137,14 @@ def test_unreader_unread():
assert b'hi there' in unreader.read()
def test_unreader_unread_should_place_data_at_the_beginning_of_the_buffer():
unreader = IterUnreader([b"abc", b"def"])
ab = unreader.read(2)
unreader.unread(ab)
assert unreader.read(None) == b"abc"
def test_unreader_read_zero_size():
unreader = Unreader()
unreader.chunk = mock.MagicMock(side_effect=[b'qwerty', b'asdfgh'])