@@ -26,6 +26,7 @@ cdef extern from "pack.h":
2626 int msgpack_pack_array(msgpack_packer* pk, size_t l)
2727 int msgpack_pack_map(msgpack_packer* pk, size_t l)
2828 int msgpack_pack_raw(msgpack_packer* pk, size_t l)
29+ int msgpack_pack_bin(msgpack_packer* pk, size_t l)
2930 int msgpack_pack_raw_body(msgpack_packer* pk, char * body, size_t l)
3031
3132cdef int DEFAULT_RECURSE_LIMIT= 511
@@ -56,6 +57,9 @@ cdef class Packer(object):
5657 :param bool autoreset:
5758 Reset buffer after each pack and return it's content as `bytes`. (default: True).
5859 If set this to false, use `bytes()` to get content and `.reset()` to clear buffer.
60+ :param bool use_bin_type:
61+ Use bin type introduced in msgpack spec 2.0 for bytes.
62+ It also enable str8 type for unicode.
5963 """
6064 cdef msgpack_packer pk
6165 cdef object _default
@@ -64,6 +68,7 @@ cdef class Packer(object):
6468 cdef char * encoding
6569 cdef char * unicode_errors
6670 cdef bool use_float
71+ cdef bool use_bin_type
6772 cdef bint autoreset
6873
6974 def __cinit__ (self ):
@@ -74,11 +79,13 @@ cdef class Packer(object):
7479 self .pk.buf_size = buf_size
7580 self .pk.length = 0
7681
77- def __init__ (self , default = None , encoding = ' utf-8' , unicode_errors = ' strict' , use_single_float = False , bint autoreset = 1 ):
82+ def __init__ (self , default = None , encoding = ' utf-8' , unicode_errors = ' strict' ,
83+ use_single_float = False , bint autoreset = 1 , bint use_bin_type = 0 ):
7884 """
7985 """
8086 self .use_float = use_single_float
8187 self .autoreset = autoreset
88+ self .pk.use_bin_type = use_bin_type
8289 if default is not None :
8390 if not PyCallable_Check(default):
8491 raise TypeError (" default must be a callable." )
@@ -110,6 +117,7 @@ cdef class Packer(object):
110117 cdef char * rawval
111118 cdef int ret
112119 cdef dict d
120+ cdef size_t L
113121
114122 if nest_limit < 0 :
115123 raise PackValueError(" recursion limit exceeded." )
@@ -140,9 +148,10 @@ cdef class Packer(object):
140148 ret = msgpack_pack_double(& self .pk, dval)
141149 elif PyBytes_Check(o):
142150 rawval = o
143- ret = msgpack_pack_raw(& self .pk, len (o))
151+ L = len (o)
152+ ret = msgpack_pack_bin(& self .pk, L)
144153 if ret == 0 :
145- ret = msgpack_pack_raw_body(& self .pk, rawval, len (o) )
154+ ret = msgpack_pack_raw_body(& self .pk, rawval, L )
146155 elif PyUnicode_Check(o):
147156 if not self .encoding:
148157 raise TypeError (" Can't encode unicode string: no encoding is specified" )
@@ -247,21 +256,26 @@ cdef class Packer(object):
247256 return PyBytes_FromStringAndSize(self .pk.buf, self .pk.length)
248257
249258
250- def pack (object o , object stream , default = None , str encoding = ' utf-8' , str unicode_errors = ' strict' ):
259+ def pack (object o , object stream ,
260+ default = None , str encoding = ' utf-8' , str unicode_errors = ' strict' ,
261+ bint use_single_float = False , bint use_bin_type = False ):
251262 """
252263 pack an object `o` and write it to stream)
253264
254265 See :class:`Packer` for options.
255266 """
256- packer = Packer(default = default, encoding = encoding, unicode_errors = unicode_errors)
267+ packer = Packer(default = default, encoding = encoding, unicode_errors = unicode_errors,
268+ use_single_float = use_single_float, use_bin_type = use_bin_type)
257269 stream.write(packer.pack(o))
258270
259- def packb (object o , default = None , encoding = ' utf-8' , str unicode_errors = ' strict' , bint use_single_float = False ):
271+ def packb (object o ,
272+ default = None , str encoding = ' utf-8' , str unicode_errors = ' strict' ,
273+ bint use_single_float = False , bint use_bin_type = False ):
260274 """
261275 pack o and return packed bytes
262276
263277 See :class:`Packer` for options.
264278 """
265279 packer = Packer(default = default, encoding = encoding, unicode_errors = unicode_errors,
266- use_single_float = use_single_float)
280+ use_single_float = use_single_float, use_bin_type = use_bin_type )
267281 return packer.pack(o)
0 commit comments