8000 msgpack pack and unpack throws only exception that inherit from Msgpa… · sugarguo/msgpack-python@7d2d46e · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d2d46e

Browse files
committed
msgpack pack and unpack throws only exception that inherit from MsgpackBaseException. cython and fallback throws same exceptions
1 parent 82b3121 commit 7d2d46e

File tree

4 files changed

+71
-52
lines changed

4 files changed

+71
-52
lines changed

msgpack/_packer.pyx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from libc.stdlib cimport *
66
from libc.string cimport *
77
from libc.limits cimport *
88

9-
from msgpack.exceptions import PackValueError
9+
from msgpack.exceptions import PackValueError, PackOverflowError
1010
from msgpack import ExtType
1111

1212

@@ -166,7 +166,7 @@ cdef class Packer(object):
166166
default_used = True
167167
continue
168168
else:
169-
raise
169+
raise PackOverflowError("Integer value out of range")
170170
elif PyInt_CheckExact(o) if strict_types else PyInt_Check(o):
171171
longval = o
172172
ret = msgpack_pack_long(&self.pk, longval)
@@ -180,7 +180,7 @@ cdef class Packer(object):
180180
elif PyBytes_CheckExact(o) if strict_types else PyBytes_Check(o):
181181
L = len(o)
182182
if L > (2**32)-1:
183-
raise ValueError("bytes is too large")
183+
raise PackValueError("bytes is too large")
184184
rawval = o
185185
ret = msgpack_pack_bin(&self.pk, L)
186186
if ret == 0:
@@ -191,7 +191,7 @@ cdef class Packer(object):
191191
o = PyUnicode_AsEncodedString(o, self.encoding, self.unicode_errors)
192192
L = len(o)
193193
if L > (2**32)-1:
194-
raise ValueError("unicode string is too large")
194+
raise PackV B41A alueError("unicode string is too large")
195195
rawval = o
196196
ret = msgpack_pack_raw(&self.pk, L)
197197
if ret == 0:
@@ -211,7 +211,7 @@ cdef class Packer(object):
211211
elif not strict_types and PyDict_Check(o):
212212
L = len(o)
213213
if L > (2**32)-1:
214-
raise ValueError("dict is too large")
214+
raise PackValueError("dict is too large")
215215
ret = msgpack_pack_map(&self.pk, L)
216216
if ret == 0:
217217
for k, v in o.items():
@@ -225,13 +225,13 @@ cdef class Packer(object):
225225
rawval = o.data
226226
L = len(o.data)
227227
if L > (2**32)-1:
228-
raise ValueError("EXT data is too large")
228+
raise PackValueError("EXT data is too large")
229229
ret = msgpack_pack_ext(&self.pk, longval, L)
230230
ret = msgpack_pack_raw_body(&self.pk, rawval, L)
231231
elif PyList_CheckExact(o) if strict_types else (PyTuple_Check(o) or PyList_Check(o)):
232232
L = len(o)
233233
if L > (2**32)-1:
234-
raise ValueError("list is too large")
234+
raise PackValueError("list is too large")
235235
ret = msgpack_pack_array(&self.pk, L)
236236
if ret == 0:
237237
for v in o:

msgpack/exceptions.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
class UnpackException(Exception):
1+
class MsgpackBaseException(Exception):
2+
pass
3+
4+
5+
class UnpackException(MsgpackBaseException):
26
pass
37

48

@@ -22,8 +26,12 @@ def __init__(self, unpacked, extra):
2226
def __str__(self):
2327
return "unpack(b) received extra data."
2428

25-
class PackException(Exception):
29+
class PackException(MsgpackBaseException):
2630
pass
2731

2832
class PackValueError(PackException, ValueError):
2933
pass
34+
35+
36+
class PackOverflowError(PackValueError, OverflowError):
37+
pass

msgpack/fallback.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def getvalue(self):
5151
OutOfData,
5252
UnpackValueError,
5353
PackValueError,
54+
PackOverflowError,
5455
ExtraData)
5556

