Commit 4215bcb7 authored by Stefan Behnel's avatar Stefan Behnel

streamlined builtin next() function

parent 341f8e7b
...@@ -40,17 +40,30 @@ static CYTHON_INLINE PyObject *__Pyx_PyIter_Next2(PyObject *, PyObject *); /*pro ...@@ -40,17 +40,30 @@ static CYTHON_INLINE PyObject *__Pyx_PyIter_Next2(PyObject *, PyObject *); /*pro
impl = ''' impl = '''
static CYTHON_INLINE PyObject *__Pyx_PyIter_Next2(PyObject* iterator, PyObject* defval) { static CYTHON_INLINE PyObject *__Pyx_PyIter_Next2(PyObject* iterator, PyObject* defval) {
PyObject* next; PyObject* next;
if (unlikely(!PyIter_Check(iterator))) { iternextfunc iternext = Py_TYPE(iterator)->tp_iternext;
#if CYTHON_COMPILING_IN_CPYTHON
if (unlikely(!iternext)) {
#else
if (unlikely(!iternext) || unlikely(!PyIter_Check(iterator))) {
#endif
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"%.200s object is not an iterator", iterator->ob_type->tp_name); "%.200s object is not an iterator", Py_TYPE(iterator)->tp_name);
return NULL; return NULL;
} }
next = (*(Py_TYPE(iterator)->tp_iternext))(iterator); next = iternext(iterator);
if (likely(next)) { if (likely(next))
return next; return next;
#if CYTHON_COMPILING_IN_CPYTHON
#if PY_VERSION_HEX >= 0x03010000 || PY_MAJOR_VERSION < 3 && PY_VERSION_HEX >= 0x02070000
if (unlikely(iternext == &_PyObject_NextNotImplemented)) {
return NULL;
#endif
#endif
} else if (defval) { } else if (defval) {
if (PyErr_Occurred()) { PyObject* exc_type = PyErr_Occurred();
if(!PyErr_ExceptionMatches(PyExc_StopIteration)) if (exc_type) {
if (unlikely(exc_type != PyExc_StopIteration) &&
!PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))
return NULL; return NULL;
PyErr_Clear(); PyErr_Clear();
} }
......
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