iterator.pxd 1.3 KB

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