@@ -27,6 +27,7 @@ cdef extern from "unpack.h":
27
27
bint use_list
28
28
bint raw
29
29
bint has_pairs_hook # call object_hook with k-v pairs
30
+ bint strict_map_key
30
31
PyObject* object_hook
31
32
PyObject* list_hook
32
33
PyObject* ext_hook
@@ -56,14 +57,15 @@ cdef extern from "unpack.h":
56
57
cdef inline init_ctx(unpack_context * ctx,
57
58
object object_hook, object object_pairs_hook,
58
59
object list_hook, object ext_hook,
59
- bint use_list, bint raw,
60
+ bint use_list, bint raw, bint strict_map_key,
60
61
const char * encoding, const char * unicode_errors,
61
62
Py_ssize_t max_str_len, Py_ssize_t max_bin_len,
62
63
Py_ssize_t max_array_len, Py_ssize_t max_map_len,
63
64
Py_ssize_t max_ext_len):
64
65
unpack_init(ctx)
65
66
ctx.user.use_list = use_list
66
67
ctx.user.raw = raw
68
+ ctx.user.strict_map_key = strict_map_key
67
69
ctx.user.object_hook = ctx.user.list_hook = < PyObject* > NULL
68
70
ctx.user.max_str_len = max_str_len
69
71
ctx.user.max_bin_len = max_bin_len
@@ -140,7 +142,7 @@ cdef inline int get_data_from_buffer(object obj,
140
142
return 1
141
143
142
144
def unpackb (object packed , object object_hook = None , object list_hook = None ,
143
- bint use_list = True , bint raw = True ,
145
+ bint use_list = True , bint raw = True , bint strict_map_key = False ,
144
146
encoding = None , unicode_errors = None ,
145
147
object_pairs_hook = None , ext_hook = ExtType,
146
148
Py_ssize_t max_str_len = 1024 * 1024 ,
@@ -180,7 +182,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
180
182
get_data_from_buffer(packed, & view, & buf, & buf_len, & new_protocol)
181
183
try :
182
184
init_ctx(& ctx, object_hook, object_pairs_hook, list_hook, ext_hook,
183
- use_list, raw, cenc, cerr,
185
+ use_list, raw, strict_map_key, cenc, cerr,
184
186
max_str_len, max_bin_len, max_array_len, max_map_len, max_ext_len)
185
187
ret = unpack_construct(& ctx, buf, buf_len, & off)
186
188
finally :
@@ -236,6 +238,11 @@ cdef class Unpacker(object):
236
238
237
239
*encoding* option which is deprecated overrides this option.
238
240
241
+ :param bool strict_map_key:
242
+ If true, only str or bytes are accepted for map (dict) keys.
243
+ It's False by default for backward-compatibility.
244
+ But it will be True from msgpack 1.0.
245
+
239
246
:param callable object_hook:
240
247
When specified, it should be callable.
241
248
Unpacker calls it with a dict argument after unpacking msgpack map.
@@ -318,7 +325,7 @@ cdef class Unpacker(object):
318
325
self .buf = NULL
319
326
320
327
def __init__ (self , file_like = None , Py_ssize_t read_size = 0 ,
321
- bint use_list = True , bint raw = True ,
328
+ bint use_list = True , bint raw = True , bint strict_map_key = False ,
322
329
object object_hook = None , object object_pairs_hook = None , object list_hook = None ,
323
330
encoding = None , unicode_errors = None , Py_ssize_t max_buffer_size = 0 ,
324
331
object ext_hook = ExtType,
@@ -366,7 +373,7 @@ cdef class Unpacker(object):
366
373
cerr = unicode_errors
367
374
368
375
init_ctx(& self .ctx, object_hook, object_pairs_hook, list_hook,
369
- ext_hook, use_list, raw, cenc, cerr,
376
+ ext_hook, use_list, raw, strict_map_key, cenc, cerr,
370
377
max_str_len, max_bin_len, max_array_len,
371
378
max_map_len, max_ext_len)
372
379
0 commit comments