8000 Merge branch 'master' of https://github.com/faerot/msgpack-python int… · sacherjj/msgpack-python@e9a47cb · GitHub
[go: up one dir, main page]

Skip to content

Commit e9a47cb

Browse files
committed
Merge branch 'master' of https://github.com/faerot/msgpack-python into pramukta-default_function_on_int_overflow
2 parents 29266b0 + b877ce2 commit e9a47cb

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

msgpack/_packer.pyx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ cdef class Packer(object):
5555
Convert unicode to bytes with this encoding. (default: 'utf-8')
5656
:param str unicode_errors:
5757
Error handler for encoding unicode. (default: 'strict')
58+
:param bool precise_mode:
59+
If set to true, types will be checked to be exact. Derived classes
60+
from serializeable types will not be serialized and will be
61+
treated as unsupported type and forwarded to default.
62+
Additionally tuples will not be serialized as lists.
63+
This is useful when trying to implement accurate serialization
64+
for python types.
5865
:param bool use_single_float:
5966
Use single precision float type for float. (default: False)
6067
:param bool autoreset:
@@ -70,6 +77,7 @@ cdef class Packer(object):
7077
cdef object _berrors
7178
cdef char *encoding
7279
cdef char *unicode_errors
80+
cdef bint precise_mode
7381
cdef bool use_float
7482
cdef bint autoreset
7583

@@ -82,10 +90,12 @@ cdef class Packer(object):
8290
self.pk.length = 0
8391

8492
def __init__(self, default=None, encoding='utf-8', unicode_errors='strict',
85-
use_single_float=False, bint autoreset=1, bint use_bin_type=0):
93+
use_single_float=False, bint autoreset=1, bint use_bin_type=0,
94+
bint precise_mode=0):
8695
"""
8796
"""
8897
self.use_float = use_single_float
98+
self.precise_mode = precise_mode
8999
self.autoreset = autoreset
90100
self.pk.use_bin_type = use_bin_type
91101
if default is not None:
@@ -121,19 +131,20 @@ cdef class Packer(object):
121131
cdef dict d
122132
cdef size_t L
123133
cdef int default_used = 0
134+
cdef bint precise = self.precise_mode
124135

125136
if nest_limit < 0:
126137
raise PackValueError("recursion limit exceeded.")
127138

128139
while True:
129140
if o is None:
130141
ret = msgpack_pack_nil(&self.pk)
131-
elif isinstance(o, bool):
142+
elif PyBool_Check(o) if precise else isinstance(o, bool):
132143
if o:
133144
ret = msgpack_pack_true(&self.pk)
134145
else:
135146
ret = msgpack_pack_false(&self.pk)
136-
elif PyLong_Check(o):
147+
elif PyLong_CheckExact(o) if precise else PyLong_Check(o):
137148
# PyInt_Check(long) is True for Python 3.
138149
# So we should test long before int.
139150
try:
@@ -150,25 +161,25 @@ cdef class Packer(object):
150161
continue
151162
else:
152163
raise
153-
elif PyInt_Check(o):
164+
elif PyInt_CheckExact(o) if precise else PyInt_Check(o):
154165
longval = o
155166
ret = msgpack_pack_long(&self.pk, longval)
156-
elif PyFloat_Check(o):
167+
elif PyFloat_CheckExact(o) if precise else PyFloat_Check(o):
157168
if self.use_float:
158169
fval = o
159170
ret = msgpack_pack_float(&self.pk, fval)
160171
else:
161172
dval = o
162173
ret = msgpack_pack_double(&self.pk, dval)
163-
elif PyBytes_Check(o):
174+
elif PyBytes_CheckExact(o) if precise else PyBytes_Check(o):
164175
L = len(o)
165176
if L > (2**32)-1:
166177
raise ValueError("bytes is too large")
167178
rawval = o
168179
ret = msgpack_pack_bin(&self.pk, L)
169180
if ret == 0:
170181
ret = msgpack_pack_raw_body(&self.pk, rawval, L)
171-
elif PyUnicode_Check(o):
182+
elif PyUnicode_CheckExact(o) if precise else PyUnicode_Check(o):
172183
if not self.encoding:
173184
raise TypeError("Can't encode unicode string: no encoding is specified")
174185
o = PyUnicode_AsEncodedString(o, self.encoding, self.unicode_errors)
@@ -191,7 +202,7 @@ cdef class Packer(object):
191202
if ret != 0: break
192203
ret = self._pack(v, nest_limit-1)
193204
if ret != 0: break
194-
elif PyDict_Check(o):
205+
elif not precise and PyDict_Check(o):
195206
L = len(o)
196207
if L > (2**32)-1:
197208
raise ValueError("dict is too large")
@@ -211,7 +222,7 @@ cdef class Packer(object):
211222
raise ValueError("EXT data is too large")
212223
ret = msgpack_pack_ext(&self.pk, longval, L)
213224
ret = msgpack_pack_raw_body(&self.pk, rawval, L)
214-
elif PyTuple_Check(o) or PyList_Check(o):
225+
elif PyList_CheckExact(o) if precise else (PyTuple_Check(o) or PyList_Check(o)):
215226
L = len(o)
216227
if L > (2**32)-1:
217228
raise ValueError("list is too large")

