Codebase list python-msgpack / da902f9
Move unpack() from each implementation to __init__. (#286) Fixes #285 INADA Naoki authored 6 years ago GitHub committed 6 years ago
3 changed file(s) with 14 addition(s) and 39 deletion(s). Raw diff Collapse all Expand all
1818
1919 import os
2020 if os.environ.get('MSGPACK_PUREPYTHON'):
21 from msgpack.fallback import Packer, unpack, unpackb, Unpacker
21 from msgpack.fallback import Packer, unpackb, Unpacker
2222 else:
2323 try:
2424 from msgpack._packer import Packer
25 from msgpack._unpacker import unpack, unpackb, Unpacker
25 from msgpack._unpacker import unpackb, Unpacker
2626 except ImportError:
27 from msgpack.fallback import Packer, unpack, unpackb, Unpacker
27 from msgpack.fallback import Packer, unpackb, Unpacker
2828
2929
3030 def pack(o, stream, **kwargs):
4545 """
4646 return Packer(**kwargs).pack(o)
4747
48
49 def unpack(stream, **kwargs):
50 """
51 Unpack an object from `stream`.
52
53 Raises `ExtraData` when `packed` contains extra bytes.
54 See :class:`Unpacker` for options.
55 """
56 return unpackb(stream.read(), **kwargs)
57
58
4859 # alias for compatibility to simplejson/marshal/pickle.
4960 load = unpack
5061 loads = unpackb
210210 raise UnpackValueError("Unpack failed: error = %d" % (ret,))
211211
212212
213 def unpack(object stream, object object_hook=None, object list_hook=None,
214 bint use_list=1, encoding=None, unicode_errors=None,
215 object_pairs_hook=None, ext_hook=ExtType,
216 Py_ssize_t max_str_len=2147483647, # 2**32-1
217 Py_ssize_t max_bin_len=2147483647,
218 Py_ssize_t max_array_len=2147483647,
219 Py_ssize_t max_map_len=2147483647,
220 Py_ssize_t max_ext_len=2147483647):
221 """
222 Unpack an object from `stream`.
223
224 Raises `ValueError` when `stream` has extra bytes.
225
226 See :class:`Unpacker` for options.
227 """
228 return unpackb(stream.read(), use_list=use_list,
229 object_hook=object_hook, object_pairs_hook=object_pairs_hook, list_hook=list_hook,
230 encoding=encoding, unicode_errors=unicode_errors, ext_hook=ext_hook,
231 max_str_len=max_str_len,
232 max_bin_len=max_bin_len,
233 max_array_len=max_array_len,
234 max_map_len=max_map_len,
235 max_ext_len=max_ext_len,
236 )
237
238
239213 cdef class Unpacker(object):
240214 """Streaming unpacker.
241215
9898 raise ValueError("cannot unpack from multi-byte object")
9999 return view
100100
101
102 def unpack(stream, **kwargs):
103 """
104 Unpack an object from `stream`.
105
106 Raises `ExtraData` when `packed` contains extra bytes.
107 See :class:`Unpacker` for options.
108 """
109 data = stream.read()
110 return unpackb(data, **kwargs)
111101
112102
113103 def unpackb(packed, **kwargs):