Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 314bae5

Browse files
committed
Documentation for PyObject_GetIter(), contributed by Greg Chapman
(with only minor changes by Fred). This closes SF bug #498607.
1 parent e38b7e8 commit 314bae5

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

Doc/api/abstract.tex

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,14 @@ \section{Object Protocol \label{object}}
307307
return false.
308308
\end{cfuncdesc}
309309

310+
\begin{cfuncdesc}{PyObject*}{PyObject_GetIter}{PyObject *o}
311+
This is equivalent to the Python expression \samp{iter(\var{o})}.
312+
It returns a new iterator for the object argument, or the object
313+
itself if the object is already an iterator. Raises
314+
\exception{TypeError} and returns \NULL{} if the object cannot be
315+
iterated.
316+
\end{cfuncdesc}
317+
310318

311319
\section{Number Protocol \label{number}}
312320

@@ -855,17 +863,24 @@ \section{Iterator Protocol \label{iterator}}
855863
look something like this:
856864

857865
\begin{verbatim}
858-
PyObject *iterator = ...;
866+
PyObject *iterator = PyObject_GetIter(obj);
859867
PyObject *item;
860868
861-
while (item = PyIter_Next(iter)) {
869+
if (iterator == NULL) {
870+
/* propagate error */
871+
}
872+
873+
while (item = PyIter_Next(iterator)) {
862874
/* do something with item */
863875
...
864876
/* release reference when done */
865877
Py_DECREF(item);
866878
}
879+
880+
Py_DECREF(iterator);
881+
867882
if (PyErr_Occurred()) {
868-
/* propogate error */
883+
/* propagate error */
869884
}
870885
else {
871886
/* continue doing useful work */

0 commit comments

Comments
 (0)