summaryrefslogtreecommitdiff
path: root/msgpack/_msgpack.pyx
diff options
context:
space:
mode:
authorjnothman <jnothman@student.usyd.edu.au>2012-09-21 16:03:41 +1000
committerjnothman <jnothman@student.usyd.edu.au>2012-09-21 16:03:41 +1000
commitffec10dff3839ae182cff3d9fff67ab3fe6165be (patch)
tree1600277817efe8347e675fd970069eb441587e6f /msgpack/_msgpack.pyx
parent5b66edaa156c43793b6f68013a738f545885b8d6 (diff)
downloadmsgpack-python-ffec10dff3839ae182cff3d9fff67ab3fe6165be.tar.gz
Expose packed stream with Unpacker.read_bytes()
At present, Unpacker buffers reading from the stream, meaning the stream can no longer be read directly. Unpacker.read_bytes(n) provides access to the underlying data, allowing content of known size to be read without unpacking.
Diffstat (limited to 'msgpack/_msgpack.pyx')
-rw-r--r--msgpack/_msgpack.pyx12
1 files changed, 12 insertions, 0 deletions
diff --git a/msgpack/_msgpack.pyx b/msgpack/_msgpack.pyx
index c9f5e31..d7ea4b4 100644
--- a/msgpack/_msgpack.pyx
+++ b/msgpack/_msgpack.pyx
@@ -455,6 +455,18 @@ cdef class Unpacker(object):
else:
raise ValueError("Unpack failed: error = %d" % (ret,))
+ def read_bytes(self, Py_ssize_t nbytes):
+ """read a specified number of raw bytes from the stream"""
+ cdef size_t nread
+ ret = ''
+ while len(ret) < nbytes and self.file_like is not None:
+ if self.buf_head == self.buf_tail:
+ self.fill_buffer()
+ nread = min(self.buf_tail - self.buf_head, nbytes - len(ret))
+ ret += PyBytes_FromStringAndSize(self.buf + self.buf_head, nread)
+ self.buf_head += nread
+ return ret
+
def __iter__(self):
return self