8000 Implement strict_map_key to fallback unpacker. · loude/msgpack-python@dc1b993 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc1b993

Browse files
committed
Implement strict_map_key to fallback unpacker.
1 parent e9086a3 commit dc1b993

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

msgpack/fallback.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ class Unpacker(object):
179179
180180
*encoding* option which is deprecated overrides this option.
181181
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+
182187
:param callable object_hook:
183188
When specified, it should be callable.
184189
Unpacker calls it with a dict argument after unpacking msgpack map.
@@ -241,7 +246,7 @@ class Unpacker(object):
241246
Other exceptions can be raised during unpacking.
242247
"""
243248

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,
245250
object_hook=None, object_pairs_hook=None, list_hook=None,
246251
encoding=None, unicode_errors=None, max_buffer_size=0,
247252
ext_hook=ExtType,
@@ -286,6 +291,7 @@ def __init__(self, file_like=None, read_size=0, use_list=True, raw=True,
286291
raise ValueError("read_size must be smaller than max_buffer_size")
287292
self._read_size = read_size or min(self._max_buffer_size, 16*1024)
288293
self._raw = bool(raw)
294+
self._strict_map_key = bool(strict_map_key)
289295
self._encoding = encoding
290296
self._unicode_errors = unicode_errors
291297
self._use_list = use_list
@@ -633,6 +639,8 @@ def _unpack(self, execute=EX_CONSTRUCT):
633639
ret = {}
634640
for _ in xrange(n):
635641
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)))
636644
ret[key] = self._unpack(EX_CONSTRUCT)
637645
if self._object_hook is not None:
638646
ret = self._object_hook(ret)

0 commit comments

Comments
 (0)
0