Last change
on this file was 391, checked in by dmik, 11 years ago |
python: Merge vendor 2.7.6 to trunk.
|
-
Property svn:eol-style
set to
native
|
File size:
1.1 KB
|
Rev | Line | |
---|
[2] | 1 | .. highlightlang:: c
|
---|
| 2 |
|
---|
| 3 | .. _iterator:
|
---|
| 4 |
|
---|
| 5 | Iterator Protocol
|
---|
| 6 | =================
|
---|
| 7 |
|
---|
| 8 | .. versionadded:: 2.2
|
---|
| 9 |
|
---|
[391] | 10 | There are two functions specifically for working with iterators.
|
---|
[2] | 11 |
|
---|
| 12 |
|
---|
[391] | 13 | .. c:function:: int PyIter_Check(PyObject *o)
|
---|
[2] | 14 |
|
---|
| 15 | Return true if the object *o* supports the iterator protocol.
|
---|
| 16 |
|
---|
| 17 |
|
---|
[391] | 18 | .. c:function:: PyObject* PyIter_Next(PyObject *o)
|
---|
[2] | 19 |
|
---|
[391] | 20 | Return the next value from the iteration *o*. The object must be an iterator
|
---|
| 21 | (it is up to the caller to check this). If there are no remaining values,
|
---|
| 22 | returns *NULL* with no exception set. If an error occurs while retrieving
|
---|
| 23 | the item, returns *NULL* and passes along the exception.
|
---|
[2] | 24 |
|
---|
| 25 | To write a loop which iterates over an iterator, the C code should look
|
---|
| 26 | something like this::
|
---|
| 27 |
|
---|
| 28 | PyObject *iterator = PyObject_GetIter(obj);
|
---|
| 29 | PyObject *item;
|
---|
| 30 |
|
---|
| 31 | if (iterator == NULL) {
|
---|
| 32 | /* propagate error */
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | while (item = PyIter_Next(iterator)) {
|
---|
| 36 | /* do something with item */
|
---|
| 37 | ...
|
---|
| 38 | /* release reference when done */
|
---|
| 39 | Py_DECREF(item);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | Py_DECREF(iterator);
|
---|
| 43 |
|
---|
| 44 | if (PyErr_Occurred()) {
|
---|
| 45 | /* propagate error */
|
---|
| 46 | }
|
---|
| 47 | else {
|
---|
| 48 | /* continue doing useful work */
|
---|
| 49 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.