@@ -181,6 +181,83 @@ dict_popstring_null(PyObject *self, PyObject *args)
181181 RETURN_INT (PyDict_PopString (dict , key , NULL ));
182182}
183183
184+
185+ static int
186+ test_dict_inner (PyObject * self , int count )
187+ {
188+ Py_ssize_t pos = 0 , iterations = 0 ;
189+ int i ;
190+ PyObject * dict = PyDict_New ();
191+ PyObject * v , * k ;
192+
193+ if (dict == NULL )
194+ return -1 ;
195+
196+ for (i = 0 ; i < count ; i ++ ) {
197+ v = PyLong_FromLong (i );
198+ if (v == NULL ) {
199+ goto error ;
200+ }
201+ if (PyDict_SetItem (dict , v , v ) < 0 ) {
202+ Py_DECREF (v );
203+ goto error ;
204+ }
205+ Py_DECREF (v );
206+ }
207+
208+ k = v = UNINITIALIZED_PTR ;
209+ while (PyDict_Next (dict , & pos , & k , & v )) {
210+ PyObject * o ;
211+ iterations ++ ;
212+
213+ assert (k != UNINITIALIZED_PTR );
214+ assert (v != UNINITIALIZED_PTR );
215+ i = PyLong_AS_LONG (v ) + 1 ;
216+ o = PyLong_FromLong (i );
217+ if (o == NULL ) {
218+ goto error ;
219+ }
220+ if (PyDict_SetItem (dict , k , o ) < 0 ) {
221+ Py_DECREF (o );
222+ goto error ;
223+ }
224+ Py_DECREF (o );
225+ k = v = UNINITIALIZED_PTR ;
226+ }
227+ assert (k == UNINITIALIZED_PTR );
228+ assert (v == UNINITIALIZED_PTR );
229+
230+ Py_DECREF (dict );
231+
232+ if (iterations != count ) {
233+ PyErr_SetString (
234+ PyExc_AssertionError ,
235+ "test_dict_iteration: dict iteration went wrong " );
236+ return -1 ;
237+ } else {
238+ return 0 ;
239+ }
240+ error :
241+ Py_DECREF (dict );
242+ return -1 ;
243+ }
244+
245+
246+ static PyObject *
247+ test_dict_iteration (PyObject * self , PyObject * Py_UNUSED (ignored ))
248+ {
249+ int i ;
250+
251+ for (i = 0 ; i < 200 ; i ++ ) {
252+ if (test_dict_inner (self , i ) < 0 ) {
253+ return NULL ;
254+ }
255+ }
256+
257+ Py_RETURN_NONE ;
258+ }
259+
260+
184261static PyMethodDef test_methods [] = {
185262 {"dict_containsstring" , dict_containsstring , METH_VARARGS },
186263 {"dict_getitemref" , dict_getitemref , METH_VARARGS },
@@ -191,6 +268,7 @@ static PyMethodDef test_methods[] = {
191268 {"dict_pop_null" , dict_pop_null , METH_VARARGS },
192269 {"dict_popstring" , dict_popstring , METH_VARARGS },
193270 {"dict_popstring_null" , dict_popstring_null , METH_VARARGS },
271+ {"test_dict_iteration" , test_dict_iteration , METH_NOARGS },
194272 {NULL },
195273};
196274
0 commit comments