11# coding: utf-8
22# cython: embedsignature=True
33
4- import warnings
5-
64from cpython cimport *
75cdef extern from " Python.h" :
86 ctypedef char * const_char_ptr " const char*"
97 ctypedef char * const_void_ptr " const void*"
108 ctypedef struct PyObject
119 cdef int PyObject_AsReadBuffer(object o, const_void_ptr* buff, Py_ssize_t* buf_len) except - 1
12- char * __FILE__
13- int __LINE__
1410
1511from libc.stdlib cimport *
1612from libc.string cimport *
1713from libc.limits cimport *
18-
14+ import warnings
1915
2016cdef extern from " pack.h" :
2117 struct msgpack_packer:
@@ -218,7 +214,9 @@ cdef extern from "unpack.h":
218214 void template_init(template_context* ctx)
219215 object template_data(template_context* ctx)
220216
221- cdef inline init_ctx(template_context * ctx, object object_hook, object object_pairs_hook, object list_hook, bint use_list, encoding, unicode_errors):
217+ cdef inline init_ctx(template_context * ctx,
218+ object object_hook, object object_pairs_hook, object list_hook,
219+ bint use_list, char * encoding, char * unicode_errors):
222220 template_init(ctx)
223221 ctx.user.use_list = use_list
224222 ctx.user.object_hook = ctx.user.list_hook = < PyObject* > NULL
@@ -244,20 +242,8 @@ cdef inline init_ctx(template_context *ctx, object object_hook, object object_pa
244242 raise TypeError (" list_hook must be a callable." )
245243 ctx.user.list_hook = < PyObject* > list_hook
246244
247- if encoding is None :
248- ctx.user.encoding = NULL
249- ctx.user.unicode_errors = NULL
250- else :
251- if isinstance (encoding, unicode ):
252- _bencoding = encoding.encode(' ascii' )
253- else :
254- _bencoding = encoding
255- ctx.user.encoding = PyBytes_AsString(_bencoding)
256- if isinstance (unicode_errors, unicode ):
257- _berrors = unicode_errors.encode(' ascii' )
258- else :
259- _berrors = unicode_errors
260- ctx.user.unicode_errors = PyBytes_AsString(_berrors)
245+ ctx.user.encoding = encoding
246+ ctx.user.unicode_errors = unicode_errors
261247
262248def unpackb (object packed , object object_hook = None , object list_hook = None ,
263249 use_list = None , encoding = None , unicode_errors = " strict" ,
@@ -273,13 +259,26 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
273259
274260 cdef char * buf
275261 cdef Py_ssize_t buf_len
262+ cdef char * cenc = NULL
263+ cdef char * cerr = NULL
276264
277265 PyObject_AsReadBuffer(packed, < const_void_ptr* > & buf, & buf_len)
278266
279267 if use_list is None :
280268 warnings.warn(" Set use_list explicitly." , category = DeprecationWarning , stacklevel = 1 )
281269 use_list = 0
282- init_ctx(& ctx, object_hook, object_pairs_hook, list_hook, use_list, encoding, unicode_errors)
270+
271+ if encoding is not None :
272+ if isinstance (encoding, unicode ):
273+ encoding = encoding.encode(' ascii' )
274+ cenc = PyBytes_AsString(encoding)
275+
276+ if unicode_errors is not None :
277+ if isinstance (unicode_errors, unicode ):
278+ unicode_errors = unicode_errors.encode(' ascii' )
279+ cerr = PyBytes_AsString(unicode_errors)
280+
281+ init_ctx(& ctx, object_hook, object_pairs_hook, list_hook, use_list, cenc, cerr)
283282 ret = template_execute(& ctx, buf, buf_len, & off, 1 )
284283 if ret == 1 :
285284 obj = template_data(& ctx)
@@ -361,10 +360,7 @@ cdef class Unpacker(object):
361360 cdef object file_like_read
362361 cdef Py_ssize_t read_size
363362 cdef object object_hook
364- cdef object _bencoding
365- cdef object _berrors
366- cdef char * encoding
367- cdef char * unicode_errors
363+ cdef object encoding, unicode_errors
368364 cdef size_t max_buffer_size
369365
370366 def __cinit__ (self ):
@@ -378,6 +374,7 @@ cdef class Unpacker(object):
378374 object object_hook = None , object object_pairs_hook = None , object list_hook = None ,
379375 encoding = None , unicode_errors = ' strict' , int max_buffer_size = 0 ,
380376 ):
377+ cdef char * cenc= NULL , * cerr= NULL
381378 if use_list is None :
382379 warnings.warn(" Set use_list explicitly." , category = DeprecationWarning , stacklevel = 1 )
383380 use_list = 0
@@ -401,7 +398,20 @@ cdef class Unpacker(object):
401398 self .buf_size = read_size
402399 self .buf_head = 0
403400 self .buf_tail = 0
404- init_ctx(& self .ctx, object_hook, object_pairs_hook, list_hook, use_list, encoding, unicode_errors)
401+
402+ if encoding is not None :
403+ if isinstance (encoding, unicode ):
404+ encoding = encoding.encode(' ascii' )
405+ self .encoding = encoding
406+ cenc = PyBytes_AsString(encoding)
407+
408+ if unicode_errors is not None :
409+ if isinstance (unicode_errors, unicode ):
410+ unicode_errors = unicode_errors.encode(' ascii' )
411+ self .unicode_errors = unicode_errors
412+ cerr = PyBytes_AsString(unicode_errors)
413+
414+ init_ctx(& self .ctx, object_hook, object_pairs_hook, list_hook, use_list, cenc, cerr)
405415
406416 def feed (self , object next_bytes ):
407417 cdef char * buf
0 commit comments