msgpack/fallback.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,13 @@ class Packer(object):
601601
Convert unicode to bytes with this encoding. (default: 'utf-8')
602602
:param str unicode_errors:
603603
Error handler for encoding unicode. (default: 'strict')
604+
:param bool precise_mode:
605+
If set to true, types will be checked to be exact. Derived classes
606+
from serializeable types will not be serialized and will be
607+
treated as unsupported type and forwarded to default.
608+
Additionally tuples will not be serialized as lists.
609+
This is useful when trying to implement accurate serialization
610+
for python types.
604611
:param bool use_single_float:
605612
Use single precision float type for float. (default: False)
606613
:param bool autoreset:
@@ -611,7 +618,9 @@ class Packer(object):
611618
It also enable str8 type for unicode.
612619
"""
613620
def __init__(self, default=None, encoding='utf-8', unicode_errors='strict',
614-
use_single_float=False, autoreset=True, use_bin_type=False):
621+
precise_mode=False, use_single_float=False, autoreset=True,
622+
use_bin_type=False):
623+
self._precise_mode = precise_mode
615624
self._use_float = use_single_float
616625
self._autoreset = autoreset
617626
self._use_bin_type = use_bin_type
@@ -623,18 +632,30 @@ def __init__(self, default=None, encoding='utf-8', unicode_errors='strict',
623632
raise TypeError("default must be callable")
624633
self._default = default
625634

626-
def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT, isinstance=isinstance):
635+
def _check_precise(obj, t, type=type, tuple=tuple):
636+
if type(t) is tuple:
637+
return type(obj) in t
638+
else:
639+
return type(obj) is t
640+
641+
def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT,
642+
check=isinstance, check_precise=_check_precise):
627643
default_used = False
644+
if self._precise_mode:
645+
check = check_precise
646+
list_types = list
647+
else:
648+
list_types = (list, tuple)
628649
while True:
629650
if nest_limit < 0:
630651
raise PackValueError("recursion limit exceeded")
631652
if obj is None:
632653
return self._buffer.write(b"\xc0")
633-
if isinstance(obj, bool):
654+
if check(obj, bool):
634655
if obj:
635656
return self._buffer.write(b"\xc3")
636657
return self._buffer.write(b"\xc2")
637-
if isinstance(obj, int_types):
658+
if check(obj, int_types):
638659
if 0 <= obj < 0x80:
639660
return self._buffer.write(struct.pack("B", obj))
640661
if -0x20 <= obj < 0:
@@ -660,7 +681,7 @@ def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT, isinstance=isinstance):
660681
default_used = True
661682
continue
662683
raise PackValueError("Integer value out of range")
663-
if self._use_bin_type and isinstance(obj, bytes):
684+
if self._use_bin_type and check(obj, bytes):
664685
n = len(obj)
665686
if n <= 0xff:
666687
self._buffer.write(struct.pack('>BB', 0xc4, n))
@@ -671,8 +692,8 @@ def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT, isinstance=isinstance):
671692
else:
672693
raise PackValueError("Bytes is too large")
673694
return self._buffer.write(obj)
674-
if isinstance(obj, (Unicode, bytes)):
675-
if isinstance(obj, Unicode):
695+
if check(obj, (Unicode, bytes)):
696+
if check(obj, Unicode):
676697
if self._encoding is None:
677698
raise TypeError(
678699
"Can't encode unicode string: "
@@ -690,11 +711,11 @@ def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT, isinstance=isinstance):
690711
else:
691712
raise PackValueError("String is too large")
692713
return self._buffer.write(obj)
693-
if isinstance(obj, float):
714+
if check(obj, float):
694715
if self._use_float:
695716
return self._buffer.write(struct.pack(">Bf", 0xca, obj))
696717
return self._buffer.write(struct.pack(">Bd", 0xcb, obj))
697-
if isinstance(obj, ExtType):
718+
if check(obj, ExtType):
698719
code = obj.code
699720
data = obj.data
700721
assert isinstance(code, int)
@@ -719,13 +740,13 @@ def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT, isinstance=isinstance):
719740
self._buffer.write(struct.pack("b", code))
720741
self._buffer.write(data)
721742
return
722-
if isinstance(obj, (list, tuple)):
743+
if check(obj, list_types):
723744
n = len(obj)
724745
self._fb_pack_array_header(n)
725746
for i in xrange(n):
726747
self._pack(obj[i], nest_limit - 1)
727748
return
728-
if isinstance(obj, dict):
749+
if check(obj, dict):
729750
return self._fb_pack_map_pairs(len(obj), dict_iteritems(obj),
730751
nest_l 3EFF imit - 1)
731752
if not default_used and self._default is not None:

0 commit comments

Comments
 (0)
0