5657
from msgpack import ExtType
@@ -363,17 +364,17 @@ def _read_header(self, execute=EX_CONSTRUCT, write_bytes=None):
363364
obj = self._fb_read(n, write_bytes)
364365
typ = TYPE_RAW
365366
if n > self._max_str_len:
366-
raise ValueError("%s exceeds max_str_len(%s)", n, self._max_str_len)
367+
raise UnpackValueError("%s exceeds max_str_len(%s)", n, self._max_str_len)
367368
elif b & 0b11110000 == 0b10010000:
10000
368369
n = b & 0b00001111
369370
typ = TYPE_ARRAY
370371
if n > self._max_array_len:
371-
raise ValueError("%s exceeds max_array_len(%s)", n, self._max_array_len)
372+
raise UnpackValueError("%s exceeds max_array_len(%s)", n, self._max_array_len)
372373
elif b & 0b11110000 == 0b10000000:
373374
n = b & 0b00001111
374375
typ = TYPE_MAP
375376
if n > self._max_map_len:
376-
raise ValueError("%s exceeds max_map_len(%s)", n, self._max_map_len)
377+
raise UnpackValueError("%s exceeds max_map_len(%s)", n, self._max_map_len)
377378
elif b == 0xc0:
378379
obj = None
379380
elif b == 0xc2:
@@ -384,37 +385,37 @@ def _read_header(self, execute=EX_CONSTRUCT, write_bytes=None):
384385
typ = TYPE_BIN
385386
n = struct.unpack("B", self._fb_read(1, write_bytes))[0]
386387
if n > self._max_bin_len:
387-
raise ValueError("%s exceeds max_bin_len(%s)" % (n, self._max_bin_len))
388+
raise UnpackValueError("%s exceeds max_bin_len(%s)" % (n, self._max_bin_len))
388389
obj = self._fb_read(n, write_bytes)
389390
elif b == 0xc5:
390391
typ = TYPE_BIN
391392
n = struct.unpack(">H", self._fb_read(2, write_bytes))[0]
392393
if n > self._max_bin_len:
393-
raise ValueError("%s exceeds max_bin_len(%s)" % (n, self._max_bin_len))
394+
raise UnpackValueError("%s exceeds max_bin_len(%s)" % (n, self._max_bin_len))
394395
obj = self._fb_read(n, write_bytes)
395396
elif b == 0xc6:
396397
typ = TYPE_BIN
397398
n = struct.unpack(">I", self._fb_read(4, write_bytes))[0]
398399
if n > self._max_bin_len:
399-
raise ValueError("%s exceeds max_bin_len(%s)" % (n, self._max_bin_len))
400+
raise UnpackValueError("%s exceeds max_bin_len(%s)" % (n, self._max_bin_len))
400401
obj = self._fb_read(n, write_bytes)
401402
elif b == 0xc7: # ext 8
402403
typ = TYPE_EXT
403404
L, n = struct.unpack('Bb', self._fb_read(2, write_bytes))
404405
if L > self._max_ext_len:
405-
raise ValueError("%s exceeds max_ext_len(%s)" % (L, self._max_ext_len))
406+
raise UnpackValueError("%s exceeds max_ext_len(%s)" % (L, self._max_ext_len))
406407
obj = self._fb_read(L, write_bytes)
407408
elif b == 0xc8: # ext 16
408409
typ = TYPE_EXT
409410
L, n = struct.unpack('>Hb', self._fb_read(3, write_bytes))
410411
if L > self._max_ext_len:
411-
raise ValueError("%s exceeds max_ext_len(%s)" % (L, self._max_ext_len))
412+
raise UnpackValueError("%s exceeds max_ext_len(%s)" % (L, self._max_ext_len))
412413
obj = self._fb_read(L, write_bytes)
413414
elif b == 0xc9: # ext 32
414415
typ = TYPE_EXT
415416
L, n = struct.unpack('>Ib', self._fb_read(5, write_bytes))
416417
if L > self._max_ext_len:
417-
raise ValueError("%s exceeds max_ext_len(%s)" % (L, self._max_ext_len))
418+
raise UnpackValueError("%s exceeds max_ext_len(%s)" % (L, self._max_ext_len))
418419
obj = self._fb_read(L, write_bytes)
419420
elif b == 0xca:
420421
obj = struct.unpack(">f", self._fb_read(4, write_bytes))[0]
@@ -439,65 +440,65 @@ def _read_header(self, execute=EX_CONSTRUCT, write_bytes=None):
439440
elif b == 0xd4: # fixext 1
440441
typ = TYPE_EXT
441442
if self._max_ext_len < 1:
442-
raise ValueError("%s exceeds max_ext_len(%s)" % (1, self._max_ext_len))
443+
raise UnpackValueError("%s exceeds max_ext_len(%s)" % (1, self._max_ext_len))
443444
n, obj = struct.unpack('b1s', self._fb_read(2, write_bytes))
444445
elif b == 0xd5: # fixext 2
445446
typ = TYPE_EXT
446447
if self._max_ext_len < 2:
447-
raise ValueError("%s exceeds max_ext_len(%s)" % (2, self._max_ext_len))
448+
raise UnpackValueError("%s exceeds max_ext_len(%s)" % (2, self._max_ext_len))
448449
n, obj = struct.unpack('b2s', self._fb_read(3, write_bytes))
449450
elif b == 0xd6: # fixext 4
450451
typ = TYPE_EXT
451452
if self._max_ext_len < 4:
452-
raise ValueError("%s exceeds max_ext_len(%s)" % (4, self._max_ext_len))
453+
raise UnpackValueError("%s exceeds max_ext_len(%s)" % (4, self._max_ext_len))
453454
n, obj = struct.unpack('b4s', self._fb_read(5, write_bytes))
454455
elif b == 0xd7: # fixext 8
455456
typ = TYPE_EXT
456457
if self._max_ext_len < 8:
457-
raise ValueError("%s exceeds max_ext_len(%s)" % (8, self._max_ext_len))
458+
raise UnpackValueError("%s exceeds max_ext_len(%s)" % (8, self._max_ext_len))
458459
n, obj = struct.unpack('b8s', self._fb_read(9, write_bytes))
459460
elif b == 0xd8: # fixext 16
460461
typ = TYPE_EXT
461462
if self._max_ext_len < 16:
462-
raise ValueError("%s exceeds max_ext_len(%s)" % (16, self._max_ext_len))
463+
raise UnpackValueError("%s exceeds max_ext_len(%s)" % (16, self._max_ext_len))
463464
n, obj = struct.unpack('b16s', self._fb_read(17, write_bytes))
464465
elif b == 0xd9:
465466
typ = TYPE_RAW
466467
n = struct.unpack("B", self._fb_read(1, write_bytes))[0]
467468
if n > self._max_str_len:
468-
raise ValueError("%s exceeds max_str_len(%s)", n, self._max_str_len)
469+
raise UnpackValueError("%s exceeds max_str_len(%s)", n, self._max_str_len)
469470
obj = self._fb_read(n, write_bytes)
470471
elif b == 0xda:
471472
typ = TYPE_RAW
472473
n = struct.unpack(">H", self._fb_read(2, write_bytes))[0]
473474
if n > self._max_str_len:
474-
raise ValueError("%s exceeds max_str_len(%s)", n, self._max_str_len)
475+
raise UnpackValueError("%s exceeds max_str_len(%s)", n, self._max_str_len)
475476
obj = self._fb_read(n, write_bytes)
476477
elif b == 0xdb:
477478
typ = TYPE_RAW
478479
n = struct.unpack(">I", self._fb_read(4, write_bytes))[0]
479480
if n > self._max_str_len:
480-
raise ValueError("%s exceeds max_str_len(%s)", n, self._max_str_len)
481+
raise UnpackValueError("%s exceeds max_str_len(%s)", n, self._max_str_len)
481482
obj = self._fb_read(n, write_bytes)
482483
elif b == 0xdc:
483484
n = struct.unpack(">H", self._fb_read(2, write_bytes))[0]
484485
if n > self._max_array_len:
485-
raise ValueError("%s exceeds max_array_len(%s)", n, self._max_array_len)
486+
raise UnpackValueError("%s exceeds max_array_len(%s)", n, self._max_array_len)
486487
typ = TYPE_ARRAY
487488
elif b == 0xdd:
488489
n = struct.unpack(">I", self._fb_read(4, write_bytes))[0]
489490
if n > self._max_array_len:
490-
raise ValueError("%s exceeds max_array_len(%s)", n, self._max_array_len)
491+
raise UnpackValueError("%s exceeds max_array_len(%s)", n, self._max_array_len)
491492
typ = TYPE_ARRAY
492493
elif b == 0xde:
493494
n = struct.unpack(">H", self._fb_read(2, write_bytes))[0]
494495
if n > self._max_map_len:
495-
raise ValueError("%s exceeds max_map_len(%s)", n, self._max_map_len)
496+
raise UnpackValueError("%s exceeds max_map_len(%s)", n, self._max_map_len)
496497
typ = TYPE_MAP
497498
elif b == 0xdf:
498499
n = struct.unpack(">I", self._fb_read(4, write_bytes))[0]
499500
if n > self._max_map_len:
500-
raise ValueError("%s exceeds max_map_len(%s)", n, self._max_map_len)
501+
raise UnpackValueError("%s exceeds max_map_len(%s)", n, self._max_map_len)
501502
typ = TYPE_MAP
502503
else:
503504
raise UnpackValueError("Unknown header: 0x%x" % b)
@@ -683,7 +684,7 @@ def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT,
683684
obj = self._default(obj)
684685
default_used = True
685686
continue
686-
raise PackValueError("Integer value out of range")
687+
raise PackOverflowError("Integer value out of range")
687688
if self._use_bin_type and check(obj, (bytes, memoryview)):
688689
n = len(obj)
689690
if n <= 0xff:
@@ -778,7 +779,7 @@ def pack_map_pairs(self, pairs):
778779

