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
2 changes: 1 addition & 1 deletion msgpack/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ def _unpack(self, execute=EX_CONSTRUCT):
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)))
if isinstance(key, str):
if type(key) is str:
key = sys.intern(key)
ret[key] = self._unpack(EX_CONSTRUCT)
if self._object_hook is not None:
Expand Down
18 changes: 17 additions & 1 deletion test/test_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pytest import raises

from msgpack import packb, unpackb
from msgpack import ExtType, packb, unpackb


def _decode_complex(obj):
Expand Down Expand Up @@ -92,3 +92,19 @@ def test_an_exception_in_objecthook2():
with raises(DecodeError):
packed = packb({1: [{"__complex__": True, "real": 1, "imag": 2}]})
unpackb(packed, list_hook=bad_complex_decoder, use_list=1, strict_map_key=False)


def test_str_subclass_map_key_from_ext_hook():
class Key(str):
pass

key = Key("name")
packed = packb({ExtType(1, b"name"): 42})
options = {"ext_hook": lambda code, data: key}

result = unpackb(packed, strict_map_key=False, **options)
assert result == {key: 42}
assert next(iter(result)) is key

with raises(ValueError):
unpackb(packed, strict_map_key=True, **options)
Loading