-
/* List object implementation */
#include "Python.h"
typedef struct {
PyObject_HEAD
long it_index;
- PyListObject *it_seq;
+ PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
} listiterobject;
PyTypeObject PyListIter_Type;
listiter_dealloc(listiterobject *it)
{
_PyObject_GC_UNTRACK(it);
- Py_DECREF(it->it_seq);
+ Py_XDECREF(it->it_seq);
PyObject_GC_Del(it);
}
static int
listiter_traverse(listiterobject *it, visitproc visit, void *arg)
{
+ if (it->it_seq == NULL)
+ return 0;
return visit((PyObject *)it->it_seq, arg);
}
assert(it != NULL);
seq = it->it_seq;
+ if (seq == NULL)
+ return NULL;
assert(PyList_Check(seq));
if (it->it_index < PyList_GET_SIZE(seq)) {
Py_INCREF(item);
return item;
}
+
+ Py_DECREF(seq);
+ it->it_seq = NULL;
return NULL;
}
-static PyMethodDef listiter_methods[] = {
- {"next", (PyCFunction)listiter_next, METH_NOARGS,
- "it.next() -- get the next value, or raise StopIteration"},
- {NULL, NULL} /* sentinel */
-};
-
PyTypeObject PyListIter_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /* ob_size */
0, /* tp_weaklistoffset */
(getiterfunc)listiter_getiter, /* tp_iter */
(iternextfunc)listiter_next, /* tp_iternext */
- listiter_methods, /* tp_methods */
+ 0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_descr_get */
0, /* tp_descr_set */
};
-