@@ -208,8 +208,10 @@ cdef extern from "unpack.h":
208208 unsigned int ct
209209 PyObject* key
210210
211- int template_execute(template_context* ctx, const_char_ptr data,
212- size_t len , size_t* off, bint construct) except - 1
211+ ctypedef int (* execute_fn)(template_context* ctx, const_char_ptr data,
212+ size_t len , size_t* off) except - 1
213+ execute_fn template_construct
214+ execute_fn template_skip
213215 void template_init(template_context* ctx)
214216 object template_data(template_context* ctx)
215217
@@ -257,7 +259,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
257259 if not PyCallable_Check(list_hook):
258260 raise TypeError (" list_hook must be a callable." )
259261 ctx.user.list_hook = < PyObject* > list_hook
260- ret = template_execute (& ctx, buf, buf_len, & off, 1 )
262+ ret = template_construct (& ctx, buf, buf_len, & off)
261263 if ret == 1 :
262264 obj = template_data(& ctx)
263265 if off < buf_len:
@@ -455,16 +457,13 @@ cdef class Unpacker(object):
455457 else :
456458 self .file_like = None
457459
458- cdef object _unpack(self , bint construct ):
460+ cdef object _unpack(self , execute_fn execute ):
459461 cdef int ret
460462 cdef object obj
461463 while 1 :
462- ret = template_execute (& self .ctx, self .buf, self .buf_tail, & self .buf_head, construct )
464+ ret = execute (& self .ctx, self .buf, self .buf_tail, & self .buf_head)
463465 if ret == 1 :
464- if construct:
465- obj = template_data(& self .ctx)
466- else :
467- obj = None
466+ obj = template_data(& self .ctx)
468467 template_init(& self .ctx)
469468 return obj
470469 elif ret == 0 :
@@ -477,17 +476,17 @@ cdef class Unpacker(object):
477476
478477 def unpack (self ):
479478 """ unpack one object"""
480- return self ._unpack(1 )
479+ return self ._unpack(template_construct )
481480
482481 def skip (self ):
483482 """ read and ignore one object, returning None"""
484- return self ._unpack(0 )
483+ return self ._unpack(template_skip )
485484
486485 def __iter__ (self ):
487486 return self
488487
489488 def __next__ (self ):
490- return self ._unpack(1 )
489+ return self ._unpack(template_construct )
491490
492491 # for debug.
493492 # def _buf(self):
0 commit comments