1cdef extern from "Python.h": 2 3 ############################################################################ 4 # 6.5 Iterator Protocol 5 ############################################################################ 6 bint PyIter_Check(object o) 7 # Return true if the object o supports the iterator protocol. 8 9 object PyIter_Next(object o) 10 # Return value: New reference. 11 # Return the next value from the iteration o. If the object is an 12 # iterator, this retrieves the next value from the iteration, and 13 # returns NULL with no exception set if there are no remaining 14 # items. If the object is not an iterator, TypeError is raised, or 15 # if there is an error in retrieving the item, returns NULL and 16 # passes along the exception. 17 18 # To write a loop which iterates over an iterator, the C code should look something like this: 19 # PyObject *iterator = PyObject_GetIter(obj); 20 # PyObject *item; 21 # if (iterator == NULL) { 22 # /* propagate error */ 23 # } 24 # while (item = PyIter_Next(iterator)) { 25 # /* do something with item */ 26 # ... 27 # /* release reference when done */ 28 # Py_DECREF(item); 29 # } 30 # Py_DECREF(iterator); 31 # if (PyErr_Occurred()) { 32 # /* propagate error */ 33 # } 34 # else { 35 # /* continue doing useful work */ 36 # } 37