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

Skip to content

Commit db81e8d

Browse files
committed
Add support for weak references to the function and method types.
1 parent 6a1c87d commit db81e8d

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

Objects/classobject.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,7 @@ PyMethod_New(PyObject *func, PyObject *self, PyObject *class)
18051805
if (im == NULL)
18061806
return NULL;
18071807
}
1808+
im->im_weakreflist = NULL;
18081809
Py_INCREF(func);
18091810
im->im_func = func;
18101811
Py_XINCREF(self);
@@ -1902,6 +1903,7 @@ instancemethod_getattro(register PyMethodObject *im, PyObject *name)
19021903
static void
19031904
instancemethod_dealloc(register PyMethodObject *im)
19041905
{
1906+
PyObject_ClearWeakRefs((PyObject *)im);
19051907
PyObject_GC_Fini(im);
19061908
Py_DECREF(im->im_func);
19071909
Py_XDECREF(im->im_self);
@@ -2019,9 +2021,12 @@ PyTypeObject PyMethod_Type = {
20192021
(getattrofunc)instancemethod_getattro, /* tp_getattro */
20202022
(setattrofunc)instancemethod_setattro, /* tp_setattro */
20212023
0, /* tp_as_buffer */
2022-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
2024+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC | Py_TPFLAGS_HAVE_WEAKREFS,
20232025
0, /* tp_doc */
20242026
(traverseproc)instancemethod_traverse, /* tp_traverse */
2027+
0, /* tp_clear */
2028+
0, /* tp_richcompare */
2029+
offsetof(PyMethodObject, im_weakreflist) /* tp_weaklistoffset */
20252030
};
20262031

20272032
/* Clear out the free list */

Objects/funcobject.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ PyFunction_New(PyObject *code, PyObject *globals)
1313
if (op != NULL) {
1414
PyObject *doc;
1515
PyObject *consts;
16+
op->func_weakreflist = NULL;
1617
Py_INCREF(code);
1718
op->func_code = code;
1819
Py_INCREF(globals);
@@ -245,6 +246,7 @@ func_setattro(PyFunctionObject *op, PyObject *name, PyObject *value)
245246
static void
246247
func_dealloc(PyFunctionObject *op)
247248
{
249+
PyObject_ClearWeakRefs((PyObject *) op);
248250
PyObject_GC_Fini(op);
249251
Py_DECREF(op->func_code);
250252
Py_DECREF(op->func_globals);
@@ -327,13 +329,16 @@ PyTypeObject PyFunction_Type = {
327329
0, /*tp_as_number*/
328330
0, /*tp_as_sequence*/
329331
0, /*tp_as_mapping*/
330-
0, /*tp_hash*/
332+
0, /*tp_hash*/
331333
0, /*tp_call*/
332334
0, /*tp_str*/
333335
(getattrofunc)func_getattro, /*tp_getattro*/
334336
(setattrofunc)func_setattro, /*tp_setattro*/
335337
0, /* tp_as_buffer */
336-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
338+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC | Py_TPFLAGS_HAVE_WEAKREFS,
337339
0, /* tp_doc */
338340
(traverseproc)func_traverse, /* tp_traverse */
341+
0, /* tp_clear */
342+
0, /* tp_richcompare */
343+
offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
339344
};

0 commit comments

Comments
 (0)