mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-27 23:54:12 +00:00
Fix minor bugs and tuning unpacking dict.
This commit is contained in:
parent
0536d1bd0c
commit
22920baae6
2 changed files with 17 additions and 10 deletions
|
|
@ -360,16 +360,19 @@ class Unpacker(object):
|
||||||
self._fb_unpack(EX_SKIP, write_bytes)
|
self._fb_unpack(EX_SKIP, write_bytes)
|
||||||
self._fb_unpack(EX_SKIP, write_bytes)
|
self._fb_unpack(EX_SKIP, write_bytes)
|
||||||
return
|
return
|
||||||
ret = []
|
|
||||||
for i in xrange(n):
|
|
||||||
ret.append((self._fb_unpack(EX_CONSTRUCT, write_bytes),
|
|
||||||
self._fb_unpack(EX_CONSTRUCT, write_bytes)))
|
|
||||||
if self.object_pairs_hook is not None:
|
if self.object_pairs_hook is not None:
|
||||||
ret = self.object_pairs_hook(ret)
|
ret = self.object_pairs_hook(
|
||||||
|
(self._fb_unpack(EX_CONSTRUCT, write_bytes),
|
||||||
|
self._fb_unpack(EX_CONSTRUCT, write_bytes))
|
||||||
|
for _ in xrange(n)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
ret = dict(ret)
|
ret = {}
|
||||||
if self.object_hook is not None:
|
for _ in xrange(n):
|
||||||
ret = self.object_hook(ret)
|
key = self._fb_unpack(EX_CONSTRUCT, write_bytes)
|
||||||
|
ret[key] = self._fb_unpack(EX_CONSTRUCT, write_bytes)
|
||||||
|
if self.object_hook is not None:
|
||||||
|
ret = self.object_hook(ret)
|
||||||
return ret
|
return ret
|
||||||
if execute == EX_SKIP:
|
if execute == EX_SKIP:
|
||||||
return
|
return
|
||||||
|
|
@ -421,7 +424,7 @@ class Packer(object):
|
||||||
raise TypeError("default must be callable")
|
raise TypeError("default must be callable")
|
||||||
self._default = default
|
self._default = default
|
||||||
|
|
||||||
def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT):
|
def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT, isinstance=isinstance):
|
||||||
if nest_limit < 0:
|
if nest_limit < 0:
|
||||||
raise PackValueError("recursion limit exceeded")
|
raise PackValueError("recursion limit exceeded")
|
||||||
if obj is None:
|
if obj is None:
|
||||||
|
|
@ -454,6 +457,10 @@ class Packer(object):
|
||||||
raise PackValueError("Integer value out of range")
|
raise PackValueError("Integer value out of range")
|
||||||
if isinstance(obj, (Unicode, bytes)):
|
if isinstance(obj, (Unicode, bytes)):
|
||||||
if isinstance(obj, Unicode):
|
if isinstance(obj, Unicode):
|
||||||
|
if self.encoding is None:
|
||||||
|
raise TypeError(
|
||||||
|
"Can't encode unicode string: "
|
||||||
|
"no encoding is specified")
|
||||||
obj = obj.encode(self.encoding, self.unicode_errors)
|
obj = obj.encode(self.encoding, self.unicode_errors)
|
||||||
n = len(obj)
|
n = len(obj)
|
||||||
if n <= 0x1f:
|
if n <= 0x1f:
|
||||||
|
|
|
||||||
|
|
@ -151,7 +151,7 @@ def test_odict():
|
||||||
od = odict(seq)
|
od = odict(seq)
|
||||||
assert unpackb(packb(od), use_list=1) == dict(seq)
|
assert unpackb(packb(od), use_list=1) == dict(seq)
|
||||||
def pair_hook(seq):
|
def pair_hook(seq):
|
||||||
return seq
|
return list(seq)
|
||||||
assert unpackb(packb(od), object_pairs_hook=pair_hook, use_list=1) == seq
|
assert unpackb(packb(od), object_pairs_hook=pair_hook, use_list=1) == seq
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue