@@ -13,6 +13,7 @@ cdef extern from "pack.h":
13
13
char * buf
14
14
size_t length
15
15
size_t buf_size
16
+ bint use_bin_type
16
17
17
18
int msgpack_pack_int(msgpack_packer* pk, int d)
18
19
int msgpack_pack_nil(msgpack_packer* pk)
@@ -26,6 +27,7 @@ cdef extern from "pack.h":
26
27
int msgpack_pack_array(msgpack_packer* pk, size_t l)
27
28
int msgpack_pack_map(msgpack_packer* pk, size_t l)
28
29
int msgpack_pack_raw(msgpack_packer* pk, size_t l)
30
+ int msgpack_pack_bin(msgpack_packer* pk, size_t l)
29
31
int msgpack_pack_raw_body(msgpack_packer* pk, char * body, size_t l)
30
32
31
33
cdef int DEFAULT_RECURSE_LIMIT= 511
@@ -56,6 +58,9 @@ cdef class Packer(object):
56
58
:param bool autoreset:
57
59
Reset buffer after each pack and return it's content as `bytes`. (default: True).
58
60
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.
59
64
"""
60
65
cdef msgpack_packer pk
61
66
cdef object _default
@@ -74,11 +79,13 @@ cdef class Packer(object):
74
79
self .pk.buf_size = buf_size
75
80
self .pk.length = 0
76
81
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 ):
78
84
"""
79
85
"""
80
86
self .use_float = use_single_float
81
87
self .autoreset = autoreset
88
+ self .pk.use_bin_type = use_bin_type
82
89
if default is not None :
83
90
if not PyCallable_Check(default):
84
91
raise TypeError (" default must be a callable." )
@@ -110,6 +117,7 @@ cdef class Packer(object):
110
117
cdef char * rawval
111
118
cdef int ret
112
119
cdef dict d
120
+ cdef size_t L
113
121
114
122
if nest_limit < 0 :
115
123
raise PackValueError(" recursion limit exceeded." )
@@ -140,9 +148,10 @@ cdef class Packer(object):
140
148
ret = msgpack_pack_double(& self .pk, dval)
141
149
elif PyBytes_Check(o):
142
150
rawval = o
143
- ret = msgpack_pack_raw(& self .pk, len (o))
151
+ L = len (o)
152
+ ret = msgpack_pack_bin(& self .pk, L)
144
153
if ret == 0 :
145
- ret = msgpack_pack_raw_body(& self .pk, rawval, len (o) )
154
+ ret = msgpack_pack_raw_body(& self .pk, rawval, L )
146
155
elif PyUnicode_Check(o):
147
156
if not self .encoding:
148
157
raise TypeError (" Can't encode unicode string: no encoding is specified" )
@@ -245,23 +254,3 @@ cdef class Packer(object):
245
254
def bytes (self ):
246
255
""" Return buffer content."""
247
256
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