File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -3397,7 +3397,8 @@ \subsection{Dictionary Objects \label{dictObjects}}
33973397all pairs have been reported. The parameters \var {pkey} and
33983398\var {pvalue} should either point to \ctype {PyObject*} variables that
33993399will 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+
34013402For 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
You can’t perform that action at this time.
0 commit comments