summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-04-13 17:08:15 (GMT)
committerGuido van Rossum <guido@python.org>2001-04-13 17:08:15 (GMT)
commiteb0d992520c99a1756be36199e976af8d66fb491 (patch)
tree81dbfabca5a181df3923c2957ec7a5e2f37c659a /Modules
parent361c53586393e5d2920f49d28c3079264ce1957c (diff)
downloadcpython-eb0d992520c99a1756be36199e976af8d66fb491.zip
cpython-eb0d992520c99a1756be36199e976af8d66fb491.tar.gz
cpython-eb0d992520c99a1756be36199e976af8d66fb491.tar.bz2
Slight adaptation of Michael Hudson's patch to test PyDict_Next()
(with modification of existing dict elements!). This is part of SF patch #409864: lazy fix for Pings bizarre scoping crash. The adaptation I made to Michael's patch was to change the error handling to avoid masking other errors (moving the specific error message to inside test_dict_inner()), and to insert a test for dict==NULL at the start.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_testcapimodule.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 0ffdc17..24cf2b3 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -95,9 +95,71 @@ test_list_api(PyObject *self, PyObject *args)
return Py_None;
}
+static int
+test_dict_inner(int count)
+{
+ int pos = 0, iterations = 0, i;
+ PyObject *dict = PyDict_New();
+ PyObject *v, *k;
+
+ if (dict == NULL)
+ return -1;
+
+ for (i = 0; i < count; i++) {
+ v = PyInt_FromLong(i);
+ PyDict_SetItem(dict, v, v);
+ Py_DECREF(v);
+ }
+
+ while (PyDict_Next(dict, &pos, &k, &v)) {
+ PyObject *o;
+ iterations++;
+
+ i = PyInt_AS_LONG(v) + 1;
+ o = PyInt_FromLong(i);
+ if (o == NULL)
+ return -1;
+ if (PyDict_SetItem(dict, k, o) < 0) {
+ Py_DECREF(o);
+ return -1;
+ }
+ Py_DECREF(o);
+ }
+
+ Py_DECREF(dict);
+
+ if (iterations != count) {
+ PyErr_SetString(
+ TestError,
+ "test_dict_iteration: dict iteration went wrong ");
+ return -1;
+ } else {
+ return 0;
+ }
+}
+
+static PyObject*
+test_dict_iteration(PyObject* self, PyObject* args)
+{
+ int i;
+
+ if (!PyArg_ParseTuple(args, ":test_dict_iteration"))
+ return NULL;
+
+ for (i = 0; i < 200; i++) {
+ if (test_dict_inner(i) < 0) {
+ return NULL;
+ }
+ }
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
static PyMethodDef TestMethods[] = {
{"test_config", test_config, METH_VARARGS},
{"test_list_api", test_list_api, METH_VARARGS},
+ {"test_dict_iteration", test_dict_iteration, METH_VARARGS},
{NULL, NULL} /* sentinel */
};