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

Skip to content

Commit 8d00a0f

Browse files
committed
Michael Hudson:
Update docs for PyDict_Next() based on the most recent changes to the dictionary code. This closes SF patch #409864.
1 parent 058dae3 commit 8d00a0f

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

Doc/api/api.tex

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3397,7 +3397,8 @@ \subsection{Dictionary Objects \label{dictObjects}}
33973397
all pairs have been reported. The parameters \var{pkey} and
33983398
\var{pvalue} should either point to \ctype{PyObject*} variables that
33993399
will be filled in with each key and value, respectively, or may be
3400-
\NULL. The dictionary \var{p} must not be mutated during iteration.
3400+
\NULL.
3401+
34013402
For example:
34023403

34033404
\begin{verbatim}
@@ -3409,6 +3410,27 @@ \subsection{Dictionary Objects \label{dictObjects}}
34093410
...
34103411
}
34113412
\end{verbatim}
3413+
3414+
The dictionary \var{p} should not be mutated during iteration. It is
3415+
safe (since Python 2.1) to modify the values of the keys as you
3416+
iterate over the dictionary, for example:
3417+
3418+
\begin{verbatim}
3419+
PyObject *key, *value;
3420+
int pos = 0;
3421+
3422+
while (PyDict_Next(self->dict, &pos, &key, &value)) {
3423+
int i = PyInt_AS_LONG(value) + 1;
3424+
PyObject *o = PyInt_FromLong(i);
3425+
if (o == NULL)
3426+
return -1;
3427+
if (PyDict_SetItem(self->dict, key, o) < 0) {
3428+
Py_DECREF(o);
3429+
return -1;
3430+
}
3431+
Py_DECREF(o);
3432+
}
3433+
\end{verbatim}
34123434
\end{cfuncdesc}
34133435

34143436

0 commit comments

Comments
 (0)