779780
def pack_array_header(self, n):
780781
if n >= 2**32:
781-
raise ValueError
782+
raise PackValueError
782783
self._fb_pack_array_header(n)
783784
ret = self._buffer.getvalue()
784785
if self._autoreset:
@@ -789,7 +790,7 @@ def pack_array_header(self, n):
789790

790791
def pack_map_header(self, n):
791792
if n >= 2**32:
792-
raise ValueError
793+
raise PackValueError
793794
self._fb_pack_map_header(n)
794795
ret = self._buffer.getvalue()
795796
if self._autoreset:
@@ -807,7 +808,7 @@ def pack_ext_type(self, typecode, data):
807808
raise TypeError("data must have bytes type")
808809
L = len(data)
809810
if L > 0xffffffff:
810-
raise ValueError("Too large data")
811+
raise PackValueError("Too large data")
811812
if L == 1:
812813
self._buffer.write(b'\xd4')
813814
elif L == 2:

test/test_limits.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,42 @@
33
from __future__ import absolute_import, division, print_function, unicode_literals
44
import pytest
55

6-
from msgpack import packb, unpackb, Packer, Unpacker, ExtType
6+
from msgpack import packb, unpackb, Packer, Unpacker, ExtType, PackException, PackOverflowError, PackValueError
7+
from msgpack import UnpackValueError, UnpackException, MsgpackBaseException
78

