diff --git a/msgpack/fallback.py b/msgpack/fallback.py index 0cbd8733..8b7d144b 100644 --- a/msgpack/fallback.py +++ b/msgpack/fallback.py @@ -539,7 +539,10 @@ 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: @@ -547,7 +550,10 @@ 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__ + ) if isinstance(key, str): key = sys.intern(key) ret[key] = self._unpack(EX_CONSTRUCT) diff --git a/test/test_except.py b/test/test_except.py index 1e9c0045..f3b17f7f 100644 --- a/test/test_except.py +++ b/test/test_except.py @@ -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"}