Skip to content

Commit b361207

Browse files
naschemeambv
authored andcommitted
Restore tp_clear for function object. (#16502)
This is a revert of the revert (GH-15826). Having a tp_clear for functions should be safe (and helpful) now that bpo-38006 has been fixed.
1 parent 2f644c0 commit b361207

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

Objects/funcobject.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -570,23 +570,31 @@ func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
570570
return (PyObject *)newfunc;
571571
}
572572

573+
static int
574+
func_clear(PyFunctionObject *op)
575+
{
576+
Py_CLEAR(op->func_code);
577+
Py_CLEAR(op->func_globals);
578+
Py_CLEAR(op->func_module);
579+
Py_CLEAR(op->func_name);
580+
Py_CLEAR(op->func_defaults);
581+
Py_CLEAR(op->func_kwdefaults);
582+
Py_CLEAR(op->func_doc);
583+
Py_CLEAR(op->func_dict);
584+
Py_CLEAR(op->func_closure);
585+
Py_CLEAR(op->func_annotations);
586+
Py_CLEAR(op->func_qualname);
587+
return 0;
588+
}
589+
573590
static void
574591
func_dealloc(PyFunctionObject *op)
575592
{
576593
_PyObject_GC_UNTRACK(op);
577-
if (op->func_weakreflist != NULL)
594+
if (op->func_weakreflist != NULL) {
578595
PyObject_ClearWeakRefs((PyObject *) op);
579-
Py_DECREF(op->func_code);
580-
Py_DECREF(op->func_globals);
581-
Py_XDECREF(op->func_module);
582-
Py_DECREF(op->func_name);
583-
Py_XDECREF(op->func_defaults);
584-
Py_XDECREF(op->func_kwdefaults);
585-
Py_XDECREF(op->func_doc);
586-
Py_XDECREF(op->func_dict);
587-
Py_XDECREF(op->func_closure);
588-
Py_XDECREF(op->func_annotations);
589-
Py_XDECREF(op->func_qualname);
596+
}
597+
(void)func_clear(op);
590598
PyObject_GC_Del(op);
591599
}
592600

@@ -661,7 +669,7 @@ PyTypeObject PyFunction_Type = {
661669
Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
662670
func_new__doc__, /* tp_doc */
663671
(traverseproc)func_traverse, /* tp_traverse */
664-
0, /* tp_clear */
672+
(inquiry)func_clear, /* tp_clear */
665673
0, /* tp_richcompare */
666674
offsetof(PyFunctionObject, func_weakreflist), /* tp_weaklistoffset */
667675
0, /* tp_iter */

0 commit comments

Comments
 (0)