Skip to content

Commit d8e3cf0

Browse files
authored
Make strict_map_key default to True (#392)
1 parent 0fc0eb2 commit d8e3cf0

8 files changed

+19
-20
lines changed

msgpack/_unpacker.pyx

+3-5
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ cdef inline int get_data_from_buffer(object obj,
131131

132132

133133
def unpackb(object packed, *, object object_hook=None, object list_hook=None,
134-
bint use_list=True, bint raw=False, bint strict_map_key=False,
134+
bint use_list=True, bint raw=False, bint strict_map_key=True,
135135
unicode_errors=None,
136136
object_pairs_hook=None, ext_hook=ExtType,
137137
Py_ssize_t max_str_len=-1,
@@ -221,9 +221,7 @@ cdef class Unpacker(object):
221221
Otherwise, unpack to Python str by decoding with UTF-8 encoding (default).
222222
223223
:param bool strict_map_key:
224-
If true, only str or bytes are accepted for map (dict) keys.
225-
It's False by default for backward-compatibility.
226-
But it will be True from msgpack 1.0.
224+
If true (default), only str or bytes are accepted for map (dict) keys.
227225
228226
:param callable object_hook:
229227
When specified, it should be callable.
@@ -305,7 +303,7 @@ cdef class Unpacker(object):
305303
self.buf = NULL
306304

307305
def __init__(self, file_like=None, *, Py_ssize_t read_size=0,
308-
bint use_list=True, bint raw=False, bint strict_map_key=False,
306+
bint use_list=True, bint raw=False, bint strict_map_key=True,
309307
object object_hook=None, object object_pairs_hook=None, object list_hook=None,
310308
unicode_errors=None, Py_ssize_t max_buffer_size=0,
311309
object ext_hook=ExtType,

msgpack/fallback.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,7 @@ class Unpacker(object):
175175
Otherwise, unpack to Python str by decoding with UTF-8 encoding (default).
176176
177177
:param bool strict_map_key:
178-
If true, only str or bytes are accepted for map (dict) keys.
179-
It's False by default for backward-compatibility.
180-
But it will be True from msgpack 1.0.
178+
If true (default), only str or bytes are accepted for map (dict) keys.
181179
182180
:param callable object_hook:
183181
When specified, it should be callable.
@@ -249,7 +247,7 @@ def __init__(
249247
read_size=0,
250248
use_list=True,
251249
raw=False,
252-
strict_map_key=False,
250+
strict_map_key=True,
253251
object_hook=None,
254252
object_pairs_hook=None,
255253
list_hook=None,

test/test_case.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def test_array32():
9292

9393
def match(obj, buf):
9494
assert packb(obj) == buf
95-
assert unpackb(buf, use_list=0) == obj
95+
assert unpackb(buf, use_list=0, strict_map_key=False) == obj
9696

9797

9898
def test_match():

test/test_format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
def check(src, should, use_list=0, raw=True):
8-
assert unpackb(src, use_list=use_list, raw=raw) == should
8+
assert unpackb(src, use_list=use_list, raw=raw, strict_map_key=False) == should
99

1010

1111
def testSimpleValue():

test/test_limits.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ def test_max_map_len():
8787
d = {1: 2, 3: 4, 5: 6}
8888
packed = packb(d)
8989

90-
unpacker = Unpacker(max_map_len=3)
90+
unpacker = Unpacker(max_map_len=3, strict_map_key=False)
9191
unpacker.feed(packed)
9292
assert unpacker.unpack() == d
9393

94-
unpacker = Unpacker(max_map_len=2)
94+
unpacker = Unpacker(max_map_len=2, strict_map_key=False)
9595
with pytest.raises(UnpackValueError):
9696
unpacker.feed(packed)
9797
unpacker.unpack()

test/test_obj.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ def test_decode_pairs_hook():
3333
packed = packb([3, {1: 2, 3: 4}])
3434
prod_sum = 1 * 2 + 3 * 4
3535
unpacked = unpackb(
36-
packed, object_pairs_hook=lambda l: sum(k * v for k, v in l), use_list=1
36+
packed,
37+
object_pairs_hook=lambda l: sum(k * v for k, v in l),
38+
use_list=1,
39+
strict_map_key=False,
3740
)
3841
assert unpacked[1] == prod_sum
3942

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

7578

7679
def test_an_exception_in_objecthook2():
7780
with raises(DecodeError):
7881
packed = packb({1: [{"__complex__": True, "real": 1, "imag": 2}]})
79-
unpackb(packed, list_hook=bad_complex_decoder, use_list=1)
82+
unpackb(packed, list_hook=bad_complex_decoder, use_list=1, strict_map_key=False)

test/test_pack.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
def check(data, use_list=False):
17-
re = unpackb(packb(data), use_list=use_list)
17+
re = unpackb(packb(data), use_list=use_list, strict_map_key=False)
1818
assert re == data
1919

2020

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

168168
bio.seek(0)
169-
unpacker = Unpacker(bio)
169+
unpacker = Unpacker(bio, strict_map_key=False)
170170
for size in sizes:
171171
assert unpacker.unpack() == dict((i, i * 2) for i in range(size))
172172

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

192192

test/test_sequnpack.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def test_unpack_tell():
132132
pack(m, stream)
133133
offsets.append(stream.tell())
134134
stream.seek(0)
135-
unpacker = Unpacker(stream)
135+
unpacker = Unpacker(stream, strict_map_key=False)
136136
for m, o in zip(messages, offsets):
137137
m2 = next(unpacker)
138138
assert m == m2

0 commit comments

Comments
 (0)