89

9-
def test_integer():
10+
@pytest.mark.parametrize("expected_exception", [OverflowError, ValueError, PackOverflowError,
11+
PackException, PackValueError, MsgpackBaseException])
12+
def test_integer(expected_exception):
1013
x = -(2 ** 63)
1114
assert unpackb(packb(x)) == x
12-
with pytest.raises((OverflowError, ValueError)):
15+
with pytest.raises(expected_exception):
1316
packb(x-1)
1417

1518
x = 2 ** 64 - 1
1619
assert unpackb(packb(x)) == x
17-
with pytest.raises((OverflowError, ValueError)):
20+
with pytest.raises(expected_exception):
1821
packb(x+1)
1922

2023

21-
def test_array_header():
24+
@pytest.mark.parametrize("expected_exception", [ValueError, PackException, PackValueError, MsgpackBaseException])
25+
def test_array_header(expected_exception):
2226
packer = Packer()
2327
packer.pack_array_header(2**32-1)
24-
with pytest.raises((OverflowError, ValueError)):
28+
with pytest.raises(expected_exception):
2529
packer.pack_array_header(2**32)
2630

2731

28-
def test_map_header():
32+
@pytest.mark.parametrize("expected_exception", [ValueError, PackException, PackValueError, MsgpackBaseException])
33+
def test_map_header(expected_exception):
2934
packer = Packer()
3035
packer.pack_map_header(2**32-1)
31-
with pytest.raises((OverflowError, ValueError)):
36+
with pytest.raises(expected_exception):
3237
packer.pack_array_header(2**32)
3338

