8
There are only a couple of functions specifically for working with iterators.
10
.. cfunction:: int PyIter_Check(PyObject *o)
12
Return true if the object *o* supports the iterator protocol.
15
.. cfunction:: PyObject* PyIter_Next(PyObject *o)
17
Return the next value from the iteration *o*. If the object is an iterator,
18
this retrieves the next value from the iteration, and returns *NULL* with no
19
exception set if there are no remaining items. If the object is not an
20
iterator, :exc:`TypeError` is raised, or if there is an error in retrieving the
21
item, returns *NULL* and passes along the exception.
23
To write a loop which iterates over an iterator, the C code should look
26
PyObject *iterator = PyObject_GetIter(obj);
29
if (iterator == NULL) {
33
while (item = PyIter_Next(iterator)) {
34
/* do something with item */
36
/* release reference when done */
42
if (PyErr_Occurred()) {
46
/* continue doing useful work */