Skip to content

Commit ffec10d

Browse files
committed
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.
1 parent 5b66eda commit ffec10d

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

msgpack/_msgpack.pyx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,18 @@ cdef class Unpacker(object):
455455
else:
456456
raise ValueError("Unpack failed: error = %d" % (ret,))
457457

458+
def read_bytes(self, Py_ssize_t nbytes):
459+
"""read a specified number of raw bytes from the stream"""
460+
cdef size_t nread
461+
ret = ''
462+
while len(ret) < nbytes and self.file_like is not None:
463+
if self.buf_head == self.buf_tail:
464+
self.fill_buffer()
465+
nread = min(self.buf_tail - self.buf_head, nbytes - len(ret))
466+
ret += PyBytes_FromStringAndSize(self.buf + self.buf_head, nread)
467+
self.buf_head += nread
468+
return ret
469+
458470
def __iter__(self):
459471
return self
460472

0 commit comments

Comments
 (0)