1.. highlightlang:: c 2 3.. _iterator: 4 5Iterator Protocol 6================= 7 8.. versionadded:: 2.2 9 10There are two functions specifically for working with iterators. 11 12 13.. c:function:: int PyIter_Check(PyObject *o) 14 15 Return true if the object *o* supports the iterator protocol. 16 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`. 21 22.. c:function:: PyObject* PyIter_Next(PyObject *o) 23 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. 28 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 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 } 54