Merge pull request #87 from msgpack/fix-83

Feed data from file before _unpack()
This commit is contained in:
INADA Naoki 2014-02-13 03:24:09 +09:00
commit 7b24d0fe5a
2 changed files with 29 additions and 0 deletions

View file

@ -327,8 +327,18 @@ cdef class Unpacker(object):
cdef int ret
cdef object obj
cdef size_t prev_head
if self.buf_head >= self.buf_tail and self.file_like is not None:
self.read_from_file()
while 1:
prev_head = self.buf_head
if prev_head >= self.buf_tail:
if iter:
raise StopIteration("No more data to unpack.")
else:
raise OutOfData("No more data to unpack.")
ret = execute(&self.ctx, self.buf, self.buf_tail, &self.buf_head)
if write_bytes is not None:
write_bytes(PyBytes_FromStringAndSize(self.buf + prev_head, self.buf_head - prev_head))

19
test/test_unpack_file.py Normal file
View file

@ -0,0 +1,19 @@
from io import BytesIO
from msgpack import Unpacker, packb, OutOfData
from pytest import raises
def test_unpack_array_header_from_file():
f = BytesIO(packb([1,2,3,4]))
unpacker = Unpacker(f)
assert unpacker.read_array_header() == 4
assert unpacker.unpack() == 1
assert unpacker.unpack() == 2
assert unpacker.unpack() == 3
assert unpacker.unpack() == 4
with raises(OutOfData):
unpacker.unpack()
if __name__ == '__main__':
test_unpack_array_header_from_file()