Skip to content

Commit 419fe6c

Browse files
add logic to convert dict list key to tuple
1 parent 0eeabfb commit 419fe6c

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

msgpack/unpack.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,25 @@ static inline int unpack_callback_map_item(unpack_user* u, unsigned int current,
199199
if (PyUnicode_CheckExact(k)) {
200200
PyUnicode_InternInPlace(&k);
201201
}
202+
if (PyList_CheckExact(k)) {
203+
Py_ssize_t list_size = PyList_Size(k);
204+
PyObject* tuple = PyTuple_New(list_size);
205+
206+
if (tuple == NULL) {
207+
return -1;
208+
}
209+
210+
for (Py_ssize_t i = 0; i < list_size; i++) {
211+
PyObject* item = PyList_GetItem(k, i);
212+
Py_INCREF(item);
213+
if (PyTuple_SetItem(tuple, i, item) != 0) {
214+
Py_DECREF(tuple);
215+
return -1;
216+
}
217+
}
218+
Py_DECREF(k);
219+
k = tuple;
220+
}
202221
if (u->has_pairs_hook) {
203222
msgpack_unpack_object item = PyTuple_Pack(2, k, v);
204223
if (!item)

test/test_case.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,6 @@ def test_match():
134134

135135
def test_unicode():
136136
assert unpackb(packb("foobar"), use_list=1) == "foobar"
137+
138+
def test_dict_tuple_key():
139+
unpackb(packb({(1, 2): 3}), strict_map_key=False)

0 commit comments

Comments
 (0)