10000 bpo-44116: Add GC support to _csv heap types (GH-26074) · python/cpython@e5ba1fe · GitHub
[go: up one dir, main page]

Skip to content

Commit e5ba1fe

Browse files
author
Erlend Egeberg Aasland
authored
bpo-44116: Add GC support to _csv heap types (GH-26074)
1 parent ff23015 commit e5ba1fe

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

Modules/_csv.c

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,12 @@ static void
316316
Dialect_dealloc(DialectObj *self)
317317
{
318318
PyTypeObject *tp = Py_TYPE(self);
319-
Py_CLEAR(self->lineterminator);
320-
tp->tp_free((PyObject *)self);
319+
PyObject_GC_UnTrack(self);
320+
tp->tp_clear((PyObject *)self);
321+
PyObject_GC_Del(self);
321322
Py_DECREF(tp);
322323
}
323324

324-
static void
325-
Dialect_finalize(DialectObj *self)
326-
{
327-
Py_CLEAR(self->lineterminator);
328-
}
329-
330325
static char *dialect_kws[] = {
331326
"dialect",
332327
"delimiter",
@@ -512,21 +507,37 @@ PyDoc_STRVAR(Dialect_Type_doc,
512507
"\n"
513508
"The Dialect type records CSV parsing and generation options.\n");
514509

510+
static int
511+
Dialect_clear(DialectObj *self)
512+
{
513+
Py_CLEAR(self->lineterminator);
514+
return 0;
515+
}
516+
517+
static int
518+
Dialect_traverse(DialectObj *self, visitproc visit, void *arg)
519+
{
520+
Py_VISIT(self->lineterminator);
521+
Py_VISIT(Py_TYPE(self));
522+
return 0;
523+
}
524+
515525
static PyType_Slot Dialect_Type_slots[] = {
516526
{Py_tp_doc, (char*)Dialect_Type_doc},
517527
{Py_tp_members, Dialect_memberlist},
518528
{Py_tp_getset, Dialect_getsetlist},
519529
{Py_tp_new, dialect_new},
520530
{Py_tp_methods, dialect_methods},
521-
{Py_tp_finalize, Dialect_finalize},
522531
{Py_tp_dealloc, Dialect_dealloc},
532+
{Py_tp_clear, Dialect_clear},
533+
{Py_tp_traverse, Dialect_traverse},
523534
{0, NULL}
524535
};
525536

526537
PyType_Spec Dialect_Type_spec = {
527538
.name = "_csv.Dialect",
528539
.basicsize = sizeof(DialectObj),
529-
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
540+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
530541
.slots = Dialect_Type_slots,
531542
};
532543

@@ -885,9 +896,7 @@ Reader_dealloc(ReaderObj *self)
885896
{
886897
PyTypeObject *tp = Py_TYPE(self);
887898
PyObject_GC_UnTrack(self);
888-
Py_CLEAR(self->dialect);
889-
Py_CLEAR(self->input_iter);
890-
Py_CLEAR(self->fields);
899+
tp->tp_clear((PyObject *)self);
891900
if (self->field != NULL) {
892901
PyMem_Free(self->field);
893902
self->field = NULL;
@@ -896,24 +905,13 @@ Reader_dealloc(ReaderObj *self)
896905
Py_DECREF(tp);
897906
}
898907

899-
static void
900-
Reader_finalize(ReaderObj *self)
901-
{
902-
Py_CLEAR(self->dialect);
903-
Py_CLEAR(self->input_iter);
904-
Py_CLEAR(self->fields);
905-
if (self->field != NULL) {
906-
PyMem_Free(self->field);
907-
self->field = NULL;
908-
}
909-
}
910-
911908
static int
912909
Reader_traverse(ReaderObj *self, visitproc visit, void *arg)
913910
{
914911
Py_VISIT(self->dialect);
915912
Py_VISIT(self->input_iter);
916913
Py_VISIT(self->fields);
914+
Py_VISIT(Py_TYPE(self));
917915
return 0;
918916
}
919917

@@ -948,12 +946,11 @@ static struct PyMemberDef Reader_memberlist[] = {
948946
static PyType_Slot Reader_Type_slots[] = {
949947
{Py_tp_doc, (char*)Reader_Type_doc},
950948
{Py_tp_traverse, Reader_traverse},
951-
{Py_tp_clear, Reader_clear},
952949
{Py_tp_iter, PyObject_SelfIter},
953950
{Py_tp_iternext, Reader_iternext},
954951
{Py_tp_methods, Reader_methods},
955952
{Py_tp_members, Reader_memberlist},
956-
{Py_tp_finalize, Reader_finalize},
953+
{Py_tp_clear, Reader_clear},
957954
{Py_tp_dealloc, Reader_dealloc},
958955
{0, NULL}
959956
};
@@ -1339,6 +1336,7 @@ Writer_traverse(WriterObj *self, visitproc visit, void *arg)
13391336
Py_VISIT(self->dialect);
13401337
Py_VISIT(self->write);
13411338
Py_VISIT(self->error_obj);
1339+
Py_VISIT(Py_TYPE(self));
13421340
return 0;
13431341
}
13441342

@@ -1352,12 +1350,16 @@ Writer_clear(WriterObj *self)
13521350
}
13531351

13541352
static void
1355-
Writer_finalize(WriterObj *self)
1353+
Writer_dealloc(WriterObj *self)
13561354
{
1357-
Writer_clear(self);
1355+
PyTypeObject *tp = Py_TYPE(self);
1356+
PyObject_GC_UnTrack(self);
1357+
tp->tp_clear((PyObject *)self);
13581358
if (self->rec != NULL) {
13591359
PyMem_Free(self->rec);
13601360
}
1361+
PyObject_GC_Del(self);
1362+
Py_DECREF(tp);
13611363
}
13621364

13631365
PyDoc_STRVAR(Writer_Type_doc,
@@ -1368,10 +1370,10 @@ PyDoc_STRVAR(Writer_Type_doc,
13681370
);
13691371

13701372
static PyType_Slot Writer_Type_slots[] = {
1371-
{Py_tp_finalize, Writer_finalize},
13721373
{Py_tp_doc, (char*)Writer_Type_doc},
13731374
{Py_tp_traverse, Writer_traverse},
13741375
{Py_tp_clear, Writer_clear},
1376+
{Py_tp_dealloc, Writer_dealloc},
13751377
{Py_tp_methods, Writer_methods},
13761378
{Py_tp_members, Writer_memberlist},
13771379
{0, NULL}

0 commit comments

Comments
 (0)
0