summaryrefslogtreecommitdiff
path: root/msgpack
diff options
context:
space:
mode:
Diffstat (limited to 'msgpack')
-rw-r--r--msgpack/_packer.pyx42
-rw-r--r--msgpack/_unpacker.pyx78
-rw-r--r--msgpack/fallback.py92
3 files changed, 133 insertions, 79 deletions
diff --git a/msgpack/_packer.pyx b/msgpack/_packer.pyx
index a5bc570..562c92c 100644
--- a/msgpack/_packer.pyx
+++ b/msgpack/_packer.pyx
@@ -39,9 +39,10 @@ cdef int DEFAULT_RECURSE_LIMIT=511
cdef class Packer(object):
- """MessagePack Packer
+ """
+ MessagePack Packer
- usage:
+ usage::
packer = Packer()
astream.write(packer.pack(a))
@@ -49,13 +50,18 @@ cdef class Packer(object):
Packer's constructor has some keyword arguments:
- * *defaut* - Convert user type to builtin type that Packer supports.
- See also simplejson's document.
- * *encoding* - Convert unicode to bytes with this encoding. (default: 'utf-8')
- * *unicode_erros* - Error handler for encoding unicode. (default: 'strict')
- * *use_single_float* - Use single precision float type for float. (default: False)
- * *autoreset* - Reset buffer after each pack and return it's content as `bytes`. (default: True).
- If set this to false, use `bytes()` to get content and `.reset()` to clear buffer.
+ :param callable default:
+ Convert user type to builtin type that Packer supports.
+ See also simplejson's document.
+ :param str encoding:
+ Convert unicode to bytes with this encoding. (default: 'utf-8')
+ :param str unicode_erros:
+ Error handler for encoding unicode. (default: 'strict')
+ :param bool use_single_float:
+ Use single precision float type for float. (default: False)
+ :param bool autoreset:
+ Reset buffer after each pack and return it's content as `bytes`. (default: True).
+ If set this to false, use `bytes()` to get content and `.reset()` to clear buffer.
"""
cdef msgpack_packer pk
cdef object _default
@@ -75,6 +81,8 @@ cdef class Packer(object):
self.pk.length = 0
def __init__(self, default=None, encoding='utf-8', unicode_errors='strict', use_single_float=False, bint autoreset=1):
+ """
+ """
self.use_float = use_single_float
self.autoreset = autoreset
if default is not None:
@@ -218,7 +226,7 @@ cdef class Packer(object):
Pack *pairs* as msgpack map type.
*pairs* should sequence of pair.
- (`len(pairs)` and `for k, v in *pairs*:` should be supported.)
+ (`len(pairs)` and `for k, v in pairs:` should be supported.)
"""
cdef int ret = msgpack_pack_map(&self.pk, len(pairs))
if ret == 0:
@@ -245,15 +253,21 @@ cdef class Packer(object):
return PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)
-def pack(object o, object stream, default=None, encoding='utf-8', unicode_errors='strict'):
+def pack(object o, object stream, default=None, str encoding='utf-8', str unicode_errors='strict'):
+ """
+ pack an object `o` and write it to stream)
+
+ See :class:`Packer` for options.
"""
- pack an object `o` and write it to stream)."""
packer = Packer(default=default, encoding=encoding, unicode_errors=unicode_errors)
stream.write(packer.pack(o))
-def packb(object o, default=None, encoding='utf-8', unicode_errors='strict', use_single_float=False):
+def packb(object o, default=None, encoding='utf-8', str unicode_errors='strict', bint use_single_float=False):
+ """
+ pack o and return packed bytes
+
+ See :class:`Packer` for options.
"""
- pack o and return packed bytes."""
packer = Packer(default=default, encoding=encoding, unicode_errors=unicode_errors,
use_single_float=use_single_float)
return packer.pack(o)
diff --git a/msgpack/_unpacker.pyx b/msgpack/_unpacker.pyx
index aeda02a..daeb6d7 100644
--- a/msgpack/_unpacker.pyx
+++ b/msgpack/_unpacker.pyx
@@ -81,9 +81,12 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
bint use_list=1, encoding=None, unicode_errors="strict",
object_pairs_hook=None,
):
- """Unpack packed_bytes to object. Returns an unpacked object.
+ """
+ Unpack packed_bytes to object. Returns an unpacked object.
Raises `ValueError` when `packed` contains extra bytes.
+
+ See :class:`Unpacker` for options.
"""
cdef template_context ctx
cdef size_t off = 0
@@ -121,9 +124,12 @@ def unpack(object stream, object object_hook=None, object list_hook=None,
bint use_list=1, encoding=None, unicode_errors="strict",
object_pairs_hook=None,
):
- """Unpack an object from `stream`.
+ """
+ Unpack an object from `stream`.
Raises `ValueError` when `stream` has extra bytes.
+
+ See :class:`Unpacker` for options.
"""
return unpackb(stream.read(), use_list=use_list,
object_hook=object_hook, object_pairs_hook=object_pairs_hook, list_hook=list_hook,
@@ -135,48 +141,58 @@ cdef class Unpacker(object):
"""
Streaming unpacker.
- `file_like` is a file-like object having `.read(n)` method.
- When `Unpacker` initialized with `file_like`, unpacker reads serialized data
- from it and `.feed()` method is not usable.
+ arguments:
- `read_size` is used as `file_like.read(read_size)`.
- (default: min(1024**2, max_buffer_size))
+ :param file_like:
+ File-like object having `.read(n)` method.
+ If specified, unpacker reads serialized data from it and :meth:`feed()` is not usable.
- If `use_list` is true (default), msgpack list is deserialized to Python list.
- Otherwise, it is deserialized to Python tuple.
+ :param int read_size:
+ Used as `file_like.read(read_size)`. (default: `min(1024**2, max_buffer_size)`)
- `object_hook` is same to simplejson. If it is not None, it should be callable
- and Unpacker calls it with a dict argument after deserializing a map.
+ :param bool use_list:
+ If true, unpack msgpack array to Python list.
+ Otherwise, unpack to Python tuple. (default: True)
- `object_pairs_hook` is same to simplejson. If it is not None, it should be callable
- and Unpacker calls it with a list of key-value pairs after deserializing a map.
+ :param callable object_hook:
+ When specified, it should be callable.
+ Unpacker calls it with a dict argument after unpacking msgpack map.
+ (See also simplejson)
- `encoding` is encoding used for decoding msgpack bytes. If it is None (default),
- msgpack bytes is deserialized to Python bytes.
+ :param callable object_pairs_hook:
+ When specified, it should be callable.
+ Unpacker calls it with a list of key-value pairs after unpacking msgpack map.
+ (See also simplejson)
- `unicode_errors` is used for decoding bytes.
+ :param str encoding:
+ Encoding used for decoding msgpack raw.
+ If it is None (default), msgpack raw is deserialized to Python bytes.
- `max_buffer_size` limits size of data waiting unpacked.
- 0 means system's INT_MAX (default).
- Raises `BufferFull` exception when it is insufficient.
- You shoud set this parameter when unpacking data from untrasted source.
+ :param str unicode_errors:
+ Used for decoding msgpack raw with *encoding*.
+ (default: `'strict'`)
+
+ :param int max_buffer_size:
+ Limits size of data waiting unpacked. 0 means system's INT_MAX (default).
+ Raises `BufferFull` exception when it is insufficient.
+ You shoud set this parameter when unpacking data from untrasted source.
example of streaming deserialize from file-like object::
unpacker = Unpacker(file_like)
for o in unpacker:
- do_something(o)
+ process(o)
example of streaming deserialize from socket::
unpacker = Unpacker()
- while 1:
+ while True:
buf = sock.recv(1024**2)
if not buf:
break
unpacker.feed(buf)
for o in unpacker:
- do_something(o)
+ process(o)
"""
cdef template_context ctx
cdef char* buf
@@ -197,7 +213,7 @@ cdef class Unpacker(object):
def __init__(self, file_like=None, Py_ssize_t read_size=0, bint use_list=1,
object object_hook=None, object object_pairs_hook=None, object list_hook=None,
- encoding=None, unicode_errors='strict', int max_buffer_size=0,
+ str encoding=None, str unicode_errors='strict', int max_buffer_size=0,
):
cdef char *cenc=NULL, *cerr=NULL
@@ -223,15 +239,17 @@ cdef class Unpacker(object):
if encoding is not None:
if isinstance(encoding, unicode):
- encoding = encoding.encode('ascii')
- self.encoding = encoding
- cenc = PyBytes_AsString(encoding)
+ self.encoding = encoding.encode('ascii')
+ else:
+ self.encoding = encoding
+ cenc = PyBytes_AsString(self.encoding)
if unicode_errors is not None:
if isinstance(unicode_errors, unicode):
- unicode_errors = unicode_errors.encode('ascii')
- self.unicode_errors = unicode_errors
- cerr = PyBytes_AsString(unicode_errors)
+ self.unicode_errors = unicode_errors.encode('ascii')
+ else:
+ self.unicode_errors = unicode_errors
+ cerr = PyBytes_AsString(self.unicode_errors)
init_ctx(&self.ctx, object_hook, object_pairs_hook, list_hook, use_list, cenc, cerr)
diff --git a/msgpack/fallback.py b/msgpack/fallback.py
index e295b18..f3247fc 100644
--- a/msgpack/fallback.py
+++ b/msgpack/fallback.py
@@ -61,46 +61,44 @@ TYPE_RAW = 3
DEFAULT_RECURSE_LIMIT=511
-def pack(o, stream, default=None, encoding='utf-8', unicode_errors='strict'):
- """ Pack object `o` and write it to `stream` """
- packer = Packer(default=default, encoding=encoding,
- unicode_errors=unicode_errors)
+def pack(o, stream, **kwargs):
+ """
+ Pack object `o` and write it to `stream`
+
+ See :class:`Packer` for options.
+ """
+ packer = Packer(**kwargs)
stream.write(packer.pack(o))
-def packb(o, default=None, encoding='utf-8', unicode_errors='struct',
- use_single_float=False):
- """ Pack object `o` and return packed bytes """
- packer = Packer(default=default,
- encoding=encoding,
- unicode_errors=unicode_errors,
- use_single_float=use_single_float)
- return packer.pack(o)
-
-def unpack(stream, object_hook=None, list_hook=None, use_list=True,
- encoding=None, unicode_errors='strict',
- object_pairs_hook=None):
- """ Unpack an object from `stream`.
-
- 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,
- object_pairs_hook=object_pairs_hook)
- ret = unpacker._unpack()
- if unpacker._got_extradata():
- raise ExtraData(ret, unpacker._get_extradata())
+def packb(o, **kwargs):
+ """
+ Pack object `o` and return packed bytes
+
+ See :class:`Packer` for options.
+ """
+ return Packer(**kwargs).pack(o)
+
+def unpack(stream, **kwargs):
+ """
+ Unpack an object from `stream`.
+
+ Raises `ExtraData` when `packed` contains extra bytes.
+ See :class:`Unpacker` for options.
+ """
+ unpacker = Unpacker(stream, **kwargs)
+ ret = unpacker._fb_unpack()
+ if unpacker._fb_got_extradata():
+ raise ExtraData(ret, unpacker._fb_get_extradata())
return ret
-def unpackb(packed, object_hook=None, list_hook=None, use_list=True,
- encoding=None, unicode_errors='strict',
- object_pairs_hook=None):
- """ Unpack an object from `packed`.
+def unpackb(packed, **kwargs):
+ """
+ Unpack an object from `packed`.
- 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,
- object_pairs_hook=object_pairs_hook)
+ Raises `ExtraData` when `packed` contains extra bytes.
+ See :class:`Unpacker` for options.
+ """
+ unpacker = Unpacker(None, **kwargs)
unpacker.feed(packed)
try:
ret = unpacker._unpack()
@@ -447,6 +445,30 @@ class Unpacker(object):
class Packer(object):
+ """
+ MessagePack Packer
+
+ usage:
+
+ packer = Packer()
+ astream.write(packer.pack(a))
+ astream.write(packer.pack(b))
+
+ Packer's constructor has some keyword arguments:
+
+ :param callable default:
+ Convert user type to builtin type that Packer supports.
+ See also simplejson's document.
+ :param str encoding:
+ Convert unicode to bytes with this encoding. (default: 'utf-8')
+ :param str unicode_erros:
+ Error handler for encoding unicode. (default: 'strict')
+ :param bool use_single_float:
+ Use single precision float type for float. (default: False)
+ :param bool autoreset:
+ Reset buffer after each pack and return it's content as `bytes`. (default: True).
+ If set this to false, use `bytes()` to get content and `.reset()` to clear buffer.
+ """
def __init__(self, default=None, encoding='utf-8', unicode_errors='strict',
use_single_float=False, autoreset=True):
self._use_float = use_single_float