Fred Drake avatar Fred Drake committed 8e1415e

Michael Hudson:
Update docs for PyDict_Next() based on the most recent changes to the
dictionary code.

This closes SF patch #409864.

Comments (0)

Files changed (1)

 all pairs have been reported.  The parameters \var{pkey} and
 \var{pvalue} should either point to \ctype{PyObject*} variables that
 will be filled in with each key and value, respectively, or may be
-\NULL.  The dictionary \var{p} must not be mutated during iteration.
+\NULL.
+
 For example:
 
 \begin{verbatim}
     ...
 }
 \end{verbatim}
+
+The dictionary \var{p} should not be mutated during iteration.  It is
+safe (since Python 2.1) to modify the values of the keys as you
+iterate over the dictionary, for example:
+
+\begin{verbatim}
+PyObject *key, *value;
+int pos = 0;
+
+while (PyDict_Next(self->dict, &pos, &key, &value)) {
+    int i = PyInt_AS_LONG(value) + 1;
+    PyObject *o = PyInt_FromLong(i);
+    if (o == NULL)
+        return -1;
+    if (PyDict_SetItem(self->dict, key, o) < 0) {
+        Py_DECREF(o);
+        return -1;
+    }
+    Py_DECREF(o);
+}
+\end{verbatim}
 \end{cfuncdesc}
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.