mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-28 16:14:13 +00:00
Merge pull request #11 from TobiasSimon/float_ext
added float serialization support
This commit is contained in:
commit
f14d926e1d
1 changed files with 12 additions and 4 deletions
|
|
@ -26,6 +26,7 @@ cdef extern from "pack.h":
|
||||||
int msgpack_pack_long(msgpack_packer* pk, long d)
|
int msgpack_pack_long(msgpack_packer* pk, long d)
|
||||||
int msgpack_pack_long_long(msgpack_packer* pk, long long d)
|
int msgpack_pack_long_long(msgpack_packer* pk, long long d)
|
||||||
int msgpack_pack_unsigned_long_long(msgpack_packer* pk, unsigned long long d)
|
int msgpack_pack_unsigned_long_long(msgpack_packer* pk, unsigned long long d)
|
||||||
|
int msgpack_pack_float(msgpack_packer* pk, float d)
|
||||||
int msgpack_pack_double(msgpack_packer* pk, double d)
|
int msgpack_pack_double(msgpack_packer* pk, double d)
|
||||||
int msgpack_pack_array(msgpack_packer* pk, size_t l)
|
int msgpack_pack_array(msgpack_packer* pk, size_t l)
|
||||||
int msgpack_pack_map(msgpack_packer* pk, size_t l)
|
int msgpack_pack_map(msgpack_packer* pk, size_t l)
|
||||||
|
|
@ -54,6 +55,7 @@ cdef class Packer(object):
|
||||||
cdef object _berrors
|
cdef object _berrors
|
||||||
cdef char *encoding
|
cdef char *encoding
|
||||||
cdef char *unicode_errors
|
cdef char *unicode_errors
|
||||||
|
cdef bool use_float
|
||||||
|
|
||||||
def __cinit__(self):
|
def __cinit__(self):
|
||||||
cdef int buf_size = 1024*1024
|
cdef int buf_size = 1024*1024
|
||||||
|
|
@ -63,7 +65,8 @@ cdef class Packer(object):
|
||||||
self.pk.buf_size = buf_size
|
self.pk.buf_size = buf_size
|
||||||
self.pk.length = 0
|
self.pk.length = 0
|
||||||
|
|
||||||
def __init__(self, default=None, encoding='utf-8', unicode_errors='strict'):
|
def __init__(self, default=None, encoding='utf-8', unicode_errors='strict', use_float=False):
|
||||||
|
self.use_float = use_float
|
||||||
if default is not None:
|
if default is not None:
|
||||||
if not PyCallable_Check(default):
|
if not PyCallable_Check(default):
|
||||||
raise TypeError("default must be a callable.")
|
raise TypeError("default must be a callable.")
|
||||||
|
|
@ -90,7 +93,8 @@ cdef class Packer(object):
|
||||||
cdef long long llval
|
cdef long long llval
|
||||||
cdef unsigned long long ullval
|
cdef unsigned long long ullval
|
||||||
cdef long longval
|
cdef long longval
|
||||||
cdef double fval
|
cdef float fval
|
||||||
|
cdef double dval
|
||||||
cdef char* rawval
|
cdef char* rawval
|
||||||
cdef int ret
|
cdef int ret
|
||||||
cdef dict d
|
cdef dict d
|
||||||
|
|
@ -116,8 +120,12 @@ cdef class Packer(object):
|
||||||
longval = o
|
longval = o
|
||||||
ret = msgpack_pack_long(&self.pk, longval)
|
ret = msgpack_pack_long(&self.pk, longval)
|
||||||
elif PyFloat_Check(o):
|
elif PyFloat_Check(o):
|
||||||
|
if self.use_float:
|
||||||
fval = o
|
fval = o
|
||||||
ret = msgpack_pack_double(&self.pk, fval)
|
ret = msgpack_pack_float(&self.pk, fval)
|
||||||
|
else:
|
||||||
|
dval = o
|
||||||
|
ret = msgpack_pack_double(&self.pk, dval)
|
||||||
elif PyBytes_Check(o):
|
elif PyBytes_Check(o):
|
||||||
rawval = o
|
rawval = o
|
||||||
ret = msgpack_pack_raw(&self.pk, len(o))
|
ret = msgpack_pack_raw(&self.pk, len(o))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue