mirror of https://github.com/python/cpython
GC for iterator objects.
This commit is contained in:
parent
19cd292bbc
commit
7eac9b72d4
|
@ -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 */
|
||||
|
|
Loading…
Reference in New Issue