blob: 63290e0a7f0bf7e6ab4a1548c2e7cdb64ee1640b [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
3.. _iterator:
4
5Iterator Protocol
6=================
7
Raymond Hettinger8ee77082013-10-09 22:42:46 -07008There are two functions specifically for working with iterators.
Georg Brandl54a3faa2008-01-20 09:30:57 +00009
Georg Brandl60203b42010-10-06 10:11:56 +000010.. c:function:: int PyIter_Check(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000011
Erlend Egeberg Aaslandcc540012021-02-16 16:05:58 +010012 Return non-zero if the object *o* supports the iterator protocol, and ``0``
13 otherwise. This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000014
Pablo Galindod9692022021-03-23 23:57:03 +000015.. c:function:: int PyAiter_Check(PyObject *o)
16
17 Returns non-zero if the object 'obj' provides :class:`AsyncIterator`
18 protocols, and ``0`` otherwise. This function always succeeds.
19
20 .. versionadded:: 3.10
Georg Brandl54a3faa2008-01-20 09:30:57 +000021
Georg Brandl60203b42010-10-06 10:11:56 +000022.. c:function:: PyObject* PyIter_Next(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000023
Raymond Hettinger8ee77082013-10-09 22:42:46 -070024 Return the next value from the iteration *o*. The object must be an iterator
25 (it is up to the caller to check this). If there are no remaining values,
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020026 returns ``NULL`` with no exception set. If an error occurs while retrieving
27 the item, returns ``NULL`` and passes along the exception.
Georg Brandl54a3faa2008-01-20 09:30:57 +000028
29To write a loop which iterates over an iterator, the C code should look
30something like this::
31
32 PyObject *iterator = PyObject_GetIter(obj);
33 PyObject *item;
34
35 if (iterator == NULL) {
36 /* propagate error */
37 }
38
William Ayd5c7ed752019-12-24 23:25:56 -050039 while ((item = PyIter_Next(iterator))) {
Georg Brandl54a3faa2008-01-20 09:30:57 +000040 /* do something with item */
41 ...
42 /* release reference when done */
43 Py_DECREF(item);
44 }
45
46 Py_DECREF(iterator);
47
48 if (PyErr_Occurred()) {
49 /* propagate error */
50 }
51 else {
52 /* continue doing useful work */
53 }
Vladimir Matveev037245c2020-10-09 17:15:15 -070054
55
56.. c:type:: PySendResult
57
58 The enum value used to represent different results of :c:func:`PyIter_Send`.
59
Vladimir Matveevcfb0f572020-10-13 10:26:51 -070060 .. versionadded:: 3.10
61
Vladimir Matveev037245c2020-10-09 17:15:15 -070062
63.. c:function:: PySendResult PyIter_Send(PyObject *iter, PyObject *arg, PyObject **presult)
64
65 Sends the *arg* value into the iterator *iter*. Returns:
66
67 - ``PYGEN_RETURN`` if iterator returns. Return value is returned via *presult*.
68 - ``PYGEN_NEXT`` if iterator yields. Yielded value is returned via *presult*.
69 - ``PYGEN_ERROR`` if iterator has raised and exception. *presult* is set to ``NULL``.
Vladimir Matveevcfb0f572020-10-13 10:26:51 -070070
71 .. versionadded:: 3.10