mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-06 17:59:52 +00:00
Merge pull request #135 from pramukta/default_function_on_int_overflow
Default function on int overflow
This commit is contained in:
commit
ca87a7e539
3 changed files with 37 additions and 6 deletions
|
|
@ -136,12 +136,20 @@ cdef class Packer(object):
|
|||
elif PyLong_Check(o):
|
||||
# PyInt_Check(long) is True for Python 3.
|
||||
# Sow we should test long before int.
|
||||
if o > 0:
|
||||
ullval = o
|
||||
ret = msgpack_pack_unsigned_long_long(&self.pk, ullval)
|
||||
else:
|
||||
llval = o
|
||||
ret = msgpack_pack_long_long(&self.pk, llval)
|
||||
try:
|
||||
if o > 0:
|
||||
ullval = o
|
||||
ret = msgpack_pack_unsigned_long_long(&self.pk, ullval)
|
||||
else:
|
||||
llval = o
|
||||
ret = msgpack_pack_long_long(&self.pk, llval)
|
||||
except OverflowError, oe:
|
||||
if not default_used and self._default is not None:
|
||||
o = self._default(o)
|
||||
default_used = True
|
||||
continue
|
||||
else:
|
||||
raise
|
||||
elif PyInt_Check(o):
|
||||
longval = o
|
||||
ret = msgpack_pack_long(&self.pk, longval)
|
||||
|
|
|
|||
|
|
@ -655,6 +655,10 @@ class Packer(object):
|
|||
return self._buffer.write(struct.pack(">BQ", 0xcf, obj))
|
||||
if -0x8000000000000000 <= obj < -0x80000000:
|
||||
return self._buffer.write(struct.pack(">Bq", 0xd3, obj))
|
||||
if not default_used and self._default is not None:
|
||||
obj = self._default(obj)
|
||||
default_used = True
|
||||
continue
|
||||
raise PackValueError("Integer value out of range")
|
||||
if self._use_bin_type and isinstance(obj, bytes):
|
||||
n = len(obj)
|
||||
|
|
|
|||
|
|
@ -55,3 +55,22 @@ def test_extension_type():
|
|||
s = msgpack.packb(obj, default=default)
|
||||
obj2 = msgpack.unpackb(s, ext_hook=ext_hook)
|
||||
assert obj == obj2
|
||||
|
||||
import sys
|
||||
if sys.version > '3':
|
||||
long = int
|
||||
|
||||
def test_overriding_hooks():
|
||||
def default(obj):
|
||||
if isinstance(obj, long):
|
||||
return {"__type__": "long", "__data__": str(obj)}
|
||||
else:
|
||||
return obj
|
||||
|
||||
obj = {"testval": long(1823746192837461928374619)}
|
||||
refobj = {"testval": default(obj["testval"])}
|
||||
refout = msgpack.packb(refobj)
|
||||
assert isinstance(refout, (str, bytes))
|
||||
testout = msgpack.packb(obj, default=default)
|
||||
|
||||
assert refout == testout
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue