@@ -13,6 +13,7 @@ cdef extern from "pack.h":
1313 char * buf
1414 size_t length
1515 size_t buf_size
16+ bint use_bin_type
1617
1718 int msgpack_pack_int(msgpack_packer* pk, int d)
1819 int msgpack_pack_nil(msgpack_packer* pk)
@@ -26,6 +27,7 @@ cdef extern from "pack.h":
2627 int msgpack_pack_array(msgpack_packer* pk, size_t l)
2728 int msgpack_pack_map(msgpack_packer* pk, size_t l)
2829 int msgpack_pack_raw(msgpack_packer* pk, size_t l)
30+ int msgpack_pack_bin(msgpack_packer* pk, size_t l)
2931 int msgpack_pack_raw_body(msgpack_packer* pk, char * body, size_t l)
3032
3133cdef int DEFAULT_RECURSE_LIMIT= 511
@@ -56,6 +58,9 @@ cdef class Packer(object):
5658 :param bool autoreset:
5759 Reset buffer after each pack and return it's content as `bytes`. (default: True).
5860 If set this to false, use `bytes()` to get content and `.reset()` to clear buffer.
61+ :param bool use_bin_type:
62+ Use bin type introduced in msgpack spec 2.0 for bytes.
63+ It also enable str8 type for unicode.
5964 """
6065 cdef msgpack_packer pk
6166 cdef object _default
@@ -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" )
@@ -245,23 +254,3 @@ cdef class Packer(object):
245254 def bytes (self ):
246255 """ Return buffer content."""
247256 return PyBytes_FromStringAndSize(self .pk.buf, self .pk.length)
248-
249-
250- def pack (object o , object stream , default = None , str encoding = ' utf-8' , str unicode_errors = ' strict' ):
251- """
252- pack an object `o` and write it to stream)
253-
254- See :class:`Packer` for options.
255- """
256- packer = Packer(default = default, encoding = encoding, unicode_errors = unicode_errors)
257- stream.write(packer.pack(o))
258-
259- def packb (object o , default = None , encoding = ' utf-8' , str unicode_errors = ' strict' , bint use_single_float = False ):
260- """
261- pack o and return packed bytes
262-
263- See :class:`Packer` for options.
264- """
265- packer = Packer(default = default, encoding = encoding, unicode_errors = unicode_errors,
266- use_single_float = use_single_float)
267- return packer.pack(o)
0 commit comments