[Python-checkins] cpython (merge 3.5 -> default): Issue #25558: Refactoring OrderedDict iteration.
serhiy.storchaka
python-checkins at python.org
Fri Nov 6 04:21:53 EST 2015
https://hg.python.org/cpython/rev/51f3547da99c
changeset: 98982:51f3547da99c
parent: 98980:d86ff708f545
parent: 98981:ad44d551c13c
user: Serhiy Storchaka <storchaka at gmail.com>
date: Fri Nov 06 10:40:05 2015 +0200
summary:
Issue #25558: Refactoring OrderedDict iteration.
files:
Objects/odictobject.c | 73 +++++++++++++-----------------
1 files changed, 32 insertions(+), 41 deletions(-)
diff --git a/Objects/odictobject.c b/Objects/odictobject.c
--- a/Objects/odictobject.c
+++ b/Objects/odictobject.c
@@ -1829,7 +1829,7 @@
static PyObject *
odictiter_iternext(odictiterobject *di)
{
- PyObject *value;
+ PyObject *result, *value;
PyObject *key = odictiter_nextkey(di); /* new reference */
if (key == NULL)
@@ -1840,52 +1840,43 @@
return key;
}
+ value = PyODict_GetItem((PyObject *)di->di_odict, key); /* borrowed */
+ if (value == NULL) {
+ if (!PyErr_Occurred())
+ PyErr_SetObject(PyExc_KeyError, key);
+ Py_DECREF(key);
+ goto done;
+ }
+ Py_INCREF(value);
+
+ /* Handle the values case. */
+ if (!(di->kind & _odict_ITER_KEYS)) {
+ Py_DECREF(key);
+ return value;
+ }
+
/* Handle the items case. */
- if (di->kind & _odict_ITER_KEYS) {
- PyObject *result = di->di_result;
+ result = di->di_result;
- value = PyODict_GetItem((PyObject *)di->di_odict, key); /* borrowed */
- if (value == NULL) {
- if (!PyErr_Occurred())
- PyErr_SetObject(PyExc_KeyError, key);
+ if (Py_REFCNT(result) == 1) {
+ /* not in use so we can reuse it
+ * (the common case during iteration) */
+ Py_INCREF(result);
+ Py_DECREF(PyTuple_GET_ITEM(result, 0)); /* borrowed */
+ Py_DECREF(PyTuple_GET_ITEM(result, 1)); /* borrowed */
+ }
+ else {
+ result = PyTuple_New(2);
+ if (result == NULL) {
Py_DECREF(key);
+ Py_DECREF(value);
goto done;
}
- Py_INCREF(value);
+ }
- if (result->ob_refcnt == 1) {
- /* not in use so we can reuse it
- * (the common case during iteration) */
- Py_INCREF(result);
- Py_DECREF(PyTuple_GET_ITEM(result, 0)); /* borrowed */
- Py_DECREF(PyTuple_GET_ITEM(result, 1)); /* borrowed */
- }
- else {
- result = PyTuple_New(2);
- if (result == NULL) {
- Py_DECREF(key);
- Py_DECREF(value);
- goto done;
- }
- }
-
- 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;
- }
+ PyTuple_SET_ITEM(result, 0, key); /* steals reference */
+ PyTuple_SET_ITEM(result, 1, value); /* steals reference */
+ return result;
done:
Py_CLEAR(di->di_current);
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list