Commit 9c967611 authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #25558: Refactoring OrderedDict iteration.

parent 98da9d0e
......@@ -1829,7 +1829,7 @@ done:
static PyObject *
odictiter_iternext(odictiterobject *di)
{
PyObject *value;
PyObject *result, *value;
PyObject *key = odictiter_nextkey(di); /* new reference */
if (key == NULL)
......@@ -1840,10 +1840,6 @@ odictiter_iternext(odictiterobject *di)
return key;
}
/* Handle the items case. */
if (di->kind & _odict_ITER_KEYS) {
PyObject *result = di->di_result;
value = PyODict_GetItem((PyObject *)di->di_odict, key); /* borrowed */
if (value == NULL) {
if (!PyErr_Occurred())
......@@ -1853,7 +1849,16 @@ odictiter_iternext(odictiterobject *di)
}
Py_INCREF(value);
if (result->ob_refcnt == 1) {
/* Handle the values case. */
if (!(di->kind & _odict_ITER_KEYS)) {
Py_DECREF(key);
return value;
}
/* Handle the items case. */
result = di->di_result;
if (Py_REFCNT(result) == 1) {
/* not in use so we can reuse it
* (the common case during iteration) */
Py_INCREF(result);
......@@ -1871,21 +1876,7 @@ odictiter_iternext(odictiterobject *di)
PyTuple_SET_ITEM(result, 0, key); /* steals reference */
PyTuple_SET_ITEM(result, 1, value); /* steals reference */
return result;
}
/* Handle the values case. */
else {
value = PyODict_GetItem((PyObject *)di->di_odict, key);
Py_DECREF(key);
if (value == NULL) {
if (!PyErr_Occurred())
PyErr_SetObject(PyExc_KeyError, key);
goto done;
}
Py_INCREF(value);
return value;
}
done:
Py_CLEAR(di->di_current);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment