From 3b933f0966b1e53ea50418970950de294ebbea76 Mon Sep 17 00:00:00 2001 From: faerot Date: Thu, 22 May 2014 11:32:54 +0300 Subject: added distinguish_tuple argument to Packer This will make precise python types serialization possible. --- msgpack/_packer.pyx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'msgpack/_packer.pyx') diff --git a/msgpack/_packer.pyx b/msgpack/_packer.pyx index 82e4a63..86e460f 100644 --- a/msgpack/_packer.pyx +++ b/msgpack/_packer.pyx @@ -56,6 +56,10 @@ cdef class Packer(object): Convert unicode to bytes with this encoding. (default: 'utf-8') :param str unicode_errors: Error handler for encoding unicode. (default: 'strict') + :param bool distinguish_tuple: + If set to true, tuples will not be serialized as lists + and will be treated as unsupported type. This is useful when trying + to implement accurate serialization for python types. :param bool use_single_float: Use single precision float type for float. (default: False) :param bool autoreset: @@ -71,6 +75,7 @@ cdef class Packer(object): cdef object _berrors cdef char *encoding cdef char *unicode_errors + cdef bool distinguish_tuple cdef bool use_float cdef bint autoreset @@ -83,10 +88,12 @@ 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, bint use_bin_type=0): + distinguish_tuple=False, use_single_float=False, bint autoreset=1, + bint use_bin_type=0): """ """ self.use_float = use_single_float + self.distinguish_tuple = distinguish_tuple self.autoreset = autoreset self.pk.use_bin_type = use_bin_type if default is not None: @@ -122,6 +129,7 @@ cdef class Packer(object): cdef dict d cdef size_t L cdef int default_used = 0 + cdef bool distinguish_tuple = self.distinguish_tuple if nest_limit < 0: raise PackValueError("recursion limit exceeded.") @@ -204,7 +212,7 @@ cdef class Packer(object): raise ValueError("EXT data is too large") ret = msgpack_pack_ext(&self.pk, longval, L) ret = msgpack_pack_raw_body(&self.pk, rawval, L) - elif PyTuple_Check(o) or PyList_Check(o): + elif (PyTuple_Check(o) and not distinguish_tuple) or PyList_Check(o): L = len(o) if L > (2**32)-1: raise ValueError("list is too large") -- cgit v1.2.1 From b877ce2afadd4a4c96d7c0542f7b29836785de71 Mon Sep 17 00:00:00 2001 From: faerot Date: Thu, 22 May 2014 16:45:26 +0300 Subject: precise_mode instead of distinguish_tuple When precise_mode flag is set, serialization will be as precise as possible - type checks will be exact (type(..) is ... instead of isinstance(..., ...) and tuple will be treated as undefined type. This mode is to make accurate object serialization possible. --- msgpack/_packer.pyx | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'msgpack/_packer.pyx') diff --git a/msgpack/_packer.pyx b/msgpack/_packer.pyx index 86e460f..ec34cd8 100644 --- a/msgpack/_packer.pyx +++ b/msgpack/_packer.pyx @@ -56,10 +56,13 @@ cdef class Packer(object): Convert unicode to bytes with this encoding. (default: 'utf-8') :param str unicode_errors: Error handler for encoding unicode. (default: 'strict') - :param bool distinguish_tuple: - If set to true, tuples will not be serialized as lists - and will be treated as unsupported type. This is useful when trying - to implement accurate serialization for python types. + :param bool precise_mode: + If set to true, types will be checked to be exact. Derived classes + from serializeable types will not be serialized and will be + treated as unsupported type and forwarded to default. + Additionally tuples will not be serialized as lists. + This is useful when trying to implement accurate serialization + for python types. :param bool use_single_float: Use single precision float type for float. (default: False) :param bool autoreset: @@ -75,7 +78,7 @@ cdef class Packer(object): cdef object _berrors cdef char *encoding cdef char *unicode_errors - cdef bool distinguish_tuple + cdef bint precise_mode cdef bool use_float cdef bint autoreset @@ -88,12 +91,12 @@ cdef class Packer(object): self.pk.length = 0 def __init__(self, default=None, encoding='utf-8', unicode_errors='strict', - distinguish_tuple=False, use_single_float=False, bint autoreset=1, - bint use_bin_type=0): + use_single_float=False, bint autoreset=1, bint use_bin_type=0, + bint precise_mode=0): """ """ self.use_float = use_single_float - self.distinguish_tuple = distinguish_tuple + self.precise_mode = precise_mode self.autoreset = autoreset self.pk.use_bin_type = use_bin_type if default is not None: @@ -129,7 +132,7 @@ cdef class Packer(object): cdef dict d cdef size_t L cdef int default_used = 0 - cdef bool distinguish_tuple = self.distinguish_tuple + cdef bint precise = self.precise_mode if nest_limit < 0: raise PackValueError("recursion limit exceeded.") @@ -137,12 +140,12 @@ cdef class Packer(object): while True: if o is None: ret = msgpack_pack_nil(&self.pk) - elif isinstance(o, bool): + elif PyBool_Check(o) if precise else isinstance(o, bool): if o: ret = msgpack_pack_true(&self.pk) else: ret = msgpack_pack_false(&self.pk) - elif PyLong_Check(o): + elif PyLong_CheckExact(o) if precise else PyLong_Check(o): # PyInt_Check(long) is True for Python 3. # Sow we should test long before int. if o > 0: @@ -151,17 +154,17 @@ cdef class Packer(object): else: llval = o ret = msgpack_pack_long_long(&self.pk, llval) - elif PyInt_Check(o): + elif PyInt_CheckExact(o) if precise else PyInt_Check(o): longval = o ret = msgpack_pack_long(&self.pk, longval) - elif PyFloat_Check(o): + elif PyFloat_CheckExact(o) if precise else PyFloat_Check(o): if self.use_float: fval = o ret = msgpack_pack_float(&self.pk, fval) else: dval = o ret = msgpack_pack_double(&self.pk, dval) - elif PyBytes_Check(o): + elif PyBytes_CheckExact(o) if precise else PyBytes_Check(o): L = len(o) if L > (2**32)-1: raise ValueError("bytes is too large") @@ -169,7 +172,7 @@ cdef class Packer(object): ret = msgpack_pack_bin(&self.pk, L) if ret == 0: ret = msgpack_pack_raw_body(&self.pk, rawval, L) - elif PyUnicode_Check(o): + elif PyUnicode_CheckExact(o) if precise else PyUnicode_Check(o): if not self.encoding: raise TypeError("Can't encode unicode string: no encoding is specified") o = PyUnicode_AsEncodedString(o, self.encoding, self.unicode_errors) @@ -192,7 +195,7 @@ cdef class Packer(object): if ret != 0: break ret = self._pack(v, nest_limit-1) if ret != 0: break - elif PyDict_Check(o): + elif not precise and PyDict_Check(o): L = len(o) if L > (2**32)-1: raise ValueError("dict is too large") @@ -212,7 +215,7 @@ cdef class Packer(object): raise ValueError("EXT data is too large") ret = msgpack_pack_ext(&self.pk, longval, L) ret = msgpack_pack_raw_body(&self.pk, rawval, L) - elif (PyTuple_Check(o) and not distinguish_tuple) or PyList_Check(o): + elif PyList_CheckExact(o) if precise else (PyTuple_Check(o) or PyList_Check(o)): L = len(o) if L > (2**32)-1: raise ValueError("list is too large") -- cgit v1.2.1