mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-06 09:50:01 +00:00
add logic to convert dict list key to tuple
This commit is contained in:
parent
0eeabfb453
commit
419fe6cf42
2 changed files with 22 additions and 0 deletions
|
|
@ -199,6 +199,25 @@ static inline int unpack_callback_map_item(unpack_user* u, unsigned int current,
|
|||
if (PyUnicode_CheckExact(k)) {
|
||||
PyUnicode_InternInPlace(&k);
|
||||
}
|
||||
if (PyList_CheckExact(k)) {
|
||||
Py_ssize_t list_size = PyList_Size(k);
|
||||
PyObject* tuple = PyTuple_New(list_size);
|
||||
|
||||
if (tuple == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (Py_ssize_t i = 0; i < list_size; i++) {
|
||||
PyObject* item = PyList_GetItem(k, i);
|
||||
Py_INCREF(item);
|
||||
if (PyTuple_SetItem(tuple, i, item) != 0) {
|
||||
Py_DECREF(tuple);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
Py_DECREF(k);
|
||||
k = tuple;
|
||||
}
|
||||
if (u->has_pairs_hook) {
|
||||
msgpack_unpack_object item = PyTuple_Pack(2, k, v);
|
||||
if (!item)
|
||||
|
|
|
|||
|
|
@ -134,3 +134,6 @@ def test_match():
|
|||
|
||||
def test_unicode():
|
||||
assert unpackb(packb("foobar"), use_list=1) == "foobar"
|
||||
|
||||
def test_dict_tuple_key():
|
||||
unpackb(packb({(1, 2): 3}), strict_map_key=False)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue