1 cdef extern from "Python.h":
3 ############################################################################
4 # 6.5 Iterator Protocol
5 ############################################################################
6 bint PyIter_Check(object o)
7 # Return true if the object o supports the iterator protocol.
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.
18 # To write a loop which iterates over an iterator, the C code should look something like this:
19 # PyObject *iterator = PyObject_GetIter(obj);
21 # if (iterator == NULL) {
22 # /* propagate error */
24 # while (item = PyIter_Next(iterator)) {
25 # /* do something with item */
27 # /* release reference when done */
30 # Py_DECREF(iterator);
31 # if (PyErr_Occurred()) {
32 # /* propagate error */
35 # /* continue doing useful work */