Skip to content

Commit dda4468

Browse files
committed
Complete the merge for issue #29203
1 parent ac7c5ac commit dda4468

1 file changed

Lines changed: 18 additions & 31 deletions

File tree

‎Modules/_functoolsmodule.c‎

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -704,73 +704,60 @@ static PyTypeObject lru_cache_type;
704704
static PyObject *
705705
lru_cache_make_key(PyObject *args, PyObject *kwds, int typed)
706706
{
707-
PyObject *key, *sorted_items;
708-
Py_ssize_t key_size, pos, key_pos;
707+
PyObject *key, *keyword, *value;
708+
Py_ssize_t key_size, pos, key_pos, kwds_size;
709709

710710
/* short path, key will match args anyway, which is a tuple */
711711
if (!typed && !kwds) {
712712
Py_INCREF(args);
713713
return args;
714714
}
715715

716-
if (kwds && PyDict_GET_SIZE(kwds) > 0) {
717-
sorted_items = PyDict_Items(kwds);
718-
if (!sorted_items)
719-
return NULL;
720-
if (PyList_Sort(sorted_items) < 0) {
721-
Py_DECREF(sorted_items);
722-
return NULL;
723-
}
724-
} else
725-
sorted_items = NULL;
716+
kwds_size = kwds ? PyDict_GET_SIZE(kwds) : 0;
717+
assert(kwds_size >= 0);
726718

727719
key_size = PyTuple_GET_SIZE(args);
728-
if (sorted_items)
729-
key_size += PyList_GET_SIZE(sorted_items);
720+
if (kwds_size)
721+
key_size += kwds_size * 2 + 1;
730722
if (typed)
731-
key_size *= 2;
732-
if (sorted_items)
733-
key_size++;
723+
key_size += PyTuple_GET_SIZE(args) + kwds_size;
734724

735725
key = PyTuple_New(key_size);
736726
if (key == NULL)
737-
goto done;
727+
return NULL;
738728

739729
key_pos = 0;
740730
for (pos = 0; pos < PyTuple_GET_SIZE(args); ++pos) {
741731
PyObject *item = PyTuple_GET_ITEM(args, pos);
742732
Py_INCREF(item);
743733
PyTuple_SET_ITEM(key, key_pos++, item);
744734
}
745-
if (sorted_items) {
735+
if (kwds_size) {
746736
Py_INCREF(kwd_mark);
747737
PyTuple_SET_ITEM(key, key_pos++, kwd_mark);
748-
for (pos = 0; pos < PyList_GET_SIZE(sorted_items); ++pos) {
749-
PyObject *item = PyList_GET_ITEM(sorted_items, pos);
750-
Py_INCREF(item);
751-
PyTuple_SET_ITEM(key, key_pos++, item);
738+
for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) {
739+
Py_INCREF(keyword);
740+
PyTuple_SET_ITEM(key, key_pos++, keyword);
741+
Py_INCREF(value);
742+
PyTuple_SET_ITEM(key, key_pos++, value);
752743
}
744+
assert(key_pos == PyTuple_GET_SIZE(args) + kwds_size * 2 + 1);
753745
}
754746
if (typed) {
755747
for (pos = 0; pos < PyTuple_GET_SIZE(args); ++pos) {
756748
PyObject *item = (PyObject *)Py_TYPE(PyTuple_GET_ITEM(args, pos));
757749
Py_INCREF(item);
758750
PyTuple_SET_ITEM(key, key_pos++, item);
759751
}
760-
if (sorted_items) {
761-
for (pos = 0; pos < PyList_GET_SIZE(sorted_items); ++pos) {
762-
PyObject *tp_items = PyList_GET_ITEM(sorted_items, pos);
763-
PyObject *item = (PyObject *)Py_TYPE(PyTuple_GET_ITEM(tp_items, 1));
752+
if (kwds_size) {
753+
for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) {
754+
PyObject *item = (PyObject *)Py_TYPE(value);
764755
Py_INCREF(item);
765756
PyTuple_SET_ITEM(key, key_pos++, item);
766757
}
767758
}
768759
}
769760
assert(key_pos == key_size);
770-
771-
done:
772-
if (sorted_items)
773-
Py_DECREF(sorted_items);
774761
return key;
775762
}
776763

0 commit comments

Comments
 (0)