Skip to content

Commit 4638b2e

Browse files
Vansh8803vanshdhameliya-pixel
authored andcommitted
fallback: match the C extension's strict_map_key error message
1 parent 3650629 commit 4638b2e

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎msgpack/fallback.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,15 +539,21 @@ def _unpack(self, execute=EX_CONSTRUCT):
539539
for _ in range(n):
540540
key = self._unpack(EX_CONSTRUCT)
541541
if self._strict_map_key and type(key) not in (str, bytes):
542-
raise ValueError("%s is not allowed for map key" % str(type(key)))
542+
raise ValueError(
543+
"%s is not allowed for map key when strict_map_key=True"
544+
% type(key).__name__
545+
)
543546
pairs.append((key, self._unpack(EX_CONSTRUCT)))
544547
ret = self._object_pairs_hook(pairs)
545548
else:
546549
ret = {}
547550
for _ in range(n):
548551
key = self._unpack(EX_CONSTRUCT)
549552
if self._strict_map_key and type(key) not in (str, bytes):
550-
raise ValueError("%s is not allowed for map key" % str(type(key)))
553+
raise ValueError(
554+
"%s is not allowed for map key when strict_map_key=True"
555+
% type(key).__name__
556+
)
551557
if isinstance(key, str):
552558
key = sys.intern(key)
553559
ret[key] = self._unpack(EX_CONSTRUCT)

‎test/test_except.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ def test_strict_map_key():
147147
unpackb(packed, raw=False, strict_map_key=True)
148148

149149

150+
def test_strict_map_key_error_message():
151+
# The pure-Python fallback should report the same message as the C
152+
# extension when a map key has a disallowed type.
153+
packed = packb({42: 1}, use_bin_type=True)
154+
with raises(ValueError, match="int is not allowed for map key when strict_map_key=True"):
155+
unpackb(packed, raw=False, strict_map_key=True)
156+
157+
150158
def test_strict_map_key_with_object_pairs_hook():
151159
# strict_map_key should be enforced even when object_pairs_hook is set
152160
invalid = {42: "value"}

0 commit comments

Comments
 (0)