From 219d47503ca982996b03f4396fdd2929c9905356 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 10 Dec 2012 00:31:19 +0900 Subject: Split exceptions. --- msgpack/_msgpack.pyx | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'msgpack/_msgpack.pyx') diff --git a/msgpack/_msgpack.pyx b/msgpack/_msgpack.pyx index 5c202fc..da70f0d 100644 --- a/msgpack/_msgpack.pyx +++ b/msgpack/_msgpack.pyx @@ -38,8 +38,13 @@ cdef extern from "pack.h": cdef int DEFAULT_RECURSE_LIMIT=511 -class BufferFull(Exception): - pass +from msgpack.exceptions import ( + UnpackException, + BufferFull, + OutOfData, + UnpackValueError, + ExtraData, + ) cdef class Packer(object): @@ -102,7 +107,7 @@ cdef class Packer(object): cdef dict d if nest_limit < 0: - raise ValueError("Too deep.") + raise UnpackValueError("recursion limit exceeded.") if o is None: ret = msgpack_pack_nil(&self.pk) @@ -296,7 +301,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None, if ret == 1: obj = template_data(&ctx) if off < buf_len: - raise ValueError("Extra data.") + raise ExtraData(obj, PyBytes_FromStringAndSize(buf+off, buf_len-off)) return obj else: return None @@ -425,7 +430,7 @@ cdef class Unpacker(object): cdef Py_ssize_t buf_len if self.file_like is not None: raise AssertionError( - "unpacker.feed() is not be able to use with`file_like`.") + "unpacker.feed() is not be able to use with `file_like`.") PyObject_AsReadBuffer(next_bytes, &buf, &buf_len) self.append_buffer(buf, buf_len) @@ -479,7 +484,7 @@ cdef class Unpacker(object): else: self.file_like = None - cdef object _unpack(self, execute_fn execute, object write_bytes): + cdef object _unpack(self, execute_fn execute, object write_bytes, bint iter=0): cdef int ret cdef object obj cdef size_t prev_head @@ -497,7 +502,10 @@ cdef class Unpacker(object): if self.file_like is not None: self.read_from_file() continue - raise StopIteration("No more data to unpack.") + if iter: + raise StopIteration("No more data to unpack.") + else: + raise OutOfData("No more data to unpack.") else: raise ValueError("Unpack failed: error = %d" % (ret,)) @@ -539,7 +547,7 @@ cdef class Unpacker(object): return self def __next__(self): - return self._unpack(template_construct, None) + return self._unpack(template_construct, None, 1) # for debug. #def _buf(self): -- cgit v1.2.1 From 4480227e066e66fd771dc1702adf4929dede453d Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 10 Dec 2012 00:39:04 +0900 Subject: Improve docstrings. --- msgpack/_msgpack.pyx | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'msgpack/_msgpack.pyx') diff --git a/msgpack/_msgpack.pyx b/msgpack/_msgpack.pyx index da70f0d..2c81cb4 100644 --- a/msgpack/_msgpack.pyx +++ b/msgpack/_msgpack.pyx @@ -426,6 +426,7 @@ cdef class Unpacker(object): init_ctx(&self.ctx, object_hook, object_pairs_hook, list_hook, use_list, cenc, cerr) def feed(self, object next_bytes): + """Append `next_bytes` to internal buffer.""" cdef char* buf cdef Py_ssize_t buf_len if self.file_like is not None: @@ -523,7 +524,11 @@ cdef class Unpacker(object): """ unpack one object - If write_bytes is not None, it will be called with parts of the raw message as it is unpacked. + If write_bytes is not None, it will be called with parts of the raw + message as it is unpacked. + + When there are not enough bytes for unpacking, `unpack()` raises + `OutOfData` Exception. """ return self._unpack(template_construct, write_bytes) @@ -531,16 +536,30 @@ cdef class Unpacker(object): """ read and ignore one object, returning None - If write_bytes is not None, it will be called with parts of the raw message as it is unpacked. + If write_bytes is not None, it will be called with parts of the raw + message as it is unpacked. + + When there are not enough bytes for unpacking, `unpack()` raises + `OutOfData` Exception. """ return self._unpack(template_skip, write_bytes) def read_array_header(self, object write_bytes=None): - """assuming the next object is an array, return its size n, such that the next n unpack() calls will iterate over its contents.""" + """assuming the next object is an array, return its size n, such that + the next n unpack() calls will iterate over its contents. + + When there are not enough bytes for unpacking, `unpack()` raises + `OutOfData` Exception. + """ return self._unpack(read_array_header, write_bytes) def read_map_header(self, object write_bytes=None): - """assuming the next object is a map, return its size n, such that the next n * 2 unpack() calls will iterate over its key-value pairs.""" + """assuming the next object is a map, return its size n, such that the + next n * 2 unpack() calls will iterate over its key-value pairs. + + When there are not enough bytes for unpacking, `unpack()` raises + `OutOfData` Exception. + """ return self._unpack(read_map_header, write_bytes) def __iter__(self): -- cgit v1.2.1 From ed40c671dac175e437fe67689053b5fe8a269ad3 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 10 Dec 2012 01:42:38 +0900 Subject: `pack` raise MemoryError when realloc is failed. --- msgpack/_msgpack.pyx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'msgpack/_msgpack.pyx') diff --git a/msgpack/_msgpack.pyx b/msgpack/_msgpack.pyx index 2c81cb4..b2b5222 100644 --- a/msgpack/_msgpack.pyx +++ b/msgpack/_msgpack.pyx @@ -179,7 +179,9 @@ cdef class Packer(object): cpdef pack(self, object obj): cdef int ret ret = self._pack(obj, DEFAULT_RECURSE_LIMIT) - if ret: + if ret == -1: + raise MemoryError + elif ret: # should not happen. raise TypeError buf = PyBytes_FromStringAndSize(self.pk.buf, self.pk.length) self.pk.length = 0 -- cgit v1.2.1 From 30025c7ea0a53e17a907ad35ffa56b7b7efd99d1 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 10 Dec 2012 20:06:00 +0900 Subject: Improve docstring. --- msgpack/_msgpack.pyx | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'msgpack/_msgpack.pyx') diff --git a/msgpack/_msgpack.pyx b/msgpack/_msgpack.pyx index b2b5222..74a4cc4 100644 --- a/msgpack/_msgpack.pyx +++ b/msgpack/_msgpack.pyx @@ -529,8 +529,7 @@ cdef class Unpacker(object): If write_bytes is not None, it will be called with parts of the raw message as it is unpacked. - When there are not enough bytes for unpacking, `unpack()` raises - `OutOfData` Exception. + Raises `OutOfData` when there are no more bytes to unpack. """ return self._unpack(template_construct, write_bytes) @@ -541,8 +540,7 @@ cdef class Unpacker(object): If write_bytes is not None, it will be called with parts of the raw message as it is unpacked. - When there are not enough bytes for unpacking, `unpack()` raises - `OutOfData` Exception. + Raises `OutOfData` when there are no more bytes to unpack. """ return self._unpack(template_skip, write_bytes) @@ -550,8 +548,7 @@ cdef class Unpacker(object): """assuming the next object is an array, return its size n, such that the next n unpack() calls will iterate over its contents. - When there are not enough bytes for unpacking, `unpack()` raises - `OutOfData` Exception. + Raises `OutOfData` when there are no more bytes to unpack. """ return self._unpack(read_array_header, write_bytes) @@ -559,8 +556,7 @@ cdef class Unpacker(object): """assuming the next object is a map, return its size n, such that the next n * 2 unpack() calls will iterate over its key-value pairs. - When there are not enough bytes for unpacking, `unpack()` raises - `OutOfData` Exception. + Raises `OutOfData` when there are no more bytes to unpack. """ return self._unpack(read_map_header, write_bytes) -- cgit v1.2.1 From 1c0fe10a2fd5a32cf3f558683695220f3718a4b8 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 10 Dec 2012 20:12:38 +0900 Subject: Remove unused UnpackException. --- msgpack/_msgpack.pyx | 1 - 1 file changed, 1 deletion(-) (limited to 'msgpack/_msgpack.pyx') diff --git a/msgpack/_msgpack.pyx b/msgpack/_msgpack.pyx index 74a4cc4..52d63cc 100644 --- a/msgpack/_msgpack.pyx +++ b/msgpack/_msgpack.pyx @@ -39,7 +39,6 @@ cdef int DEFAULT_RECURSE_LIMIT=511 from msgpack.exceptions import ( - UnpackException, BufferFull, OutOfData, UnpackValueError, -- cgit v1.2.1