Skip to content

Make strict_map_key default to True #392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2019
Merged
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
8 changes: 3 additions & 5 deletions msgpack/_unpacker.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ cdef inline int get_data_from_buffer(object obj,


def unpackb(object packed, *, object object_hook=None, object list_hook=None,
bint use_list=True, bint raw=False, bint strict_map_key=False,
bint use_list=True, bint raw=False, bint strict_map_key=True,
unicode_errors=None,
object_pairs_hook=None, ext_hook=ExtType,
Py_ssize_t max_str_len=-1,
Expand Down Expand Up @@ -221,9 +221,7 @@ cdef class Unpacker(object):
Otherwise, unpack to Python str by decoding with UTF-8 encoding (default).

:param bool strict_map_key:
If true, only str or bytes are accepted for map (dict) keys.
It's False by default for backward-compatibility.
But it will be True from msgpack 1.0.
If true (default), only str or bytes are accepted for map (dict) keys.

:param callable object_hook:
When specified, it should be callable.
Expand Down Expand Up @@ -305,7 +303,7 @@ cdef class Unpacker(object):
self.buf = NULL

def __init__(self, file_like=None, *, Py_ssize_t read_size=0,
bint use_list=True, bint raw=False, bint strict_map_key=False,
bint use_list=True, bint raw=False, bint strict_map_key=True,
object object_hook=None, object object_pairs_hook=None, object list_hook=None,
unicode_errors=None, Py_ssize_t max_buffer_size=0,
object ext_hook=ExtType,
Expand Down
6 changes: 2 additions & 4 deletions msgpack/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ class Unpacker(object):
Otherwise, unpack to Python str by decoding with UTF-8 encoding (default).

:param bool strict_map_key:
If true, only str or bytes are accepted for map (dict) keys.
It's False by default for backward-compatibility.
But it will be True from msgpack 1.0.
If true (default), only str or bytes are accepted for map (dict) keys.

:param callable object_hook:
When specified, it should be callable.
Expand Down Expand Up @@ -249,7 +247,7 @@ def __init__(
read_size=0,
use_list=True,
raw=False,
strict_map_key=False,
strict_map_key=True,
object_hook=None,
object_pairs_hook=None,
list_hook=None,
Expand Down
2 changes: 1 addition & 1 deletion test/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_array32():

def match(obj, buf):
assert packb(obj) == buf
assert unpackb(buf, use_list=0) == obj
assert unpackb(buf, use_list=0, strict_map_key=False) == obj


def test_match():
Expand Down
2 changes: 1 addition & 1 deletion test/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def check(src, should, use_list=0, raw=True):
assert unpackb(src, use_list=use_list, raw=raw) == should
assert unpackb(src, use_list=use_list, raw=raw, strict_map_key=False) == should


def testSimpleValue():
Expand Down
4 changes: 2 additions & 2 deletions test/test_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ def test_max_map_len():
d = {1: 2, 3: 4, 5: 6}
packed = packb(d)

unpacker = Unpacker(max_map_len=3)
unpacker = Unpacker(max_map_len=3, strict_map_key=False)
unpacker.feed(packed)
assert unpacker.unpack() == d

unpacker = Unpacker(max_map_len=2)
unpacker = Unpacker(max_map_len=2, strict_map_key=False)
with pytest.raises(UnpackValueError):
unpacker.feed(packed)
unpacker.unpack()
Expand Down
9 changes: 6 additions & 3 deletions test/test_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ def test_decode_pairs_hook():
packed = packb([3, {1: 2, 3: 4}])
prod_sum = 1 * 2 + 3 * 4
unpacked = unpackb(
packed, object_pairs_hook=lambda l: sum(k * v for k, v in l), use_list=1
packed,
object_pairs_hook=lambda l: sum(k * v for k, v in l),
use_list=1,
strict_map_key=False,
)
assert unpacked[1] == prod_sum

Expand Down Expand Up @@ -70,10 +73,10 @@ def bad_complex_decoder(o):
def test_an_exception_in_objecthook1():
with raises(DecodeError):
packed = packb({1: {"__complex__": True, "real": 1, "imag": 2}})
unpackb(packed, object_hook=bad_complex_decoder)
unpackb(packed, object_hook=bad_complex_decoder, strict_map_key=False)


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)
unpackb(packed, list_hook=bad_complex_decoder, use_list=1, strict_map_key=False)
6 changes: 3 additions & 3 deletions test/test_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


def check(data, use_list=False):
re = unpackb(packb(data), use_list=use_list)
re = unpackb(packb(data), use_list=use_list, strict_map_key=False)
assert re == data


Expand Down Expand Up @@ -166,7 +166,7 @@ def testMapSize(sizes=[0, 5, 50, 1000]):
bio.write(packer.pack(i * 2)) # value

bio.seek(0)
unpacker = Unpacker(bio)
unpacker = Unpacker(bio, strict_map_key=False)
for size in sizes:
assert unpacker.unpack() == dict((i, i * 2) for i in range(size))

Expand All @@ -186,7 +186,7 @@ def test_pairlist():
pairlist = [(b"a", 1), (2, b"b"), (b"foo", b"bar")]
packer = Packer()
packed = packer.pack_map_pairs(pairlist)
unpacked = unpackb(packed, object_pairs_hook=list)
unpacked = unpackb(packed, object_pairs_hook=list, strict_map_key=False)
assert pairlist == unpacked


Expand Down
2 changes: 1 addition & 1 deletion test/test_sequnpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_unpack_tell():
pack(m, stream)
offsets.append(stream.tell())
stream.seek(0)
unpacker = Unpacker(stream)
unpacker = Unpacker(stream, strict_map_key=False)
for m, o in zip(messages, offsets):
m2 = next(unpacker)
assert m == m2
Expand Down