summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorINADA Naoki <inada-n@klab.com>2014-02-13 03:10:51 +0900
committerINADA Naoki <inada-n@klab.com>2014-02-13 03:10:51 +0900
commit9d61f243878eeabd2042bb16fe22d4325e441da6 (patch)
tree2364417fcb3e9f6aa0d5491f85b6e47aff979d7c
parentd2fc8010342512378e01322f8871c10a5974af4f (diff)
downloadmsgpack-python-9d61f243878eeabd2042bb16fe22d4325e441da6.tar.gz
Feed data from file before _unpack()
-rw-r--r--msgpack/_unpacker.pyx10
-rw-r--r--test/test_unpack_file.py19
2 files changed, 29 insertions, 0 deletions
diff --git a/msgpack/_unpacker.pyx b/msgpack/_unpacker.pyx
index 732adef..16aca5c 100644
--- a/msgpack/_unpacker.pyx
+++ b/msgpack/_unpacker.pyx
@@ -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))
diff --git a/test/test_unpack_file.py b/test/test_unpack_file.py
new file mode 100644
index 0000000..1563008
--- /dev/null
+++ b/test/test_unpack_file.py
@@ -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()