diff options
author | Fred Drake <fdrake@acm.org> | 2002-03-11 18:46:29 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2002-03-11 18:46:29 (GMT) |
commit | 314bae50b967393d1e64f5b42e3cfb2027ff9b32 (patch) | |
tree | 9534198b90fea32d0833d38cf9e17d709ff47e3f /Doc | |
parent | e38b7e8fe9591736cacc7190a01cebb3e1bf9b68 (diff) | |
download | cpython-314bae50b967393d1e64f5b42e3cfb2027ff9b32.zip cpython-314bae50b967393d1e64f5b42e3cfb2027ff9b32.tar.gz cpython-314bae50b967393d1e64f5b42e3cfb2027ff9b32.tar.bz2 |
Documentation for PyObject_GetIter(), contributed by Greg Chapman
(with only minor changes by Fred).
This closes SF bug #498607.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/api/abstract.tex | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/Doc/api/abstract.tex b/Doc/api/abstract.tex index af49611..4d92268 100644 --- a/Doc/api/abstract.tex +++ b/Doc/api/abstract.tex @@ -307,6 +307,14 @@ determination. return false. \end{cfuncdesc} +\begin{cfuncdesc}{PyObject*}{PyObject_GetIter}{PyObject *o} + This is equivalent to the Python expression \samp{iter(\var{o})}. + It returns a new iterator for the object argument, or the object + itself if the object is already an iterator. Raises + \exception{TypeError} and returns \NULL{} if the object cannot be + iterated. +\end{cfuncdesc} + \section{Number Protocol \label{number}} @@ -855,17 +863,24 @@ To write a loop which iterates over an iterator, the C code should look something like this: \begin{verbatim} -PyObject *iterator = ...; +PyObject *iterator = PyObject_GetIter(obj); PyObject *item; -while (item = PyIter_Next(iter)) { +if (iterator == NULL) { + /* propagate error */ +} + +while (item = PyIter_Next(iterator)) { /* do something with item */ ... /* release reference when done */ Py_DECREF(item); } + +Py_DECREF(iterator); + if (PyErr_Occurred()) { - /* propogate error */ + /* propagate error */ } else { /* continue doing useful work */ |