diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-11-06 08:40:05 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-11-06 08:40:05 (GMT) |
commit | 6980be40bd7522261be45431e23dbde4c8ec4d70 (patch) | |
tree | da4410414057de400789d2a581b1c6aaa1d326e9 /Objects | |
parent | e2e0deec45f9dad96aff67cbce0830674feeca0c (diff) | |
parent | 9c967611e3ef92f7aa605356c88027caa9dd9566 (diff) | |
download | cpython-6980be40bd7522261be45431e23dbde4c8ec4d70.zip cpython-6980be40bd7522261be45431e23dbde4c8ec4d70.tar.gz cpython-6980be40bd7522261be45431e23dbde4c8ec4d70.tar.bz2 |
Issue #25558: Refactoring OrderedDict iteration.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/odictobject.c | 71 |
1 files changed, 31 insertions, 40 deletions
diff --git a/Objects/odictobject.c b/Objects/odictobject.c index 28537a8..7a3ded0 100644 --- a/Objects/odictobject.c +++ b/Objects/odictobject.c @@ -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,53 +1840,44 @@ 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()) - PyErr_SetObject(PyExc_KeyError, key); - Py_DECREF(key); - 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; - } - } + 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); - PyTuple_SET_ITEM(result, 0, key); /* steals reference */ - PyTuple_SET_ITEM(result, 1, value); /* steals reference */ + /* Handle the values case. */ + if (!(di->kind & _odict_ITER_KEYS)) { + Py_DECREF(key); + return value; + } - return result; + /* 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); + Py_DECREF(PyTuple_GET_ITEM(result, 0)); /* borrowed */ + Py_DECREF(PyTuple_GET_ITEM(result, 1)); /* borrowed */ } - /* 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); + result = PyTuple_New(2); + if (result == NULL) { + Py_DECREF(key); + Py_DECREF(value); 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); Py_CLEAR(di->di_odict); |