Skip to content

Commit 35ad464

Browse files
committed
more cleanup
1 parent a0887e6 commit 35ad464

File tree

1 file changed

+15
-38
lines changed

1 file changed

+15
-38
lines changed

msgpack/_packer.pyx

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ cdef class Packer:
118118
self.pk.buf_size = buf_size
119119
self.pk.length = 0
120120

121+
def __dealloc__(self):
122+
PyMem_Free(self.pk.buf)
123+
self.pk.buf = NULL
124+
121125
def __init__(self, *, default=None,
122126
bint use_single_float=False, bint autoreset=True, bint use_bin_type=True,
123127
bint strict_types=False, bint datetime=False, unicode_errors=None,
@@ -138,17 +142,11 @@ cdef class Packer:
138142
else:
139143
self.unicode_errors = self._berrors
140144

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
145146
cdef int _pack_inner(self, object o, bint will_default, int nest_limit) except -1:
146147
cdef long long llval
147148
cdef unsigned long long ullval
148149
cdef unsigned long ulval
149-
cdef long longval
150-
cdef float fval
151-
cdef double dval
152150
cdef const char* rawval
153151
cdef Py_ssize_t L
154152
cdef bool strict_types = self.strict_types
@@ -175,16 +173,13 @@ cdef class Packer:
175173
raise OverflowError("Integer value out of range")
176174
elif PyFloat_CheckExact(o) if strict_types else PyFloat_Check(o):
177175
if self.use_float:
178-
fval = o
179-
msgpack_pack_float(&self.pk, fval)
176+
msgpack_pack_float(&self.pk, <float>o)
180177
else:
181-
dval = o
182-
msgpack_pack_double(&self.pk, dval)
178+
msgpack_pack_double(&self.pk, <double>o)
183179
elif PyBytesLike_CheckExact(o) if strict_types else PyBytesLike_Check(o):
184180
L = Py_SIZE(o)
185181
if L > ITEM_LIMIT:
186182
PyErr_Format(ValueError, b"%.200s object is too large", Py_TYPE(o).tp_name)
187-
return -1
188183
rawval = o
189184
msgpack_pack_bin(&self.pk, L)
190185
msgpack_pack_raw_body(&self.pk, rawval, L)
@@ -211,12 +206,11 @@ cdef class Packer:
211206
self._pack(v, nest_limit)
212207
elif type(o) is ExtType if strict_types else isinstance(o, ExtType):
213208
# This should be before Tuple because ExtType is namedtuple.
214-
longval = o.code
215209
rawval = o.data
216210
L = len(o.data)
217211
if L > ITEM_LIMIT:
218212
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)
220214
msgpack_pack_raw_body(&self.pk, rawval, L)
221215
elif type(o) is Timestamp:
222216
llval = o.seconds
@@ -252,10 +246,8 @@ cdef class Packer:
252246
elif self.datetime and PyDateTime_CheckExact(o):
253247
# this should be later than will_default
254248
PyErr_Format(ValueError, b"can not serialize '%.200s' object where tzinfo=None", Py_TYPE(o).tp_name)
255-
return -1
256249
else:
257250
PyErr_Format(TypeError, b"can not serialize '%.200s' object", Py_TYPE(o).tp_name)
258-
return -1
259251

260252
cdef int _pack(self, object o, int nest_limit=DEFAULT_RECURSE_LIMIT) except -1:
261253
cdef int ret
@@ -270,7 +262,7 @@ cdef class Packer:
270262
return ret
271263
return self._pack_inner(o, 0, nest_limit)
272264

273-
cpdef pack(self, object obj):
265+
def pack(self, object obj):
274266
cdef int ret
275267
try:
276268
ret = self._pack(obj, DEFAULT_RECURSE_LIMIT)
@@ -291,11 +283,7 @@ cdef class Packer:
291283
def pack_array_header(self, long long size):
292284
if size > ITEM_LIMIT:
293285
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)
299287
if self.autoreset:
300288
buf = PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)
301289
self.pk.length = 0
@@ -304,11 +292,7 @@ cdef class Packer:
304292
def pack_map_header(self, long long size):
305293
if size > ITEM_LIMIT:
306294
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)
312296
if self.autoreset:
313297
buf = PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)
314298
self.pk.length = 0
@@ -321,17 +305,10 @@ cdef class Packer:
321305
*pairs* should be a sequence of pairs.
322306
(`len(pairs)` and `for k, v in pairs:` should be supported.)
323307
"""
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)
335312
if self.autoreset:
336313
buf = PyBytes_FromStringAndSize(self.pk.buf, self.pk.length)
337314
self.pk.length = 0

0 commit comments

Comments
 (0)