From ffec10dff3839ae182cff3d9fff67ab3fe6165be Mon Sep 17 00:00:00 2001 From: jnothman Date: Fri, 21 Sep 2012 16:03:41 +1000 Subject: 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. --- msgpack/_msgpack.pyx | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'msgpack/_msgpack.pyx') 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 -- cgit v1.2.1