Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 9c96761

Browse files
Issue python#25558: Refactoring OrderedDict iteration.
1 parent 98da9d0 commit 9c96761

File tree

1 file changed

+31
-40
lines changed

1 file changed

+31
-40
lines changed

Objects/odictobject.c

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,7 @@ odictiter_nextkey(odictiterobject *di)
18291829
static PyObject *
18301830
odictiter_iternext(odictiterobject *di)
18311831
{
1832-
PyObject *value;
1832+
PyObject *result, *value;
18331833
PyObject *key = odictiter_nextkey(di); /* new reference */
18341834

18351835
if (key == NULL)
@@ -1840,53 +1840,44 @@ odictiter_iternext(odictiterobject *di)
18401840
return key;
18411841
}
18421842

1843-
/* Handle the items case. */
1844-
if (di->kind & _odict_ITER_KEYS) {
1845-
PyObject *result = di->di_result;
1846-
1847-
value = PyODict_GetItem((PyObject *)di->di_odict, key); /* borrowed */
1848-
if (value == NULL) {
1849-
if (!PyErr_Occurred())
1850-
PyErr_SetObject(PyExc_KeyError, key);
1851-
Py_DECREF(key);
1852-
goto done;
1853-
}
1854-
Py_INCREF(value);
1855-
1856-
if (result->ob_refcnt == 1) {
1857-
/* not in use so we can reuse it
1858-
* (the common case during iteration) */
1859-
Py_INCREF(result);
1860-
Py_DECREF(PyTuple_GET_ITEM(result, 0)); /* borrowed */
1861-
Py_DECREF(PyTuple_GET_ITEM(result, 1)); /* borrowed */
1862-
}
1863-
else {
1864-
result = PyTuple_New(2);
1865-
if (result == NULL) {
1866-
Py_DECREF(key);
1867-
Py_DECREF(value);
1868-
goto done;
1869-
}
1870-
}
1843+
value = PyODict_GetItem((PyObject *)di->di_odict, key); /* borrowed */
1844+
if (value == NULL) {
1845+
if (!PyErr_Occurred())
1846+
PyErr_SetObject(PyExc_KeyError, key);
1847+
Py_DECREF(key);
1848+
goto done;
1849+
}
1850+
Py_INCREF(value);
18711851

1872-
PyTuple_SET_ITEM(result, 0, key); /* steals reference */
1873-
PyTuple_SET_ITEM(result, 1, value); /* steals reference */
1852+
/* Handle the values case. */
1853+
if (!(di->kind & _odict_ITER_KEYS)) {
1854+
Py_DECREF(key);
1855+
return value;
1856+
}
18741857

1875-
return result;
1858+
/* Handle the items case. */
1859+
result = di->di_result;
1860+
1861+
if (Py_REFCNT(result) == 1) {
1862+
/* not in use so we can reuse it
1863+
* (the common case during iteration) */
1864+
Py_INCREF(result);
1865+
Py_DECREF(PyTuple_GET_ITEM(result, 0)); /* borrowed */
1866+
Py_DECREF(PyTuple_GET_ITEM(result, 1)); /* borrowed */
18761867
}
1877-
/* Handle the values case. */
18781868
else {
1879-
value = PyODict_GetItem((PyObject *)di->di_odict, key);
1880-
Py_DECREF(key);
1881-
if (value == NULL) {
1882-
if (!PyErr_Occurred())
1883-
PyErr_SetObject(PyExc_KeyError, key);
1869+
result = PyTuple_New(2);
1870+
if (result == NULL) {
1871+
Py_DECREF(key);
1872+
Py_DECREF(value);
18841873
goto done;
18851874
}
1886-
Py_INCREF(value);
1887-
return value;
18881875
}
18891876

1877+
PyTuple_SET_ITEM(result, 0, key); /* steals reference */
1878+
PyTuple_SET_ITEM(result, 1, value); /* steals reference */
1879+
return result;
1880+
18901881
done:
18911882
Py_CLEAR(di->di_current);
18921883
Py_CLEAR(di->di_odict);

0 commit comments

Comments
 (0)