Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-44116: Add GC support to _csv heap types #26074

Merged
merged 4 commits into from
May 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,12 @@ static void
Dialect_dealloc(DialectObj *self)
{
PyTypeObject *tp = Py_TYPE(self);
Py_CLEAR(self->lineterminator);
tp->tp_free((PyObject *)self);
PyObject_GC_UnTrack(self);
tp->tp_clear((PyObject *)self);
PyObject_GC_Del(self);
Py_DECREF(tp);
}

static void
Dialect_finalize(DialectObj *self)
{
Py_CLEAR(self->lineterminator);
}

static char *dialect_kws[] = {
"dialect",
"delimiter",
Expand Down Expand Up @@ -512,21 +507,37 @@ PyDoc_STRVAR(Dialect_Type_doc,
"\n"
"The Dialect type records CSV parsing and generation options.\n");

static int
Dialect_clear(DialectObj *self)
{
Py_CLEAR(self->lineterminator);
return 0;
}

static int
Dialect_traverse(DialectObj *self, visitproc visit, void *arg)
{
Py_VISIT(self->lineterminator);
Py_VISIT(Py_TYPE(self));
return 0;
}

static PyType_Slot Dialect_Type_slots[] = {
{Py_tp_doc, (char*)Dialect_Type_doc},
{Py_tp_members, Dialect_memberlist},
{Py_tp_getset, Dialect_getsetlist},
{Py_tp_new, dialect_new},
{Py_tp_methods, dialect_methods},
{Py_tp_finalize, Dialect_finalize},
{Py_tp_dealloc, Dialect_dealloc},
{Py_tp_clear, Dialect_clear},
{Py_tp_traverse, Dialect_traverse},
{0, NULL}
};

PyType_Spec Dialect_Type_spec = {
.name = "_csv.Dialect",
.basicsize = sizeof(DialectObj),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
.slots = Dialect_Type_slots,
};

Expand Down Expand Up @@ -885,9 +896,7 @@ Reader_dealloc(ReaderObj *self)
{
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);
Py_CLEAR(self->dialect);
Py_CLEAR(self->input_iter);
Py_CLEAR(self->fields);
tp->tp_clear((PyObject *)self);
if (self->field != NULL) {
PyMem_Free(self->field);
self->field = NULL;
Expand All @@ -896,24 +905,13 @@ Reader_dealloc(ReaderObj *self)
Py_DECREF(tp);
}

static void
Reader_finalize(ReaderObj *self)
{
Py_CLEAR(self->dialect);
Py_CLEAR(self->input_iter);
Py_CLEAR(self->fields);
if (self->field != NULL) {
PyMem_Free(self->field);
self->field = NULL;
}
}

static int
Reader_traverse(ReaderObj *self, visitproc visit, void *arg)
{
Py_VISIT(self->dialect);
Py_VISIT(self->input_iter);
Py_VISIT(self->fields);
Py_VISIT(Py_TYPE(self));
return 0;
}

Expand Down Expand Up @@ -948,12 +946,11 @@ static struct PyMemberDef Reader_memberlist[] = {
static PyType_Slot Reader_Type_slots[] = {
{Py_tp_doc, (char*)Reader_Type_doc},
{Py_tp_traverse, Reader_traverse},
{Py_tp_clear, Reader_clear},
{Py_tp_iter, PyObject_SelfIter},
{Py_tp_iternext, Reader_iternext},
{Py_tp_methods, Reader_methods},
{Py_tp_members, Reader_memberlist},
{Py_tp_finalize, Reader_finalize},
{Py_tp_clear, Reader_clear},
{Py_tp_dealloc, Reader_dealloc},
{0, NULL}
};
Expand Down Expand Up @@ -1339,6 +1336,7 @@ Writer_traverse(WriterObj *self, visitproc visit, void *arg)
Py_VISIT(self->dialect);
Py_VISIT(self->write);
Py_VISIT(self->error_obj);
Py_VISIT(Py_TYPE(self));
return 0;
}

Expand All @@ -1352,12 +1350,16 @@ Writer_clear(WriterObj *self)
}

static void
Writer_finalize(WriterObj *self)
Writer_dealloc(WriterObj *self)
{
Writer_clear(self);
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);
tp->tp_clear((PyObject *)self);
if (self->rec != NULL) {
PyMem_Free(self->rec);
}
PyObject_GC_Del(self);
Py_DECREF(tp);
}

PyDoc_STRVAR(Writer_Type_doc,
Expand All @@ -1368,10 +1370,10 @@ PyDoc_STRVAR(Writer_Type_doc,
);

static PyType_Slot Writer_Type_slots[] = {
{Py_tp_finalize, Writer_finalize},
{Py_tp_doc, (char*)Writer_Type_doc},
{Py_tp_traverse, Writer_traverse},
{Py_tp_clear, Writer_clear},
{Py_tp_dealloc, Writer_dealloc},
{Py_tp_methods, Writer_methods},
{Py_tp_members, Writer_memberlist},
{0, NULL}
Expand Down