diff --git a/msgpack/fallback.py b/msgpack/fallback.py index 0cbd8733..da49c426 100644 --- a/msgpack/fallback.py +++ b/msgpack/fallback.py @@ -375,8 +375,10 @@ def _reserve(self, n, raise_outofdata=True): return if self._feeding: - self._buff_i = self._buf_checkpoint - raise OutOfData + if raise_outofdata: + self._buff_i = self._buf_checkpoint + raise OutOfData + return # Strip buffer before checkpoint before reading file. if self._buf_checkpoint > 0: diff --git a/test/test_sequnpack.py b/test/test_sequnpack.py index 904be42f..d85834f7 100644 --- a/test/test_sequnpack.py +++ b/test/test_sequnpack.py @@ -1,7 +1,7 @@ #!/usr/bin/env python import io -from pytest import raises +from pytest import mark, raises from msgpack import BufferFull, Unpacker, pack, packb from msgpack.exceptions import OutOfData @@ -169,3 +169,18 @@ def test_unpack_tell(): m2 = next(unpacker) assert m == m2 assert o == unpacker.tell() + + +@mark.parametrize("data", [b"", b"ab"]) +def test_readbytes_short_feed(data): + unpacker = Unpacker() + unpacker.feed(packb(42) + data) + assert unpacker.unpack() == 42 + assert unpacker.read_bytes(5) == data + assert unpacker.tell() == 1 + len(data) + assert unpacker.read_bytes(5) == b"" + assert unpacker.tell() == 1 + len(data) + + unpacker.feed(packb("next")) + assert unpacker.unpack() == "next" + assert unpacker.tell() == 1 + len(data) + len(packb("next"))