@@ -197,6 +197,7 @@ cdef extern from "unpack.h":
197
197
ctypedef struct msgpack_user:
198
198
int use_list
199
199
PyObject* object_hook
200
+ bint has_pairs_hook # call object_hook with k-v pairs
200
201
PyObject* list_hook
201
202
char * encoding
202
203
char * unicode_errors
@@ -213,18 +214,32 @@ cdef extern from "unpack.h":
213
214
void template_init(template_context* ctx)
214
215
object template_data(template_context* ctx)
215
216
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):
217
218
template_init(ctx)
218
219
ctx.user.use_list = use_list
219
220
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
+
220
225
if object_hook is not None :
221
226
if not PyCallable_Check(object_hook):
222
227
raise TypeError (" object_hook must be a callable." )
223
228
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
+
224
238
if list_hook is not None :
225
239
if not PyCallable_Check(list_hook):
226
240
raise TypeError (" list_hook must be a callable." )
227
241
ctx.user.list_hook = < PyObject* > list_hook
242
+
228
243
if encoding is None :
229
244
ctx.user.encoding = NULL
230
245
ctx.user.unicode_errors = NULL
@@ -240,7 +255,7 @@ cdef inline init_ctx(template_context *ctx, object object_hook, object list_hook
240
255
_berrors = unicode_errors
241
256
ctx.user.unicode_errors = PyBytes_AsString(_berrors)
242
257
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 ,
244
259
bint use_list = 0 , encoding = None , unicode_errors = " strict" ,
245
260
):
246
261
""" 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,
255
270
cdef Py_ssize_t buf_len
256
271
PyObject_AsReadBuffer(packed, < const_void_ptr* > & buf, & buf_len)
257
272
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)
259
274
ret = template_execute(& ctx, buf, buf_len, & off, 1 )
260
275
if ret == 1 :
261
276
obj = template_data(& ctx)
@@ -266,15 +281,15 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
266
281
return None
267
282
268
283
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 ,
270
285
bint use_list = 0 , encoding = None , unicode_errors = " strict" ,
271
286
):
272
287
""" Unpack an object from `stream`.
273
288
274
289
Raises `ValueError` when `stream` has extra bytes.
275
290
"""
276
291
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,
278
293
encoding = encoding, unicode_errors = unicode_errors,
279
294
)
280
295
@@ -294,7 +309,10 @@ cdef class Unpacker(object):
294
309
Otherwise, it is deserialized to Python tuple. (default: False)
295
310
296
311
`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.
298
316
299
317
`encoding` is encoding used for decoding msgpack bytes. If it is None (default),
300
318
msgpack bytes is deserialized to Python bytes.
@@ -345,7 +363,7 @@ cdef class Unpacker(object):
345
363
self .buf = NULL
346
364
347
365
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 ,
349
367
encoding = None , unicode_errors = ' strict' , int max_buffer_size = 0 ,
350
368
):
351
369
self .use_list = use_list
@@ -368,7 +386,7 @@ cdef class Unpacker(object):
368
386
self .buf_size = read_size
369
387
self .buf_head = 0
370
388
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)
372
390
373
391
def feed (self , object next_bytes ):
374
392
cdef char * buf
0 commit comments