@@ -118,6 +118,10 @@ cdef class Packer:
118
118
self .pk.buf_size = buf_size
119
119
self .pk.length = 0
120
120
121
+ def __dealloc__ (self ):
122
+ PyMem_Free(self .pk.buf)
123
+ self .pk.buf = NULL
124
+
121
125
def __init__ (self , *, default = None ,
122
126
bint use_single_float = False , bint autoreset = True , bint use_bin_type = True ,
123
127
bint strict_types = False , bint datetime = False , unicode_errors = None ,
@@ -138,17 +142,11 @@ cdef class Packer:
138
142
else :
139
143
self .unicode_errors = self ._berrors
140
144
141
- def __dealloc__ (self ):
142
- PyMem_Free(self .pk.buf)
143
- self .pk.buf = NULL
144
-
145
+ # returns -2 when default should(o) be called
145
146
cdef int _pack_inner(self , object o, bint will_default, int nest_limit) except - 1 :
146
147
cdef long long llval
147
148
cdef unsigned long long ullval
148
149
cdef unsigned long ulval
149
- cdef long longval
150
- cdef float fval
151
- cdef double dval
152
150
cdef const char * rawval
153
151
cdef Py_ssize_t L
154
152
cdef bool strict_types = self .strict_types
@@ -175,16 +173,13 @@ cdef class Packer:
175
173
raise OverflowError (" Integer value out of range" )
176
174
elif PyFloat_CheckExact(o) if strict_types else PyFloat_Check(o):
177
175
if self .use_float:
178
- fval = o
179
- msgpack_pack_float(& self .pk, fval)
176
+ msgpack_pack_float(& self .pk, < float > o)
180
177
else :
181
- dval = o
182
- msgpack_pack_double(& self .pk, dval)
178
+ msgpack_pack_double(& self .pk, < double > o)
183
179
elif PyBytesLike_CheckExact(o) if strict_types else PyBytesLike_Check(o):
184
180
L = Py_SIZE(o)
185
181
if L > ITEM_LIMIT:
186
182
PyErr_Format(ValueError , b" %.200s object is too large" , Py_TYPE(o).tp_name)
187
- return - 1
188
183
rawval = o
189
184
msgpack_pack_bin(& self .pk, L)
190
185
msgpack_pack_raw_body(& self .pk, rawval, L)
@@ -211,12 +206,11 @@ cdef class Packer:
211
206
self ._pack(v, nest_limit)
212
207
elif type (o) is ExtType if strict_types else isinstance (o, ExtType):
213
208
# This should be before Tuple because ExtType is namedtuple.
214
- longval = o.code
215
209
rawval = o.data
216
210
L = len (o.data)
217
211
if L > ITEM_LIMIT:
218
212
raise ValueError (" EXT data is too large" )
219
- msgpack_pack_ext(& self .pk, longval , L)
213
+ msgpack_pack_ext(& self .pk, < long > o.code , L)
220
214
msgpack_pack_raw_body(& self .pk, rawval, L)
221
215
elif type (o) is Timestamp:
222
216
llval = o.seconds
@@ -252,10 +246,8 @@ cdef class Packer:
252
246
elif self .datetime and PyDateTime_CheckExact(o):
253
247
# this should be later than will_default
254
248
PyErr_Format(ValueError , b" can not serialize '%.200s ' object where tzinfo=None" , Py_TYPE(o).tp_name)
255
- return - 1
256
249
else :
257
250
PyErr_Format(TypeError , b" can not serialize '%.200s ' object" , Py_TYPE(o).tp_name)
258
- return - 1
259
251
260
252
cdef int _pack(self , object o, int nest_limit = DEFAULT_RECURSE_LIMIT) except - 1 :
261
253
cdef int ret
@@ -270,7 +262,7 @@ cdef class Packer:
270
262
return ret
271
263
return self ._pack_inner(o, 0 , nest_limit)
272
264
273
- cpdef pack(self , object obj):
265
+ def pack (self , object obj ):
274
266
cdef int ret
275
267
try :
276
268
ret = self ._pack(obj, DEFAULT_RECURSE_LIMIT)
@@ -291,11 +283,7 @@ cdef class Packer:
291
283
def pack_array_header (self , long long size ):
292
284
if size > ITEM_LIMIT:
293
285
raise ValueError
294
- cdef int ret = msgpack_pack_array(& self .pk, size)
295
- if ret == - 1 :
296
- raise MemoryError
297
- elif ret: # should not happen
298
- raise TypeError
286
+ msgpack_pack_array(& self .pk, size)
299
287
if self .autoreset:
300
288
buf = PyBytes_FromStringAndSize(self .pk.buf, self .pk.length)
301
289
self .pk.length = 0
@@ -304,11 +292,7 @@ cdef class Packer:
304
292
def pack_map_header (self , long long size ):
305
293
if size > ITEM_LIMIT:
306
294
raise ValueError
307
- cdef int ret = msgpack_pack_map(& self .pk, size)
308
- if ret == - 1 :
309
- raise MemoryError
310
- elif ret: # should not happen
311
- raise TypeError
295
+ msgpack_pack_map(& self .pk, size)
312
296
if self .autoreset:
313
297
buf = PyBytes_FromStringAndSize(self .pk.buf, self .pk.length)
314
298
self .pk.length = 0
@@ -321,17 +305,10 @@ cdef class Packer:
321
305
*pairs* should be a sequence of pairs.
322
306
(`len(pairs)` and `for k, v in pairs:` should be supported.)
323
307
"""
324
- cdef int ret = msgpack_pack_map(& self .pk, len (pairs))
325
- if ret == 0 :
326
- for k, v in pairs:
327
- ret = self ._pack(k)
328
- if ret != 0 : break
329
- ret = self ._pack(v)
330
- if ret != 0 : break
331
- if ret == - 1 :
332
- raise MemoryError
333
- elif ret: # should not happen
334
- raise TypeError
308
+ msgpack_pack_map(& self .pk, len (pairs))
309
+ for k, v in pairs:
310
+ self ._pack(k)
311
+ self ._pack(v)
335
312
if self .autoreset:
336
313
buf = PyBytes_FromStringAndSize(self .pk.buf, self .pk.length)
337
314
self .pk.length = 0
0 commit comments