diff options
author | Joel Nothman <joel.nothman@gmail.com> | 2012-09-23 11:13:44 +1000 |
---|---|---|
committer | Joel Nothman <joel.nothman@gmail.com> | 2012-09-23 20:46:49 +1000 |
commit | e7c51d9089e9270ce197c00a6af1c60e45f36e97 (patch) | |
tree | 8d5c683b5406bcdf7abc37d1a6e866e1413a7435 | |
parent | ffec10dff3839ae182cff3d9fff67ab3fe6165be (diff) | |
download | msgpack-python-e7c51d9089e9270ce197c00a6af1c60e45f36e97.tar.gz |
Cleaner read_bytes and a test case
No longer reads via buffer for unbuffered bytes
-rw-r--r-- | msgpack/_msgpack.pyx | 12 | ||||
-rw-r--r-- | test/test_sequnpack.py | 16 |
2 files changed, 21 insertions, 7 deletions
diff --git a/msgpack/_msgpack.pyx b/msgpack/_msgpack.pyx index d7ea4b4..30fb9fc 100644 --- a/msgpack/_msgpack.pyx +++ b/msgpack/_msgpack.pyx @@ -458,13 +458,11 @@ cdef class Unpacker(object): 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 + nread = min(self.buf_tail - self.buf_head, nbytes) + ret = PyBytes_FromStringAndSize(self.buf + self.buf_head, nread) + self.buf_head += nread + if len(ret) < nbytes and self.file_like is not None: + ret += self.file_like.read(nbytes - len(ret)) return ret def __iter__(self): diff --git a/test/test_sequnpack.py b/test/test_sequnpack.py index b1b80b2..c763f40 100644 --- a/test/test_sequnpack.py +++ b/test/test_sequnpack.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # coding: utf-8 +import six from msgpack import Unpacker, BufferFull import nose @@ -42,5 +43,20 @@ def test_maxbuffersize(): assert ord('b') == next(unpacker) +def test_readbytes(): + unpacker = Unpacker(read_size=3) + unpacker.feed(b'foobar') + assert unpacker.unpack() == ord(b'f') + assert unpacker.read_bytes(3) == b'oob' + assert unpacker.unpack() == ord(b'a') + assert unpacker.unpack() == ord(b'r') + + # Test buffer refill + unpacker = Unpacker(six.BytesIO(b'foobar'), read_size=3) + assert unpacker.unpack() == ord(b'f') + assert unpacker.read_bytes(3) == b'oob' + assert unpacker.unpack() == ord(b'a') + assert unpacker.unpack() == ord(b'r') + if __name__ == '__main__': nose.main() |