8000 Remove deprecated exception classes (#323) · loude/msgpack-python@07f0bee · GitHub
[go: up one dir, main page]

Skip to content

Commit 07f0bee

Browse files
authored
Remove deprecated exception classes (msgpack#323)
1 parent 1bf62ba commit 07f0bee

File tree

4 files changed

+85
-96
lines changed

4 files changed

+85
-96
lines changed

msgpack/_packer.pyx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ from cpython cimport *
55
from cpython.version cimport PY_MAJOR_VERSION
66
from cpython.exc cimport PyErr_WarnEx
77

8-
from msgpack.exceptions import PackValueError, PackOverflowError
98
from msgpack import ExtType
109

1110

@@ -165,7 +164,7 @@ cdef class Packer(object):
165164
cdef Py_buffer view
166165

167166
if nest_limit < 0:
168-
raise PackValueError("recursion limit exceeded.")
167+
raise ValueError("recursion limit exceeded.")
169168

170169
while True:
171170
if o is None:
@@ -191,7 +190,7 @@ cdef class Packer(object):
191190
default_used = True
192191
continue
193192
else:
194-
raise PackOverflowError("Integer value out of range")
193+
raise OverflowError("Integer value out of range")
195194
elif PyInt_CheckExact(o) if strict_types else PyInt_Check(o):
196195
longval = o
197196
ret = msgpack_pack_long(&self.pk, longval)
@@ -205,7 +204,7 @@ cdef class Packer(object):
205204
elif PyBytesLike_CheckExact(o) if strict_types else PyBytesLike_Check(o):
206205
L = len(o)
207206
if L > ITEM_LIMIT:
208-
raise PackValueError("%s is too large" % type(o).__name__)
207+
raise ValueError("%s is too large" % type(o).__name__)
209208
rawval = o
210209
ret = msgpack_pack_bin(&self.pk, L)
211210
if ret == 0:
@@ -214,12 +213,12 @@ cdef class Packer(object):
214213
if self.encoding == NULL and self.unicode_errors == NULL:
215214
ret = msgpack_pack_unicode(&self.pk, o, ITEM_LIMIT);
216215
if ret == -2:
217-
raise PackValueError("unicode string is too large")
216+
raise ValueError("unicode string is too large")
218217
else:
219218
o = PyUnicode_AsEncodedString(o, self.encoding, self.unicode_errors)
220219
L = len(o)
221220
if L > ITEM_LIMIT:
222-
raise PackValueError("unicode string is too large")
221+
raise ValueError("unicode string is too large")
223222
ret = msgpack_pack_raw(&self.pk, L)
224223
if ret == 0:
225224
rawval = o
@@ -228,7 +227,7 @@ cdef class Packer(object):
228227
d = <dict>o
229228
L = len(d)
230229
if L > ITEM_LIMIT:
231-
raise PackValueError("dict is too large")
230+
raise ValueError("dict is too large")
232231
ret = msgpack_pack_map(&self.pk, L)
233232
if ret == 0:
234233
for k, v in d.iteritems():
@@ -239,7 +238,7 @@ cdef class Packer(object):
239238
elif not strict_types and PyDict_Check(o):
240239
L = len(o)
241240
if L > ITEM_LIMIT:
242-
raise PackValueError("dict is too large")
241+
raise ValueError("dict is too large")
243242
ret = msgpack_pack_map(&self.pk, L)
244243
if ret == 0:
245244
for k, v in o.items():
@@ -253,25 +252,25 @@ cdef class Packer(object):
253252
rawval = o.data
254253
L = len(o.data)
255254
if L > ITEM_LIMIT:
256-
raise PackValueError("EXT data is too large")
255+
raise ValueError("EXT data is too large")
257256
ret = msgpack_pack_ext(&self.pk, longval, L)
258257
ret = msgpack_pack_raw_body(&self.pk, rawval, L)
259258
elif PyList_CheckExact(o) if strict_types else (PyTuple_Check(o) or PyList_Check(o)):
260259
L = len(o)
261260
if L > ITEM_LIMIT:
262-
raise PackValueError("list is too large")
261+
raise ValueError("list is too large")
263262
ret = msgpack_pack_array(&self.pk, L)
264263
if ret == 0:
265264
for v in o:
266265
ret = self._pack(v, nest_limit-1)
267266
if ret != 0: break
268267
elif PyMemoryView_Check(o):
269268
if PyObject_GetBuffer(o, &view, PyBUF_SIMPLE) != 0:
270-
raise PackValueError("could not get buffer for memoryview")
269+
raise ValueError("could not get buffer for memoryview")
271270
L = view.len
272271
if L > ITEM_LIMIT:
273272
PyBuffer_Release(&view);
274-
raise PackValueError("memoryview is too large")
273+
raise ValueError("memoryview is too large")
275274
ret = msgpack_pack_bin(&self.pk, L)
276275
if ret == 0:
277276
ret = msgpack_pack_raw_body(&self.pk, <char*>view.buf, L)
@@ -304,7 +303,7 @@ cdef class Packer(object):
304303

305304
def pack_array_header(self, long long size):
306305
if size > ITEM_LIMIT:
307-
raise PackValueError
306+
raise ValueError
308307
cdef int ret = msgpack_pack_array(&self.pk, size)
309308
if ret == -1:
310309
raise MemoryError
@@ -317,7 +316,7 @@ cdef class Packer(object):
317316

318317
def pack_map_header(self, long long size):
319318
if size > ITEM_LIMIT:
320-
raise PackValueError
319+
raise ValueError
321320
cdef int ret = msgpack_pack_map(&self.pk, size)
322321
if ret == -1:
323322
raise MemoryError

msgpack/_unpacker.pyx

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ ctypedef unsigned long long uint64_t
3535
from msgpack.exceptions import (
3636
BufferFull,
3737
OutOfData,
38-
UnpackValueError,
3938
ExtraData,
4039
)
4140
from msgpack import ExtType
@@ -208,7 +207,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
208207
raise ExtraData(obj, PyBytes_FromStringAndSize(buf+off, buf_len-off))
209208
return obj
210209
unpack_clear(&ctx)
211-
raise UnpackValueError("Unpack failed: error = %d" % (ret,))
210+
raise ValueError("Unpack failed: error = %d" % (ret,))
212211

213212

214213
def unpack(object stream, **kwargs):
@@ -460,28 +459,25 @@ cdef class Unpacker(object):
460459
else:
461460
raise OutOfData("No more data to unpack.")
462461

463-
try:
464-
ret = execute(&self.ctx, self.buf, self.buf_tail, &self.buf_head)
465-
self.stream_offset += self.buf_head - prev_head
466-
if write_bytes is not None:
467-
write_bytes(PyBytes_FromStringAndSize(self.buf + prev_head, self.buf_head - prev_head))
468-
469-
if ret == 1:
470-
obj = unpack_data(&self.ctx)
471-
unpack_init(&self.ctx)
472-
return obj
473-
elif ret == 0:
474-
if self.file_like is not None:
475-
self.read_from_file()
476-
continue
477-
if iter:
478-
raise StopIteration("No more data to unpack.")
479-
else:
480-
raise OutOfData("No more data to unpack.")
462+
ret = execute(&self.ctx, self.buf, self.buf_tail, &self.buf_head)
463+
self.stream_offset += self.buf_head - prev_head
464+
if write_bytes is not None:
465+
write_bytes(PyBytes_FromStringAndSize(self.buf + prev_head, self.buf_head - prev_head))
466+
467+
if ret == 1:
468+
obj = unpack_data(&self.ctx)
469+
unpack_init(&self.ctx)
470+
return obj
471+
elif ret == 0:
472+
if self.file_like is not None:
473+
self.read_from_file()
474+
continue
475+
if iter:
476+
raise StopIteration("No more data to unpack.")
481477
else:
482-
raise UnpackValueError("Unpack failed: error = %d" % (ret,))
483-
except ValueError as e:
484-
raise UnpackValueError(e)
478+
raise OutOfData("No more data to unpack.")
479+
else:
480+
raise ValueError("Unpack failed: error = %d" % (ret,))
485481

486482
def read_bytes(self, Py_ssize_t nbytes):
487483
"""Read a specified number of raw bytes from the stream"""

msgpack/exceptions.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
class UnpackException(Exception):
2-
"""Deprecated. Use Exception instead to catch all exception during unpacking."""
2+
"""Base class for some exceptions raised while unpacking.
33
4+
NOTE: unpack may raise exception other than subclass of
5+
UnpackException. If you want to catch all error, catch
6+
Exception instead.
7+
"""
48

59
class BufferFull(UnpackException):
610
pass
@@ -10,11 +14,16 @@ class OutOfData(UnpackException):
1014
pass
1115

1216

13-
class UnpackValueError(UnpackException, ValueError):
14-
"""Deprecated. Use ValueError instead."""
17+
# Deprecated. Use ValueError instead
18+
UnpackValueError = ValueError
1519

1620

1721
class ExtraData(UnpackValueError):
22+
"""ExtraData is raised when there is trailing data.
23+
24+
This exception is raised while only one-shot (not streaming)
25+
unpack.
26+
"""
1827
def __init__(self, unpacked, extra):
1928
self.unpacked = unpacked
2029
self.extra = extra
@@ -23,19 +32,7 @@ def __str__(self):
2332
return "unpack(b) received extra data."
2433

2534

26-
class PackException(Exception):
27-
"""Deprecated. Use Exception instead to catch all exception during packing."""
28-
29-
30-
class PackValueError(PackException, ValueError):
31-
"""PackValueError is raised when type of input data is supported but it's value is unsupported.
32-
33-
Deprecated. Use ValueError instead.
34-
"""
35-
36-
37-
class PackOverflowError(PackValueError, OverflowError):
38-
"""PackOverflowError is raised when integer value is out of range of msgpack support [-2**31, 2**32).
39-
40-
Deprecated. Use ValueError instead.
41-
"""
35+
#Deprecated. Use Exception instead to catch all exception during packing.
36+
PackException = Exception
37+
PackValueError = ValueError
38+
PackOverflowError = OverflowError

0 commit comments

Comments
 (0)
0