Skip to content

Commit f5fd6d3

Browse files
committed
fallback: return available bytes on short raw reads
1 parent 874b828 commit f5fd6d3

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

‎msgpack/fallback.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,10 @@ def _reserve(self, n, raise_outofdata=True):
375375
return
376376

377377
if self._feeding:
378-
self._buff_i = self._buf_checkpoint
379-
raise OutOfData
378+
if raise_outofdata:
379+
self._buff_i = self._buf_checkpoint
380+
raise OutOfData
381+
return
380382

381383
# Strip buffer before checkpoint before reading file.
382384
if self._buf_checkpoint > 0:

‎test/test_sequnpack.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
import io
33

4-
from pytest import raises
4+
from pytest import mark, raises
55

66
from msgpack import BufferFull, Unpacker, pack, packb
77
from msgpack.exceptions import OutOfData
@@ -169,3 +169,18 @@ def test_unpack_tell():
169169
m2 = next(unpacker)
170170
assert m == m2
171171
assert o == unpacker.tell()
172+
173+
174+
@mark.parametrize("data", [b"", b"ab"])
175+
def test_readbytes_short_feed(data):
176+
unpacker = Unpacker()
177+
unpacker.feed(packb(42) + data)
178+
assert unpacker.unpack() == 42
179+
assert unpacker.read_bytes(5) == data
180+
assert unpacker.tell() == 1 + len(data)
181+
assert unpacker.read_bytes(5) == b""
182+
assert unpacker.tell() == 1 + len(data)
183+
184+
unpacker.feed(packb("next"))
185+
assert unpacker.unpack() == "next"
186+
assert unpacker.tell() == 1 + len(data) + len(packb("next"))

0 commit comments

Comments
 (0)