Stéphane Wirtel | cbb6484 | 2019-05-17 11:55:34 +0200 | [diff] [blame] | 1 | .. highlight:: c |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 2 | |
| 3 | .. _iterator: |
| 4 | |
| 5 | Iterator Protocol |
| 6 | ================= |
| 7 | |
Raymond Hettinger | 8ee7708 | 2013-10-09 22:42:46 -0700 | [diff] [blame] | 8 | There are two functions specifically for working with iterators. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 9 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 10 | .. c:function:: int PyIter_Check(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 11 | |
Erlend Egeberg Aasland | cc54001 | 2021-02-16 16:05:58 +0100 | [diff] [blame^] | 12 | Return non-zero if the object *o* supports the iterator protocol, and ``0`` |
| 13 | otherwise. This function always succeeds. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 14 | |
| 15 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 16 | .. c:function:: PyObject* PyIter_Next(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 17 | |
Raymond Hettinger | 8ee7708 | 2013-10-09 22:42:46 -0700 | [diff] [blame] | 18 | Return the next value from the iteration *o*. The object must be an iterator |
| 19 | (it is up to the caller to check this). If there are no remaining values, |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 20 | returns ``NULL`` with no exception set. If an error occurs while retrieving |
| 21 | the item, returns ``NULL`` and passes along the exception. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 22 | |
| 23 | To write a loop which iterates over an iterator, the C code should look |
| 24 | something like this:: |
| 25 | |
| 26 | PyObject *iterator = PyObject_GetIter(obj); |
| 27 | PyObject *item; |
| 28 | |
| 29 | if (iterator == NULL) { |
| 30 | /* propagate error */ |
| 31 | } |
| 32 | |
William Ayd | 5c7ed75 | 2019-12-24 23:25:56 -0500 | [diff] [blame] | 33 | while ((item = PyIter_Next(iterator))) { |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 34 | /* do something with item */ |
| 35 | ... |
| 36 | /* release reference when done */ |
| 37 | Py_DECREF(item); |
| 38 | } |
| 39 | |
| 40 | Py_DECREF(iterator); |
| 41 | |
| 42 | if (PyErr_Occurred()) { |
| 43 | /* propagate error */ |
| 44 | } |
| 45 | else { |
| 46 | /* continue doing useful work */ |
| 47 | } |
Vladimir Matveev | 037245c | 2020-10-09 17:15:15 -0700 | [diff] [blame] | 48 | |
| 49 | |
| 50 | .. c:type:: PySendResult |
| 51 | |
| 52 | The enum value used to represent different results of :c:func:`PyIter_Send`. |
| 53 | |
Vladimir Matveev | cfb0f57 | 2020-10-13 10:26:51 -0700 | [diff] [blame] | 54 | .. versionadded:: 3.10 |
| 55 | |
Vladimir Matveev | 037245c | 2020-10-09 17:15:15 -0700 | [diff] [blame] | 56 | |
| 57 | .. c:function:: PySendResult PyIter_Send(PyObject *iter, PyObject *arg, PyObject **presult) |
| 58 | |
| 59 | Sends the *arg* value into the iterator *iter*. Returns: |
| 60 | |
| 61 | - ``PYGEN_RETURN`` if iterator returns. Return value is returned via *presult*. |
| 62 | - ``PYGEN_NEXT`` if iterator yields. Yielded value is returned via *presult*. |
| 63 | - ``PYGEN_ERROR`` if iterator has raised and exception. *presult* is set to ``NULL``. |
Vladimir Matveev | cfb0f57 | 2020-10-13 10:26:51 -0700 | [diff] [blame] | 64 | |
| 65 | .. versionadded:: 3.10 |