mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-20 12:23:16 +00:00
pep8 friendly.
This commit is contained in:
parent
4cde7f080c
commit
328369e52e
1 changed files with 29 additions and 17 deletions
|
@ -1,4 +1,4 @@
|
|||
# Fallback pure Python implementation of msgpack
|
||||
"""Fallback pure Python implementation of msgpack"""
|
||||
|
||||
import sys
|
||||
import array
|
||||
|
@ -68,7 +68,8 @@ def unpack(stream, object_hook=None, list_hook=None, use_list=True,
|
|||
|
||||
Raises `ExtraData` when `stream` has extra bytes. """
|
||||
unpacker = Unpacker(stream, object_hook=object_hook, list_hook=list_hook,
|
||||
use_list=use_list, encoding=encoding, unicode_errors=unicode_errors,
|
||||
use_list=use_list,
|
||||
encoding=encoding, unicode_errors=unicode_errors,
|
||||
object_pairs_hook=object_pairs_hook)
|
||||
ret = unpacker._fb_unpack()
|
||||
if unpacker._fb_got_extradata():
|
||||
|
@ -82,7 +83,8 @@ def unpackb(packed, object_hook=None, list_hook=None, use_list=True,
|
|||
|
||||
Raises `ExtraData` when `packed` contains extra bytes. """
|
||||
unpacker = Unpacker(None, object_hook=object_hook, list_hook=list_hook,
|
||||
use_list=use_list, encoding=encoding, unicode_errors=unicode_errors,
|
||||
use_list=use_list,
|
||||
encoding=encoding, unicode_errors=unicode_errors,
|
||||
object_pairs_hook=object_pairs_hook)
|
||||
unpacker.feed(packed)
|
||||
ret = unpacker._fb_unpack()
|
||||
|
@ -174,7 +176,7 @@ class Unpacker(object):
|
|||
if object_pairs_hook is not None and not callable(object_pairs_hook):
|
||||
raise ValueError('`object_pairs_hook` is not callable')
|
||||
if object_hook is not None and object_pairs_hook is not None:
|
||||
raise ValueError("object_pairs_hook and object_hook are mutually "+
|
||||
raise ValueError("object_pairs_hook and object_hook are mutually "
|
||||
"exclusive")
|
||||
|
||||
def feed(self, next_bytes):
|
||||
|
@ -404,6 +406,7 @@ class Packer(object):
|
|||
if not callable(default):
|
||||
raise TypeError("default must be callable")
|
||||
self._default = default
|
||||
|
||||
def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT):
|
||||
if nest_limit < 0:
|
||||
raise PackValueError("recursion limit exceeded")
|
||||
|
@ -465,6 +468,7 @@ class Packer(object):
|
|||
if self._default is not None:
|
||||
return self._pack(self._default(obj), nest_limit - 1)
|
||||
raise TypeError("Cannot serialize %r" % obj)
|
||||
|
||||
def pack(self, obj):
|
||||
self._pack(obj)
|
||||
ret = self.buffer.getvalue()
|
||||
|
@ -473,6 +477,7 @@ class Packer(object):
|
|||
elif USING_STRINGBUILDER:
|
||||
self.buffer = StringIO(ret)
|
||||
return ret
|
||||
|
||||
def pack_map_pairs(self, pairs):
|
||||
self._fb_pack_map_pairs(len(pairs), pairs)
|
||||
ret = self.buffer.getvalue()
|
||||
|
@ -481,6 +486,7 @@ class Packer(object):
|
|||
elif USING_STRINGBUILDER:
|
||||
self.buffer = StringIO(ret)
|
||||
return ret
|
||||
|
||||
def pack_array_header(self, n):
|
||||
self._fb_pack_array_header(n)
|
||||
ret = self.buffer.getvalue()
|
||||
|
@ -489,6 +495,7 @@ class Packer(object):
|
|||
elif USING_STRINGBUILDER:
|
||||
self.buffer = StringIO(ret)
|
||||
return ret
|
||||
|
||||
def pack_map_header(self, n):
|
||||
self._fb_pack_map_header(n)
|
||||
ret = self.buffer.getvalue()
|
||||
|
@ -497,6 +504,7 @@ class Packer(object):
|
|||
elif USING_STRINGBUILDER:
|
||||
self.buffer = StringIO(ret)
|
||||
return ret
|
||||
|
||||
def _fb_pack_array_header(self, n):
|
||||
if n <= 0x0f:
|
||||
return self.buffer.write(chr(0x90 + n))
|
||||
|
@ -505,6 +513,7 @@ class Packer(object):
|
|||
if n <= 0xffffffff:
|
||||
return self.buffer.write(struct.pack(">BI", 0xdd, n))
|
||||
raise PackValueError("Array is too large")
|
||||
|
||||
def _fb_pack_map_header(self, n):
|
||||
if n <= 0x0f:
|
||||
return self.buffer.write(chr(0x80 + n))
|
||||
|
@ -513,12 +522,15 @@ class Packer(object):
|
|||
if n <= 0xffffffff:
|
||||
return self.buffer.write(struct.pack(">BI", 0xdf, n))
|
||||
raise PackValueError("Dict is too large")
|
||||
|
||||
def _fb_pack_map_pairs(self, n, pairs, nest_limit=DEFAULT_RECURSE_LIMIT):
|
||||
self._fb_pack_map_header(n)
|
||||
for (k, v) in pairs:
|
||||
self._pack(k, nest_limit - 1)
|
||||
self._pack(v, nest_limit - 1)
|
||||
|
||||
def bytes(self):
|
||||
return self.buffer.getvalue()
|
||||
|
||||
def reset(self):
|
||||
self.buffer = StringIO()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue