Split exceptions.
This commit is contained in:
@@ -38,8 +38,13 @@ cdef extern from "pack.h":
|
|||||||
cdef int DEFAULT_RECURSE_LIMIT=511
|
cdef int DEFAULT_RECURSE_LIMIT=511
|
||||||
|
|
||||||
|
|
||||||
class BufferFull(Exception):
|
from msgpack.exceptions import (
|
||||||
pass
|
UnpackException,
|
||||||
|
BufferFull,
|
||||||
|
OutOfData,
|
||||||
|
UnpackValueError,
|
||||||
|
ExtraData,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
cdef class Packer(object):
|
cdef class Packer(object):
|
||||||
@@ -102,7 +107,7 @@ cdef class Packer(object):
|
|||||||
cdef dict d
|
cdef dict d
|
||||||
|
|
||||||
if nest_limit < 0:
|
if nest_limit < 0:
|
||||||
raise ValueError("Too deep.")
|
raise UnpackValueError("recursion limit exceeded.")
|
||||||
|
|
||||||
if o is None:
|
if o is None:
|
||||||
ret = msgpack_pack_nil(&self.pk)
|
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:
|
if ret == 1:
|
||||||
obj = template_data(&ctx)
|
obj = template_data(&ctx)
|
||||||
if off < buf_len:
|
if off < buf_len:
|
||||||
raise ValueError("Extra data.")
|
raise ExtraData(obj, PyBytes_FromStringAndSize(buf+off, buf_len-off))
|
||||||
return obj
|
return obj
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
@@ -425,7 +430,7 @@ cdef class Unpacker(object):
|
|||||||
cdef Py_ssize_t buf_len
|
cdef Py_ssize_t buf_len
|
||||||
if self.file_like is not None:
|
if self.file_like is not None:
|
||||||
raise AssertionError(
|
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, <const_void_ptr*>&buf, &buf_len)
|
PyObject_AsReadBuffer(next_bytes, <const_void_ptr*>&buf, &buf_len)
|
||||||
self.append_buffer(buf, buf_len)
|
self.append_buffer(buf, buf_len)
|
||||||
|
|
||||||
@@ -479,7 +484,7 @@ cdef class Unpacker(object):
|
|||||||
else:
|
else:
|
||||||
self.file_like = None
|
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 int ret
|
||||||
cdef object obj
|
cdef object obj
|
||||||
cdef size_t prev_head
|
cdef size_t prev_head
|
||||||
@@ -497,7 +502,10 @@ cdef class Unpacker(object):
|
|||||||
if self.file_like is not None:
|
if self.file_like is not None:
|
||||||
self.read_from_file()
|
self.read_from_file()
|
||||||
continue
|
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:
|
else:
|
||||||
raise ValueError("Unpack failed: error = %d" % (ret,))
|
raise ValueError("Unpack failed: error = %d" % (ret,))
|
||||||
|
|
||||||
@@ -539,7 +547,7 @@ cdef class Unpacker(object):
|
|||||||
return self
|
return self
|
||||||
|
|
||||||
def __next__(self):
|
def __next__(self):
|
||||||
return self._unpack(template_construct, None)
|
return self._unpack(template_construct, None, 1)
|
||||||
|
|
||||||
# for debug.
|
# for debug.
|
||||||
#def _buf(self):
|
#def _buf(self):
|
||||||
|
|||||||
23
msgpack/exceptions.py
Normal file
23
msgpack/exceptions.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
class UnpackException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class BufferFull(UnpackException):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class OutOfData(UnpackException):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class UnpackValueError(UnpackException, ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ExtraData(ValueError):
|
||||||
|
def __init__(self, unpacked, extra):
|
||||||
|
self.unpacked = unpacked
|
||||||
|
self.extra = extra
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "unpack(b) recieved extra data."
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
"""Test Unpacker's read_array_header and read_map_header methods"""
|
"""Test Unpacker's read_array_header and read_map_header methods"""
|
||||||
from msgpack import packb, Unpacker
|
from msgpack import packb, Unpacker, OutOfData
|
||||||
UnexpectedTypeException = ValueError
|
UnexpectedTypeException = ValueError
|
||||||
|
|
||||||
def test_read_array_header():
|
def test_read_array_header():
|
||||||
@@ -12,7 +12,7 @@ def test_read_array_header():
|
|||||||
try:
|
try:
|
||||||
unpacker.unpack()
|
unpacker.unpack()
|
||||||
assert 0, 'should raise exception'
|
assert 0, 'should raise exception'
|
||||||
except StopIteration:
|
except OutOfData:
|
||||||
assert 1, 'okay'
|
assert 1, 'okay'
|
||||||
|
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ def test_read_map_header():
|
|||||||
try:
|
try:
|
||||||
unpacker.unpack()
|
unpacker.unpack()
|
||||||
assert 0, 'should raise exception'
|
assert 0, 'should raise exception'
|
||||||
except StopIteration:
|
except OutOfData:
|
||||||
assert 1, 'okay'
|
assert 1, 'okay'
|
||||||
|
|
||||||
def test_incorrect_type_array():
|
def test_incorrect_type_array():
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
import six
|
import six
|
||||||
from msgpack import Unpacker, BufferFull
|
from msgpack import Unpacker, BufferFull
|
||||||
|
from msgpack.exceptions import OutOfData
|
||||||
import nose
|
import nose
|
||||||
|
|
||||||
def test_foobar():
|
def test_foobar():
|
||||||
@@ -17,7 +18,7 @@ def test_foobar():
|
|||||||
try:
|
try:
|
||||||
o = unpacker.unpack()
|
o = unpacker.unpack()
|
||||||
assert 0, "should raise exception"
|
assert 0, "should raise exception"
|
||||||
except StopIteration:
|
except OutOfData:
|
||||||
assert 1, "ok"
|
assert 1, "ok"
|
||||||
|
|
||||||
unpacker.feed(b'foo')
|
unpacker.feed(b'foo')
|
||||||
@@ -41,7 +42,7 @@ def test_foobar_skip():
|
|||||||
try:
|
try:
|
||||||
o = unpacker.unpack()
|
o = unpacker.unpack()
|
||||||
assert 0, "should raise exception"
|
assert 0, "should raise exception"
|
||||||
except StopIteration:
|
except OutOfData:
|
||||||
assert 1, "ok"
|
assert 1, "ok"
|
||||||
|
|
||||||
def test_maxbuffersize():
|
def test_maxbuffersize():
|
||||||
|
|||||||
Reference in New Issue
Block a user