@@ -179,6 +179,11 @@ class Unpacker(object):
179
179
180
180
*encoding* option which is deprecated overrides this option.
181
181
182
+ :param bool strict_map_key:
183
+ If true, only str or bytes are accepted for map (dict) keys.
184
+ It's False by default for backward-compatibility.
185
+ But it will be True from msgpack 1.0.
186
+
182
187
:param callable object_hook:
183
188
When specified, it should be callable.
184
189
Unpacker calls it with a dict argument after unpacking msgpack map.
@@ -241,7 +246,7 @@ class Unpacker(object):
241
246
Other exceptions can be raised during unpacking.
242
247
"""
243
248
244
- def __init__ (self , file_like = None , read_size = 0 , use_list = True , raw = True ,
249
+ def __init__ (self , file_like = None , read_size = 0 , use_list = True , raw = True , strict_map_key = False ,
245
250
object_hook = None , object_pairs_hook = None , list_hook = None ,
246
251
encoding = None , unicode_errors = None , max_buffer_size = 0 ,
247
252
ext_hook = ExtType ,
@@ -286,6 +291,7 @@ def __init__(self, file_like=None, read_size=0, use_list=True, raw=True,
286
291
raise ValueError ("read_size must be smaller than max_buffer_size" )
287
292
self ._read_size = read_size or min (self ._max_buffer_size , 16 * 1024 )
288
293
self ._raw = bool (raw )
294
+ self ._strict_map_key = bool (strict_map_key )
289
295
self ._encoding = encoding
290
296
self ._unicode_errors = unicode_errors
291
297
self ._use_list = use_list
@@ -633,6 +639,8 @@ def _unpack(self, execute=EX_CONSTRUCT):
633
639
ret = {}
634
640
for _ in xrange (n ):
635
641
key = self ._unpack (EX_CONSTRUCT )
642
+ if self ._strict_map_key and type (key ) not in (unicode , bytes ):
643
+ raise ValueError ("%s is not allowed for map key" % str (type (key )))
636
644
ret [key ] = self ._unpack (EX_CONSTRUCT )
637
645
if self ._object_hook is not None :
638
646
ret = self ._object_hook (ret )
0 commit comments