Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _iterator: |
| 4 | |
| 5 | Iterator Protocol |
| 6 | ================= |
| 7 | |
| 8 | .. versionadded:: 2.2 |
| 9 | |
Raymond Hettinger | 8b1701d | 2013-10-09 22:39:11 -0700 | [diff] [blame] | 10 | There are two functions specifically for working with iterators. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 11 | |
| 12 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 13 | .. c:function:: int PyIter_Check(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 14 | |
| 15 | Return true if the object *o* supports the iterator protocol. |
| 16 | |
Raymond Hettinger | 632d692 | 2015-05-11 10:22:20 -0700 | [diff] [blame] | 17 | This function can return a false positive in the case of old-style |
| 18 | classes because those classes always define a :c:member:`tp_iternext` |
| 19 | slot with logic that either invokes a :meth:`next` method or raises |
| 20 | a :exc:`TypeError`. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 21 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 22 | .. c:function:: PyObject* PyIter_Next(PyObject *o) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 23 | |
Raymond Hettinger | 8b1701d | 2013-10-09 22:39:11 -0700 | [diff] [blame] | 24 | 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, |
| 26 | returns *NULL* with no exception set. If an error occurs while retrieving |
| 27 | the item, returns *NULL* and passes along the exception. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 28 | |
| 29 | To write a loop which iterates over an iterator, the C code should look |
| 30 | something like this:: |
| 31 | |
| 32 | PyObject *iterator = PyObject_GetIter(obj); |
| 33 | PyObject *item; |
| 34 | |
| 35 | if (iterator == NULL) { |
| 36 | /* propagate error */ |
| 37 | } |
| 38 | |
| 39 | while (item = PyIter_Next(iterator)) { |
| 40 | /* 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 | } |