From 7eac9b72d45f68d82e1dc463d1050aa86af94709 Mon Sep 17 00:00:00 2001 From: Neil Schemenauer Date: Thu, 12 Jul 2001 13:27:25 +0000 Subject: [PATCH] GC for iterator objects. --- Objects/iterobject.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/Objects/iterobject.c b/Objects/iterobject.c index 6a11772377e..c4f4e616b62 100644 --- a/Objects/iterobject.c +++ b/Objects/iterobject.c @@ -18,15 +18,24 @@ PySeqIter_New(PyObject *seq) it->it_index = 0; Py_INCREF(seq); it->it_seq = seq; + PyObject_GC_Init(it); return (PyObject *)it; } static void iter_dealloc(seqiterobject *it) { + PyObject_GC_Fini(it); Py_DECREF(it->it_seq); + it = (seqiterobject *) PyObject_AS_GC(it); PyObject_DEL(it); } +static int +iter_traverse(seqiterobject *it, visitproc visit, void *arg) +{ + return visit(it->it_seq, arg); +} + static PyObject * iter_next(seqiterobject *it, PyObject *args) { @@ -97,7 +106,7 @@ PyTypeObject PySeqIter_Type = { PyObject_HEAD_INIT(&PyType_Type) 0, /* ob_size */ "iterator", /* tp_name */ - sizeof(seqiterobject), /* tp_basicsize */ + sizeof(seqiterobject) + PyGC_HEAD_SIZE, /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ (destructor)iter_dealloc, /* tp_dealloc */ @@ -115,9 +124,9 @@ PyTypeObject PySeqIter_Type = { 0, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */ 0, /* tp_doc */ - 0, /* tp_traverse */ + (traverseproc)iter_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ @@ -144,16 +153,30 @@ PyCallIter_New(PyObject *callable, PyObject *sentinel) it->it_callable = callable; Py_INCREF(sentinel); it->it_sentinel = sentinel; + PyObject_GC_Init(it); return (PyObject *)it; } static void calliter_dealloc(calliterobject *it) { + PyObject_GC_Fini(it); Py_DECREF(it->it_callable); Py_DECREF(it->it_sentinel); + it = (calliterobject *) PyObject_AS_GC(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 * calliter_next(calliterobject *it, PyObject *args) { @@ -200,7 +223,7 @@ PyTypeObject PyCallIter_Type = { PyObject_HEAD_INIT(&PyType_Type) 0, /* ob_size */ "callable-iterator", /* tp_name */ - sizeof(calliterobject), /* tp_basicsize */ + sizeof(calliterobject) + PyGC_HEAD_SIZE,/* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ (destructor)calliter_dealloc, /* tp_dealloc */ @@ -218,9 +241,9 @@ PyTypeObject PyCallIter_Type = { 0, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */ 0, /* tp_doc */ - 0, /* tp_traverse */ + (traverseproc)calliter_traverse, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */