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

Skip to content

Commit dbcaeda

Browse files
committed
Added documentation for PyIter_Check() and PyIter_Next().
Wrapped a long line.
1 parent 8572b4f commit dbcaeda

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

Doc/api/api.tex

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2082,13 +2082,51 @@ \section{Mapping Protocol \label{mapping}}
20822082
\samp{\var{o}[\var{key}]}.
20832083
\end{cfuncdesc}
20842084

2085-
\begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key, PyObject *v}
2085+
\begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key,
2086+
PyObject *v}
20862087
Map the object \var{key} to the value \var{v} in object \var{o}.
20872088
Returns \code{-1} on failure. This is the equivalent of the Python
20882089
statement \samp{\var{o}[\var{key}] = \var{v}}.
20892090
\end{cfuncdesc}
20902091

20912092

2093+
\section{Iterator Protocol \label{iterator}}
2094+
2095+
There are only a couple of functions specifically for working with
2096+
iterators.
2097+
2098+
\begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o}
2099+
Return true if the object \var{o} supports the iterator protocol.
2100+
\end{cfuncdesc}
2101+
2102+
\begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o}
2103+
Return the next value from the iteration \var{o}. If the object is
2104+
an iterator, this retrieves the next value from the iteration, and
2105+
returns \NULL{} with no exception set if there are no remaining
2106+
items. If the object is not an iterator, \exception{TypeError} is
2107+
raised, or if there is an error in retrieving the item, returns
2108+
\NULL{} and passes along the exception.
2109+
\end{cfuncdesc}
2110+
2111+
To write a loop which iterates over an iterator, the C code should
2112+
look something like this:
2113+
2114+
\begin{verbatim}
2115+
PyObject *iterator = ...;
2116+
PyObject *item;
2117+
2118+
while (item = PyIter_Next(iter)) {
2119+
/* do something with item */
2120+
}
2121+
if (PyErr_Occurred()) {
2122+
/* propogate error */
2123+
}
2124+
else {
2125+
/* continue doing useful work */
2126+
}
2127+
\end{verbatim}
2128+
2129+
20922130
\chapter{Concrete Objects Layer \label{concrete}}
20932131

20942132
The functions in this chapter are specific to certain Python object

0 commit comments

Comments
 (0)