[3.13] gh-135607: remove null checking of weakref list in dealloc of extension modules and objects (#135614) (#136126)

gh-135607: remove null checking of weakref list in dealloc of extension modules and objects (#135614)

(cherry picked from commit b1056c2a44)

Co-authored-by: Xuanteng Huang <44627253+xuantengh@users.noreply.github.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This commit is contained in:
Victor Stinner 2025-07-01 11:26:52 +02:00 committed by GitHub
parent 1e3466a1ae
commit e3a277c8d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 75 additions and 73 deletions

View file

@ -7,6 +7,7 @@
#include "pycore_object.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()
/* undefine macro trampoline to PyCFunction_NewEx */
@ -162,9 +163,7 @@ meth_dealloc(PyCFunctionObject *m)
// call PyObject_GC_UnTrack twice on an object.
PyObject_GC_UnTrack(m);
Py_TRASHCAN_BEGIN(m, meth_dealloc);
if (m->m_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject*) m);
}
FT_CLEAR_WEAKREFS((PyObject*) m, m->m_weakreflist);
// Dereference class before m_self: PyCFunction_GET_CLASS accesses
// PyMethodDef m_ml, which could be kept alive by m_self
Py_XDECREF(PyCFunction_GET_CLASS(m));