GC for iterator objects.

This commit is contained in:
Neil Schemenauer 2001-07-12 13:27:25 +00:00
parent 19cd292bbc
commit 7eac9b72d4
1 changed files with 29 additions and 6 deletions

View File

@ -18,15 +18,24 @@ PySeqIter_New(PyObject *seq)
it->it_index = 0; it->it_index = 0;
Py_INCREF(seq); Py_INCREF(seq);
it->it_seq = seq; it->it_seq = seq;
PyObject_GC_Init(it);
return (PyObject *)it; return (PyObject *)it;
} }
static void static void
iter_dealloc(seqiterobject *it) iter_dealloc(seqiterobject *it)
{ {
PyObject_GC_Fini(it);
Py_DECREF(it->it_seq); Py_DECREF(it->it_seq);
it = (seqiterobject *) PyObject_AS_GC(it);
PyObject_DEL(it); PyObject_DEL(it);
} }
static int
iter_traverse(seqiterobject *it, visitproc visit, void *arg)
{
return visit(it->it_seq, arg);
}
static PyObject * static PyObject *
iter_next(seqiterobject *it, PyObject *args) iter_next(seqiterobject *it, PyObject *args)
{ {
@ -97,7 +106,7 @@ PyTypeObject PySeqIter_Type = {
PyObject_HEAD_INIT(&PyType_Type) PyObject_HEAD_INIT(&PyType_Type)
0, /* ob_size */ 0, /* ob_size */
"iterator", /* tp_name */ "iterator", /* tp_name */
sizeof(seqiterobject), /* tp_basicsize */ sizeof(seqiterobject) + PyGC_HEAD_SIZE, /* tp_basicsize */
0, /* tp_itemsize */ 0, /* tp_itemsize */
/* methods */ /* methods */
(destructor)iter_dealloc, /* tp_dealloc */ (destructor)iter_dealloc, /* tp_dealloc */
@ -115,9 +124,9 @@ PyTypeObject PySeqIter_Type = {
0, /* tp_getattro */ 0, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
0, /* tp_doc */ 0, /* tp_doc */
0, /* tp_traverse */ (traverseproc)iter_traverse, /* tp_traverse */
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
@ -144,16 +153,30 @@ PyCallIter_New(PyObject *callable, PyObject *sentinel)
it->it_callable = callable; it->it_callable = callable;
Py_INCREF(sentinel); Py_INCREF(sentinel);
it->it_sentinel = sentinel; it->it_sentinel = sentinel;
PyObject_GC_Init(it);
return (PyObject *)it; return (PyObject *)it;
} }
static void static void
calliter_dealloc(calliterobject *it) calliter_dealloc(calliterobject *it)
{ {
PyObject_GC_Fini(it);
Py_DECREF(it->it_callable); Py_DECREF(it->it_callable);
Py_DECREF(it->it_sentinel); Py_DECREF(it->it_sentinel);
it = (calliterobject *) PyObject_AS_GC(it);
PyObject_DEL(it); PyObject_DEL(it);
} }
static int
calliter_traverse(calliterobject *it, visitproc visit, void *arg)
{
int err;
if ((err = visit(it->it_callable, arg)))
return err;
if ((err = visit(it->it_sentinel, arg)))
return err;
return 0;
}
static PyObject * static PyObject *
calliter_next(calliterobject *it, PyObject *args) calliter_next(calliterobject *it, PyObject *args)
{ {
@ -200,7 +223,7 @@ PyTypeObject PyCallIter_Type = {
PyObject_HEAD_INIT(&PyType_Type) PyObject_HEAD_INIT(&PyType_Type)
0, /* ob_size */ 0, /* ob_size */
"callable-iterator", /* tp_name */ "callable-iterator", /* tp_name */
sizeof(calliterobject), /* tp_basicsize */ sizeof(calliterobject) + PyGC_HEAD_SIZE,/* tp_basicsize */
0, /* tp_itemsize */ 0, /* tp_itemsize */
/* methods */ /* methods */
(destructor)calliter_dealloc, /* tp_dealloc */ (destructor)calliter_dealloc, /* tp_dealloc */
@ -218,9 +241,9 @@ PyTypeObject PyCallIter_Type = {
0, /* tp_getattro */ 0, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
0, /* tp_doc */ 0, /* tp_doc */
0, /* tp_traverse */ (traverseproc)calliter_traverse, /* tp_traverse */
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */