Make strict_map_key default to True (#392)

This commit is contained in:
Inada Naoki 2019-12-06 22:23:15 +09:00 committed by GitHub
parent 0fc0eb2f16
commit d8e3cf0563
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 19 additions and 20 deletions

View file

@ -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, 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, unicode_errors=None,
object_pairs_hook=None, ext_hook=ExtType, object_pairs_hook=None, ext_hook=ExtType,
Py_ssize_t max_str_len=-1, Py_ssize_t max_str_len=-1,
@ -221,9 +221,7 @@ cdef class Unpacker(object):
Otherwise, unpack to Python str by decoding with UTF-8 encoding (default). Otherwise, unpack to Python str by decoding with UTF-8 encoding (default).
:param bool strict_map_key: :param bool strict_map_key:
If true, only str or bytes are accepted for map (dict) keys. If true (default), 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.
:param callable object_hook: :param callable object_hook:
When specified, it should be callable. When specified, it should be callable.
@ -305,7 +303,7 @@ cdef class Unpacker(object):
self.buf = NULL self.buf = NULL
def __init__(self, file_like=None, *, Py_ssize_t read_size=0, 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, object object_hook=None, object object_pairs_hook=None, object list_hook=None,
unicode_errors=None, Py_ssize_t max_buffer_size=0, unicode_errors=None, Py_ssize_t max_buffer_size=0,
object ext_hook=ExtType, object ext_hook=ExtType,

View file

@ -175,9 +175,7 @@ class Unpacker(object):
Otherwise, unpack to Python str by decoding with UTF-8 encoding (default). Otherwise, unpack to Python str by decoding with UTF-8 encoding (default).
:param bool strict_map_key: :param bool strict_map_key:
If true, only str or bytes are accepted for map (dict) keys. If true (default), 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.
:param callable object_hook: :param callable object_hook:
When specified, it should be callable. When specified, it should be callable.
@ -249,7 +247,7 @@ class Unpacker(object):
read_size=0, read_size=0,
use_list=True, use_list=True,
raw=False, raw=False,
strict_map_key=False, strict_map_key=True,
object_hook=None, object_hook=None,
object_pairs_hook=None, object_pairs_hook=None,
list_hook=None, list_hook=None,

View file

@ -92,7 +92,7 @@ def test_array32():
def match(obj, buf): def match(obj, buf):
assert packb(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(): def test_match():

View file

@ -5,7 +5,7 @@ from msgpack import unpackb
def check(src, should, use_list=0, raw=True): 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(): def testSimpleValue():

View file

@ -87,11 +87,11 @@ def test_max_map_len():
d = {1: 2, 3: 4, 5: 6} d = {1: 2, 3: 4, 5: 6}
packed = packb(d) packed = packb(d)
unpacker = Unpacker(max_map_len=3) unpacker = Unpacker(max_map_len=3, strict_map_key=False)
unpacker.feed(packed) unpacker.feed(packed)
assert unpacker.unpack() == d assert unpacker.unpack() == d
unpacker = Unpacker(max_map_len=2) unpacker = Unpacker(max_map_len=2, strict_map_key=False)
with pytest.raises(UnpackValueError): with pytest.raises(UnpackValueError):
unpacker.feed(packed) unpacker.feed(packed)
unpacker.unpack() unpacker.unpack()

View file

@ -33,7 +33,10 @@ def test_decode_pairs_hook():
packed = packb([3, {1: 2, 3: 4}]) packed = packb([3, {1: 2, 3: 4}])
prod_sum = 1 * 2 + 3 * 4 prod_sum = 1 * 2 + 3 * 4
unpacked = unpackb( 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 assert unpacked[1] == prod_sum
@ -70,10 +73,10 @@ def bad_complex_decoder(o):
def test_an_exception_in_objecthook1(): def test_an_exception_in_objecthook1():
with raises(DecodeError): with raises(DecodeError):
packed = packb({1: {"__complex__": True, "real": 1, "imag": 2}}) 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(): def test_an_exception_in_objecthook2():
with raises(DecodeError): with raises(DecodeError):
packed = packb({1: [{"__complex__": True, "real": 1, "imag": 2}]}) 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)

View file

@ -14,7 +14,7 @@ from msgpack import packb, unpackb, Unpacker, Packer, pack
def check(data, use_list=False): 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 assert re == data
@ -166,7 +166,7 @@ def testMapSize(sizes=[0, 5, 50, 1000]):
bio.write(packer.pack(i * 2)) # value bio.write(packer.pack(i * 2)) # value
bio.seek(0) bio.seek(0)
unpacker = Unpacker(bio) unpacker = Unpacker(bio, strict_map_key=False)
for size in sizes: for size in sizes:
assert unpacker.unpack() == dict((i, i * 2) for i in range(size)) assert unpacker.unpack() == dict((i, i * 2) for i in range(size))
@ -186,7 +186,7 @@ def test_pairlist():
pairlist = [(b"a", 1), (2, b"b"), (b"foo", b"bar")] pairlist = [(b"a", 1), (2, b"b"), (b"foo", b"bar")]
packer = Packer() packer = Packer()
packed = packer.pack_map_pairs(pairlist) 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 assert pairlist == unpacked

View file

@ -132,7 +132,7 @@ def test_unpack_tell():
pack(m, stream) pack(m, stream)
offsets.append(stream.tell()) offsets.append(stream.tell())
stream.seek(0) stream.seek(0)
unpacker = Unpacker(stream) unpacker = Unpacker(stream, strict_map_key=False)
for m, o in zip(messages, offsets): for m, o in zip(messages, offsets):
m2 = next(unpacker) m2 = next(unpacker)
assert m == m2 assert m == m2