Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8107176

Browse files
committed
add gc support to the AST base type (closes #15293)
1 parent 4d378d8 commit 8107176

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

Parser/asdl_c.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,19 @@ def visitModule(self, mod):
615615
Py_TYPE(self)->tp_free(self);
616616
}
617617
618+
static int
619+
ast_traverse(AST_object *self, visitproc visit, void *arg)
620+
{
621+
Py_VISIT(self->dict);
622+
return 0;
623+
}
624+
625+
static void
626+
ast_clear(AST_object *self)
627+
{
628+
Py_CLEAR(self->dict);
629+
}
630+
618631
static int
619632
ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
620633
{
@@ -718,10 +731,10 @@ def visitModule(self, mod):
718731
PyObject_GenericGetAttr, /* tp_getattro */
719732
PyObject_GenericSetAttr, /* tp_setattro */
720733
0, /* tp_as_buffer */
721-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
734+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
722735
0, /* tp_doc */
723-
0, /* tp_traverse */
724-
0, /* tp_clear */
736+
(traverseproc)ast_traverse, /* tp_traverse */
737+
(inquiry)ast_clear, /* tp_clear */
725738
0, /* tp_richcompare */
726739
0, /* tp_weaklistoffset */
727740
0, /* tp_iter */
@@ -737,7 +750,7 @@ def visitModule(self, mod):
737750
(initproc)ast_type_init, /* tp_init */
738751
PyType_GenericAlloc, /* tp_alloc */
739752
PyType_GenericNew, /* tp_new */
740-
PyObject_Del, /* tp_free */
753+
PyObject_GC_Del, /* tp_free */
741754
};
742755
743756

Python/Python-ast.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,19 @@ ast_dealloc(AST_object *self)
467467
Py_TYPE(self)->tp_free(self);
468468
}
469469

470+
static int
471+
ast_traverse(AST_object *self, visitproc visit, void *arg)
472+
{
473+
Py_VISIT(self->dict);
474+
return 0;
475+
}
476+
477+
static void
478+
ast_clear(AST_object *self)
479+
{
480+
Py_CLEAR(self->dict);
481+
}
482+
470483
static int
471484
ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
472485
{
@@ -570,10 +583,10 @@ static PyTypeObject AST_type = {
570583
PyObject_GenericGetAttr, /* tp_getattro */
571584
PyObject_GenericSetAttr, /* tp_setattro */
572585
0, /* tp_as_buffer */
573-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
586+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
574587
0, /* tp_doc */
575-
0, /* tp_traverse */
576-
0, /* tp_clear */
588+
(traverseproc)ast_traverse, /* tp_traverse */
589+
(inquiry)ast_clear, /* tp_clear */
577590
0, /* tp_richcompare */
578591
0, /* tp_weaklistoffset */
579592
0, /* tp_iter */
@@ -589,7 +602,7 @@ static PyTypeObject AST_type = {
589602
(initproc)ast_type_init, /* tp_init */
590603
PyType_GenericAlloc, /* tp_alloc */
591604
PyType_GenericNew, /* tp_new */
592-
PyObject_Del, /* tp_free */
605+
PyObject_GC_Del, /* tp_free */
593606
};
594607

595608

0 commit comments

Comments
 (0)