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

Skip to content

Commit 10c6692

Browse files
committed
GC for method objects.
1 parent 7eac9b7 commit 10c6692

1 file changed

Lines changed: 31 additions & 11 deletions

File tree

Objects/methodobject.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ PyCFunction_New(PyMethodDef *ml, PyObject *self)
2424
op->m_ml = ml;
2525
Py_XINCREF(self);
2626
op->m_self = self;
27+
PyObject_GC_Init(op);
2728
return (PyObject *)op;
2829
}
2930

@@ -62,11 +63,21 @@ PyCFunction_GetFlags(PyObject *op)
6263
static void
6364
meth_dealloc(PyCFunctionObject *m)
6465
{
66+
PyObject_GC_Fini(m);
6567
Py_XDECREF(m->m_self);
6668
m->m_self = (PyObject *)free_list;
6769
free_list = m;
6870
}
6971

72+
static int
73+
meth_traverse(PyCFunctionObject *m, visitproc visit, void *arg)
74+
{
75+
if (m->m_self != NULL)
76+
return visit(m->m_self, arg);
77+
else
78+
return 0;
79+
}
80+
7081
static PyObject *
7182
meth_getattr(PyCFunctionObject *m, char *name)
7283
{
@@ -152,18 +163,26 @@ PyTypeObject PyCFunction_Type = {
152163
PyObject_HEAD_INIT(&PyType_Type)
153164
0,
154165
"builtin_function_or_method",
155-
sizeof(PyCFunctionObject),
166+
sizeof(PyCFunctionObject) + PyGC_HEAD_SIZE,
156167
0,
157-
(destructor)meth_dealloc, /*tp_dealloc*/
158-
0, /*tp_print*/
159-
(getattrfunc)meth_getattr, /*tp_getattr*/
160-
0, /*tp_setattr*/
161-
(cmpfunc)meth_compare, /*tp_compare*/
162-
(reprfunc)meth_repr, /*tp_repr*/
163-
0, /*tp_as_number*/
164-
0, /*tp_as_sequence*/
165-
0, /*tp_as_mapping*/
166-
(hashfunc)meth_hash, /*tp_hash*/
168+
(destructor)meth_dealloc, /* tp_dealloc */
169+
0, /* tp_print */
170+
(getattrfunc)meth_getattr, /* tp_getattr */
171+
0, /* tp_setattr */
172+
(cmpfunc)meth_compare, /* tp_compare */
173+
(reprfunc)meth_repr, /* tp_repr */
174+
0, /* tp_as_number */
175+
0, /* tp_as_sequence */
176+
0, /* tp_as_mapping */
177+
(hashfunc)meth_hash, /* tp_hash */
178+
0, /* tp_call */
179+
0, /* tp_str */
180+
0, /* tp_getattro */
181+
0, /* tp_setattro */
182+
0, /* tp_as_buffer */
183+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
184+
0, /* tp_doc */
185+
(traverseproc)meth_traverse, /* tp_traverse */
167186
};
168187

169188
/* List all methods in a chain -- helper for findmethodinchain */
@@ -245,6 +264,7 @@ PyCFunction_Fini(void)
245264
while (free_list) {
246265
PyCFunctionObject *v = free_list;
247266
free_list = (PyCFunctionObject *)(v->m_self);
267+
v = (PyCFunctionObject *) PyObject_AS_GC(v);
248268
PyObject_DEL(v);
249269
}
250270
}

0 commit comments

Comments
 (0)