3439

35-
def test_max_str_len():
40+
@pytest.mark.parametrize("expected_exception", [ValueError, UnpackValueError, UnpackException, MsgpackBaseException])
41+
def test_max_str_len(expected_exception):
3642
d = 'x' * 3
3743
packed = packb(d)
3844

@@ -41,12 +47,13 @@ def test_max_str_len():
4147
assert unpacker.unpack() == d
4248

4349
unpacker = Unpacker(max_str_len=2, encoding='utf-8')
44-
with pytest.raises(ValueError):
50+
with pytest.raises(expected_exception):
4551
unpacker.feed(packed)
4652
unpacker.unpack()
4753

4854

49-
def test_max_bin_len():
55+
@pytest.mark.parametrize("expected_exception", [ValueError, UnpackValueError, UnpackException, MsgpackBaseException])
56+
def test_max_bin_len(expected_exception):
5057
d = b'x' * 3
5158
packed = packb(d, use_bin_type=True)
5259

@@ -55,12 +62,13 @@ def test_max_bin_len():
5562
assert unpacker.unpack() == d
5663

5764
unpacker = Unpacker(max_bin_len=2)
58-
with pytest.raises(ValueError):
65+
with pytest.raises(expected_exception):
5966
unpacker.feed(packed)
6067
unpacker.unpack()
6168

6269

63-
def test_max_array_len():
70+
@pytest.mark.parametrize("expected_exception", [ValueError, UnpackValueError, UnpackException, MsgpackBaseException])
71+
def test_max_array_len(expected_exception):
6472
d = [1,2,3]
6573
packed = packb(d)
6674

@@ -69,12 +77,13 @@ def test_max_array_len():
6977
assert unpacker.unpack() == d
7078

7179
unpacker = Unpacker(max_array_len=2)
72-
with pytest.raises(ValueError):
80+
with pytest.raises(expected_exception):
7381
unpacker.feed(packed)
7482
unpacker.unpack()
7583

7684

77-
def test_max_map_len():
85+
@pytest.mark.parametrize("expected_exception", [ValueError, UnpackValueError, UnpackException, MsgpackBaseException])
86+
def test_max_map_len(expected_exception):
7887
d = {1: 2, 3: 4, 5: 6}
7988
packed = packb(d)
8089

@@ -83,12 +92,13 @@ def test_max_map_len():
8392
assert unpacker.unpack() == d
8493

8594
unpacker = Unpacker(max_map_len=2)
86-
with pytest.raises(ValueError):
95+
with pytest.raises(expected_exception):
8796
unpacker.feed(packed)
8897
unpacker.unpack()
8998

9099

91-
def test_max_ext_len():
100+
@pytest.mark.parametrize("expected_exception", [ValueError, UnpackValueError, UnpackException, MsgpackBaseException])
101+
def test_max_ext_len(expected_exception):
92102
d = ExtType(42, b"abc")
93103
packed = packb(d)
94104

@@ -97,7 +107,7 @@ def test_max_ext_len():
97107
assert unpacker.unpack() == d
98108

99109
unpacker = Unpacker(max_ext_len=2)
100-
with pytest.raises(ValueError):
110+
with pytest.raises(expected_exception):
101111
unpacker.feed(packed)
102112
unpacker.unpack()
103113

0 commit comments

Comments
 (0)
0