Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions msgpack/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,15 +539,21 @@ def _unpack(self, execute=EX_CONSTRUCT):
for _ in range(n):
key = self._unpack(EX_CONSTRUCT)
if self._strict_map_key and type(key) not in (str, bytes):
raise ValueError("%s is not allowed for map key" % str(type(key)))
raise ValueError(
"%s is not allowed for map key when strict_map_key=True"
% type(key).__name__
)
pairs.append((key, self._unpack(EX_CONSTRUCT)))
ret = self._object_pairs_hook(pairs)
else:
ret = {}
for _ in range(n):
key = self._unpack(EX_CONSTRUCT)
if self._strict_map_key and type(key) not in (str, bytes):
raise ValueError("%s is not allowed for map key" % str(type(key)))
raise ValueError(
"%s is not allowed for map key when strict_map_key=True"
% type(key).__name__
)
if isinstance(key, str):
key = sys.intern(key)
ret[key] = self._unpack(EX_CONSTRUCT)
Expand Down
8 changes: 8 additions & 0 deletions test/test_except.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ def test_strict_map_key():
unpackb(packed, raw=False, strict_map_key=True)


def test_strict_map_key_error_message():
# The pure-Python fallback should report the same message as the C
# extension when a map key has a disallowed type.
packed = packb({42: 1}, use_bin_type=True)
with raises(ValueError, match="int is not allowed for map key when strict_map_key=True"):
unpackb(packed, raw=False, strict_map_key=True)


def test_strict_map_key_with_object_pairs_hook():
# strict_map_key should be enforced even when object_pairs_hook is set
invalid = {42: "value"}
Expand Down
Loading