@@ -213,6 +213,32 @@ cdef extern from "unpack.h":
213213 void template_init(template_context* ctx)
214214 object template_data(template_context* ctx)
215215
216+ cdef inline init_ctx(template_context * ctx, object object_hook, object list_hook, bint use_list, encoding, unicode_errors):
217+ template_init(ctx)
218+ ctx.user.use_list = use_list
219+ ctx.user.object_hook = ctx.user.list_hook = < PyObject* > NULL
220+ if object_hook is not None :
221+ if not PyCallable_Check(object_hook):
222+ raise TypeError (" object_hook must be a callable." )
223+ ctx.user.object_hook = < PyObject* > object_hook
224+ if list_hook is not None :
225+ if not PyCallable_Check(list_hook):
226+ raise TypeError (" list_hook must be a callable." )
227+ ctx.user.list_hook = < PyObject* > list_hook
228+ if encoding is None :
229+ ctx.user.encoding = NULL
230+ ctx.user.unicode_errors = NULL
231+ else :
232+ if isinstance (encoding, unicode ):
233+ _bencoding = encoding.encode(' ascii' )
234+ else :
235+ _bencoding = encoding
236+ ctx.user.encoding = PyBytes_AsString(_bencoding)
237+ if isinstance (unicode_errors, unicode ):
238+ _berrors = unicode_errors.encode(' ascii' )
239+ else :
240+ _berrors = unicode_errors
241+ ctx.user.unicode_errors = PyBytes_AsString(_berrors)
216242
217243def unpackb (object packed , object object_hook = None , object list_hook = None ,
218244 bint use_list = 0 , encoding = None , unicode_errors = " strict" ,
@@ -229,34 +255,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
229255 cdef Py_ssize_t buf_len
230256 PyObject_AsReadBuffer(packed, < const_void_ptr* > & buf, & buf_len)
231257
232- if encoding is None :
233- enc = NULL
234- err = NULL
235- else :
236- if isinstance (encoding, unicode ):
237- bencoding = encoding.encode(' ascii' )
238- else :
239- bencoding = encoding
240- if isinstance (unicode_errors, unicode ):
241- berrors = unicode_errors.encode(' ascii' )
242- else :
243- berrors = unicode_errors
244- enc = PyBytes_AsString(bencoding)
245- err = PyBytes_AsString(berrors)
246-
247- template_init(& ctx)
248- ctx.user.use_list = use_list
249- ctx.user.object_hook = ctx.user.list_hook = NULL
250- ctx.user.encoding = < const_char_ptr> enc
251- ctx.user.unicode_errors = < const_char_ptr> err
252- if object_hook is not None :
253- if not PyCallable_Check(object_hook):
254- raise TypeError (" object_hook must be a callable." )
255- ctx.user.object_hook = < PyObject* > object_hook
256- if list_hook is not None :
257- if not PyCallable_Check(list_hook):
258- raise TypeError (" list_hook must be a callable." )
259- ctx.user.list_hook = < PyObject* > list_hook
258+ init_ctx(& ctx, object_hook, list_hook, use_list, encoding, unicode_errors)
260259 ret = template_execute(& ctx, buf, buf_len, & off, 1 )
261260 if ret == 1 :
262261 obj = template_data(& ctx)
@@ -348,7 +347,6 @@ cdef class Unpacker(object):
348347 def __init__ (self , file_like = None , Py_ssize_t read_size = 0 , bint use_list = 0 ,
349348 object object_hook = None , object list_hook = None ,
350349 encoding = None , unicode_errors = ' strict' , int max_buffer_size = 0 ,
351- object object_pairs_hook = None ,
352350 ):
353351 self .use_list = use_list
354352 self .file_like = file_like
@@ -370,31 +368,7 @@ cdef class Unpacker(object):
370368 self .buf_size = read_size
371369 self .buf_head = 0
372370 self .buf_tail = 0
373- template_init(& self .ctx)
374- self .ctx.user.use_list = use_list
375- self .ctx.user.object_hook = self .ctx.user.list_hook = < PyObject* > NULL
376- if object_hook is not None :
377- if not PyCallable_Check(object_hook):
378- raise TypeError (" object_hook must be a callable." )
379- self .ctx.user.object_hook = < PyObject* > object_hook
380- if list_hook is not None :
381- if not PyCallable_Check(list_hook):
382- raise TypeError (" list_hook must be a callable." )
383- self .ctx.user.list_hook = < PyObject* > list_hook
384- if encoding is None :
385- self .ctx.user.encoding = NULL
386- self .ctx.user.unicode_errors = NULL
387- else :
388- if isinstance (encoding, unicode ):
389- self ._bencoding = encoding.encode(' ascii' )
390- else :
391- self ._bencoding = encoding
392- self .ctx.user.encoding = PyBytes_AsString(self ._bencoding)
393- if isinstance (unicode_errors, unicode ):
394- self ._berrors = unicode_errors.encode(' ascii' )
395- else :
396- self ._berrors = unicode_errors
397- self .ctx.user.unicode_errors = PyBytes_AsString(self ._berrors)
371+ init_ctx(& self .ctx, object_hook, list_hook, use_list, encoding, unicode_errors)
398372
399373 def feed (self , object next_bytes ):
400374 cdef char * buf
0 commit comments