@@ -197,6 +197,7 @@ cdef extern from "unpack.h":
197197 ctypedef struct msgpack_user:
198198 int use_list
199199 PyObject* object_hook
200+ bint has_pairs_hook # call object_hook with k-v pairs
200201 PyObject* list_hook
201202 char * encoding
202203 char * unicode_errors
@@ -213,18 +214,32 @@ cdef extern from "unpack.h":
213214 void template_init(template_context* ctx)
214215 object template_data(template_context* ctx)
215216
216- cdef inline init_ctx(template_context * ctx, object object_hook, object list_hook, bint use_list, encoding, unicode_errors):
217+ cdef inline init_ctx(template_context * ctx, object object_hook, object object_pairs_hook, object list_hook, bint use_list, encoding, unicode_errors):
217218 template_init(ctx)
218219 ctx.user.use_list = use_list
219220 ctx.user.object_hook = ctx.user.list_hook = < PyObject* > NULL
221+
222+ if object_hook is not None and object_pairs_hook is not None :
223+ raise ValueError (" object_pairs_hook and object_hook are mutually exclusive." )
224+
220225 if object_hook is not None :
221226 if not PyCallable_Check(object_hook):
222227 raise TypeError (" object_hook must be a callable." )
223228 ctx.user.object_hook = < PyObject* > object_hook
229+
230+ if object_pairs_hook is None :
231+ ctx.user.has_pairs_hook = False
232+ else :
233+ if not PyCallable_Check(object_pairs_hook):
234+ raise TypeError (" object_pairs_hook must be a callable." )
235+ ctx.user.object_hook = < PyObject* > object_pairs_hook
236+ ctx.user.has_pairs_hook = True
237+
224238 if list_hook is not None :
225239 if not PyCallable_Check(list_hook):
226240 raise TypeError (" list_hook must be a callable." )
227241 ctx.user.list_hook = < PyObject* > list_hook
242+
228243 if encoding is None :
229244 ctx.user.encoding = NULL
230245 ctx.user.unicode_errors = NULL
@@ -240,7 +255,7 @@ cdef inline init_ctx(template_context *ctx, object object_hook, object list_hook
240255 _berrors = unicode_errors
241256 ctx.user.unicode_errors = PyBytes_AsString(_berrors)
242257
243- def unpackb (object packed , object object_hook = None , object list_hook = None ,
258+ def unpackb (object packed , object object_hook = None , object object_pairs_hook = None , object list_hook = None ,
244259 bint use_list = 0 , encoding = None , unicode_errors = " strict" ,
245260 ):
246261 """ Unpack packed_bytes to object. Returns an unpacked object.
@@ -255,7 +270,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
255270 cdef Py_ssize_t buf_len
256271 PyObject_AsReadBuffer(packed, < const_void_ptr* > & buf, & buf_len)
257272
258- init_ctx(& ctx, object_hook, list_hook, use_list, encoding, unicode_errors)
273+ init_ctx(& ctx, object_hook, object_pairs_hook, list_hook, use_list, encoding, unicode_errors)
259274 ret = template_execute(& ctx, buf, buf_len, & off, 1 )
260275 if ret == 1 :
261276 obj = template_data(& ctx)
@@ -266,15 +281,15 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
266281 return None
267282
268283
269- def unpack (object stream , object object_hook = None , object list_hook = None ,
284+ def unpack (object stream , object object_hook = None , object object_pairs_hook = None , object list_hook = None ,
270285 bint use_list = 0 , encoding = None , unicode_errors = " strict" ,
271286 ):
272287 """ Unpack an object from `stream`.
273288
274289 Raises `ValueError` when `stream` has extra bytes.
275290 """
276291 return unpackb(stream.read(), use_list = use_list,
277- object_hook = object_hook, list_hook = list_hook,
292+ object_hook = object_hook, object_pairs_hook = object_pairs_hook, list_hook = list_hook,
278293 encoding = encoding, unicode_errors = unicode_errors,
279294 )
280295
@@ -294,7 +309,10 @@ cdef class Unpacker(object):
294309 Otherwise, it is deserialized to Python tuple. (default: False)
295310
296311 `object_hook` is same to simplejson. If it is not None, it should be callable
297- and Unpacker calls it when deserializing key-value.
312+ and Unpacker calls it with a dict argument after deserializing a map.
313+
314+ `object_pairs_hook` is same to simplejson. If it is not None, it should be callable
315+ and Unpacker calls it with a list of key-value pairs after deserializing a map.
298316
299317 `encoding` is encoding used for decoding msgpack bytes. If it is None (default),
300318 msgpack bytes is deserialized to Python bytes.
@@ -345,7 +363,7 @@ cdef class Unpacker(object):
345363 self .buf = NULL
346364
347365 def __init__ (self , file_like = None , Py_ssize_t read_size = 0 , bint use_list = 0 ,
348- object object_hook = None , object list_hook = None ,
366+ object object_hook = None , object object_pairs_hook = None , object list_hook = None ,
349367 encoding = None , unicode_errors = ' strict' , int max_buffer_size = 0 ,
350368 ):
351369 self .use_list = use_list
@@ -368,7 +386,7 @@ cdef class Unpacker(object):
368386 self .buf_size = read_size
369387 self .buf_head = 0
370388 self .buf_tail = 0
371- init_ctx(& self .ctx, object_hook, list_hook, use_list, encoding, unicode_errors)
389+ init_ctx(& self .ctx, object_hook, object_pairs_hook, list_hook, use_list, encoding, unicode_errors)
372390
373391 def feed (self , object next_bytes ):
374392 cdef char * buf
0 commit comments