summaryrefslogtreecommitdiff
path: root/msgpack/_packer.pyx
diff options
context:
space:
mode:
authorINADA Naoki <methane@users.noreply.github.com>2015-11-09 02:23:22 +0900
committerINADA Naoki <methane@users.noreply.github.com>2015-11-09 02:23:22 +0900
commitca87a7e539e0bd1b69ce8e715b1fd6ecab5f5553 (patch)
tree4c0bd9cc6e798eb715ef50ab7fc3d4f90f849f9d /msgpack/_packer.pyx
parent7d900371c8f13fa64f06aaf336b6ae65c705bf2c (diff)
parent6f02d252e1dc66d67861b45c5bead8392ed822d4 (diff)
downloadmsgpack-python-ca87a7e539e0bd1b69ce8e715b1fd6ecab5f5553.tar.gz
Merge pull request #135 from pramukta/default_function_on_int_overflow
Default function on int overflow
Diffstat (limited to 'msgpack/_packer.pyx')
-rw-r--r--msgpack/_packer.pyx20
1 files changed, 14 insertions, 6 deletions
diff --git a/msgpack/_packer.pyx b/msgpack/_packer.pyx
index fcd20a7..7129208 100644
--- a/msgpack/_packer.pyx
+++ b/msgpack/_packer.pyx
@@ -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)