Skip to content

Commit 01edb37

Browse files
author
Erlend E. Aasland
committed
bpo-44116: Add GC support to _csv heap types
1 parent 2b458c1 commit 01edb37

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

Modules/_csv.c

+41-2
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ static PyGetSetDef Dialect_getsetlist[] = {
315315
static void
316316
Dialect_dealloc(DialectObj *self)
317317
{
318+
PyObject_GC_UnTrack(self);
318319
PyTypeObject *tp = Py_TYPE(self);
319320
Py_CLEAR(self->lineterminator);
320321
tp->tp_free((PyObject *)self);
@@ -512,6 +513,13 @@ PyDoc_STRVAR(Dialect_Type_doc,
512513
"\n"
513514
"The Dialect type records CSV parsing and generation options.\n");
514515

516+
static int
517+
Dialect_traverse(PyObject *self, visitproc visit, void *arg)
518+
{
519+
Py_VISIT(Py_TYPE(self));
520+
return 0;
521+
}
522+
515523
static PyType_Slot Dialect_Type_slots[] = {
516524
{Py_tp_doc, (char*)Dialect_Type_doc},
517525
{Py_tp_members, Dialect_memberlist},
@@ -520,13 +528,14 @@ static PyType_Slot Dialect_Type_slots[] = {
520528
{Py_tp_methods, dialect_methods},
521529
{Py_tp_finalize, Dialect_finalize},
522530
{Py_tp_dealloc, Dialect_dealloc},
531+
{Py_tp_traverse, Dialect_traverse},
523532
{0, NULL}
524533
};
525534

526535
PyType_Spec Dialect_Type_spec = {
527536
.name = "_csv.Dialect",
528537
.basicsize = sizeof(DialectObj),
529-
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
538+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
530539
.slots = Dialect_Type_slots,
531540
};
532541

@@ -914,6 +923,7 @@ Reader_traverse(ReaderObj *self, visitproc visit, void *arg)
914923
Py_VISIT(self->dialect);
915924
Py_VISIT(self->input_iter);
916925
Py_VISIT(self->fields);
926+
Py_VISIT(Py_TYPE(self));
917927
return 0;
918928
}
919929

@@ -1339,6 +1349,7 @@ Writer_traverse(WriterObj *self, visitproc visit, void *arg)
13391349
Py_VISIT(self->dialect);
13401350
Py_VISIT(self->write);
13411351
Py_VISIT(self->error_obj);
1352+
Py_VISIT(Py_TYPE(self));
13421353
return 0;
13431354
}
13441355

@@ -1351,6 +1362,15 @@ Writer_clear(WriterObj *self)
13511362
return 0;
13521363
}
13531364

1365+
static void
1366+
Writer_dealloc(WriterObj *self)
1367+
{
1368+
PyObject_GC_UnTrack(self);
1369+
PyTypeObject *tp = Py_TYPE(self);
1370+
tp->tp_clear((PyObject *)self);
1371+
Py_DECREF(tp);
1372+
}
1373+
13541374
static void
13551375
Writer_finalize(WriterObj *self)
13561376
{
@@ -1372,6 +1392,7 @@ static PyType_Slot Writer_Type_slots[] = {
13721392
{Py_tp_doc, (char*)Writer_Type_doc},
13731393
{Py_tp_traverse, Writer_traverse},
13741394
{Py_tp_clear, Writer_clear},
1395+
{Py_tp_dealloc, Writer_dealloc},
13751396
{Py_tp_methods, Writer_methods},
13761397
{Py_tp_members, Writer_memberlist},
13771398
{0, NULL}
@@ -1509,13 +1530,31 @@ csv_field_size_limit(PyObject *module, PyObject *args)
15091530
return PyLong_FromLong(old_limit);
15101531
}
15111532

1533+
static void
1534+
error_dealloc(PyObject *self)
1535+
{
1536+
PyObject_GC_UnTrack(self);
1537+
PyTypeObject *tp = Py_TYPE(self);
1538+
tp->tp_free((PyObject *)self);
1539+
Py_DECREF(tp);
1540+
}
1541+
1542+
static int
1543+
error_traverse(PyObject *self, visitproc visit, void *arg)
1544+
{
1545+
Py_VISIT(Py_TYPE(self));
1546+
return 0;
1547+
}
1548+
15121549
static PyType_Slot error_slots[] = {
1550+
{Py_tp_traverse, error_traverse},
1551+
{Py_tp_dealloc, error_dealloc},
15131552
{0, NULL},
15141553
};
15151554

15161555
PyType_Spec error_spec = {
15171556
.name = "_csv.Error",
1518-
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1557+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
15191558
.slots = error_slots,
15201559
};
15211560

0 commit comments

Comments
 (0)