8000 Split exceptions. · xunzhang/msgpack-python@219d475 · GitHub
[go: up one dir, main page]

Skip to content

Commit 219d475

Browse files
committed
Split exceptions.
1 parent dd5c76b commit 219d475

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

msgpack/_msgpack.pyx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ cdef extern from "pack.h":
3838
cdef int DEFAULT_RECURSE_LIMIT=511
3939

4040

41-
class BufferFull(Exception):
42-
pass
41+
from msgpack.exceptions import (
42+
UnpackException,
43+
BufferFull,
44+
OutOfData,
45+
UnpackValueError,
46+
ExtraData,
47+
)
4348

4449

4550
cdef class Packer(object):
@@ -102,7 +107,7 @@ cdef class Packer(object):
102107
cdef dict d
103108

104109
if nest_limit < 0:
105-
raise ValueError("Too deep.")
110+
raise UnpackValueError("recursion limit exceeded.")
106111

107112
if o is None:
108113
ret = msgpack_pack_nil(&self.pk)
@@ -296,7 +301,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
296301
if ret == 1:
297302
obj = template_data(&ctx)
298303
if off < buf_len:
299-
raise ValueError("Extra data.")
304+
raise ExtraData(obj, PyBytes_FromStringAndSize(buf+off, buf_len-off))
300305
return obj
301306
else:
302307
return None
@@ -425,7 +430,7 @@ cdef class Unpacker(object):
425430
cdef Py_ssize_t buf_len
426431
if self.file_like is not None:
427432
raise AssertionError(
428-
"unpacker.feed() is not be able to use with`file_like`.")
433+
"unpacker.feed() is not be able to use with `file_like`.")
429434
PyObject_AsReadBuffer(next_bytes, <const_void_ptr*>&buf, &buf_len)
430435
self.append_buffer(buf, buf_len)
431436

@@ -479,7 +484,7 @@ cdef class Unpacker(object):
479484
else:
480485
self.file_like = None
481486

482-
cdef object _unpack(self, execute_fn execute, object write_bytes):
487+
cdef object _unpack(self, execute_fn execute, object write_bytes, bint iter=0):
483488
cdef int ret
484489
cdef object obj
485490
cdef size_t prev_head
@@ -497,7 +502,10 @@ cdef class Unpacker(object):
497502
if self.file_like is not None:
498503
self.read_from_file()
499504
continue
500-
raise StopIteration("No more data to unpack.")
505+
if iter:
506+
raise StopIteration("No more data to unpack.")
507+
else:
508+
raise OutOfData("No more data to unpack.")
501509
else:
502510
raise ValueError("Unpack failed: error = %d" % (ret,))
503511

@@ -539,7 +547,7 @@ cdef class Unpacker(object):
539547
return self
540548

541549
def __next__(self):
542-
return self._unpack(template_construct, None)
550+
return self._unpack(template_construct, None, 1)
543551

544552
# for debug.
545553
#def _buf(self):

msgpack/exceptions.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class UnpackException(Exception):
2+
pass
3+
4+
5+
class BufferFull(UnpackException):
6+
pass
7+
8+
9+
class OutOfData(UnpackException):
10+
pass
11+
12+
13+
class UnpackValueError(UnpackException, ValueError):
14+
pass
15+
16+
17+
class ExtraData(ValueError):
18+
def __init__(self, unpacked, extra):
19+
self.unpacked = unpacked
20+
self.extra = extra
21+
22+
def __str__(self):
23+
return "unpack(b) recieved extra data."

test/test_read_size.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Test Unpacker's read_array_header and read_map_header methods"""
2-
from msgpack import packb, Unpacker
2+
from msgpack import packb, Unpacker, OutOfData
33
UnexpectedTypeException = ValueError
44

55
def test_read_array_header():
@@ -12,7 +12,7 @@ def test_read_array_header():
1212
try:
1313
unpacker.unpack()
1414
assert 0, 'should raise exception'
15-
except StopIteration:
15+
except OutOfData:
1616
assert 1, 'okay'
1717

1818

@@ -25,7 +25,7 @@ def test_read_map_header():
2525
try:
2626
unpacker.unpack()
2727
assert 0, 'should raise exception'
28-
except StopIteration:
28+
except OutOfData:
2929
assert 1, 'okay'
3030

3131
def test_incorrect_type_array():

test/test_sequnpack.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import six
55
from msgpack import Unpacker, BufferFull
6+
from msgpack.exceptions import OutOfData
67
import nose
78

89
def test_foobar():
@@ -17,7 +18,7 @@ def test_foobar():
1718
try:
1819
o = unpacker.unpack()
1920
assert 0, "should raise exception"
20-
except StopIteration:
21+
except OutOfData:
2122
assert 1, "ok"
2223

2324
unpacker.feed(b'foo')
@@ -41,7 +42,7 @@ def test_foobar_skip():
4142
try:
4243
o = unpacker.unpack()
4344
assert 0, "should raise exception"
44-
except StopIteration:
45+
except OutOfData:
4546
assert 1, "ok"
4647

4748
def test_maxbuffersize():

0 commit comments

Comments
 (0)
0