diff options
author | Raymond Hettinger <python@rcn.com> | 2015-09-26 08:30:51 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2015-09-26 08:30:51 (GMT) |
commit | 7a84552c84867b548a72b20c177542e892c528f6 (patch) | |
tree | ef490675dd545d10f5f5b5ae5398e3bf7598556e /Modules | |
parent | 98de5340d4d782b2a0595c9fa729460a2c1b4b91 (diff) | |
download | cpython-7a84552c84867b548a72b20c177542e892c528f6.zip cpython-7a84552c84867b548a72b20c177542e892c528f6.tar.gz cpython-7a84552c84867b548a72b20c177542e892c528f6.tar.bz2 |
Hoist constant expression out of an inner loop.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_collectionsmodule.c | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index d50bc42..2dcdf8b 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -371,6 +371,7 @@ static PyObject * deque_extend(dequeobject *deque, PyObject *iterable) { PyObject *it, *item; + PyObject *(*iternext)(PyObject *); int trim = (deque->maxlen != -1); /* Handle case where id(deque) == id(iterable) */ @@ -399,7 +400,8 @@ deque_extend(dequeobject *deque, PyObject *iterable) if (deque->maxlen == 0) return consume_iterator(it); - while ((item = PyIter_Next(it)) != NULL) { + iternext = *Py_TYPE(it)->tp_iternext; + while ((item = iternext(it)) != NULL) { deque->state++; if (deque->rightindex == BLOCKLEN - 1) { block *b = newblock(Py_SIZE(deque)); @@ -422,8 +424,12 @@ deque_extend(dequeobject *deque, PyObject *iterable) deque_trim_left(deque); } if (PyErr_Occurred()) { - Py_DECREF(it); - return NULL; + if (PyErr_ExceptionMatches(PyExc_StopIteration)) + PyErr_Clear(); + else { + Py_DECREF(it); + return NULL; + } } Py_DECREF(it); Py_RETURN_NONE; @@ -436,6 +442,7 @@ static PyObject * deque_extendleft(dequeobject *deque, PyObject *iterable) { PyObject *it, *item; + PyObject *(*iternext)(PyObject *); int trim = (deque->maxlen != -1); /* Handle case where id(deque) == id(iterable) */ @@ -464,7 +471,8 @@ deque_extendleft(dequeobject *deque, PyObject *iterable) if (deque->maxlen == 0) return consume_iterator(it); - while ((item = PyIter_Next(it)) != NULL) { + iternext = *Py_TYPE(it)->tp_iternext; + while ((item = iternext(it)) != NULL) { deque->state++; if (deque->leftindex == 0) { block *b = newblock(Py_SIZE(deque)); @@ -487,8 +495,12 @@ deque_extendleft(dequeobject *deque, PyObject *iterable) deque_trim_right(deque); } if (PyErr_Occurred()) { - Py_DECREF(it); - return NULL; + if (PyErr_ExceptionMatches(PyExc_StopIteration)) + PyErr_Clear(); + else { + Py_DECREF(it); + return NULL; + } } Py_DECREF(it); Py_RETURN_NONE; |