File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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+
150158def 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" }
You can’t perform that action at this time.
0 commit comments