From 8f420473b30dfb240d5a32f491f42e39b97d47f1 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Mon, 21 Oct 2024 12:51:37 -0400 Subject: [PATCH] fix: handle weakrefs consistently acrosss Python versions (#324) Closes #323. --------- Co-authored-by: Jens Vagelpohl Co-authored-by: David Glick --- CHANGES.rst | 2 ++ .../_zope_interface_coptimizations.c | 29 +++++++------------ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index ad3f6759..2c693f41 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,8 @@ 7.1.1 (unreleased) ================== +- Fix segmentation faults in `weakrefobject.c` on Python 3.12 and 3.13. + (`#323 `_) 7.1.0 (2024-10-10) ================== diff --git a/src/zope/interface/_zope_interface_coptimizations.c b/src/zope/interface/_zope_interface_coptimizations.c index 453057a4..45810ec6 100644 --- a/src/zope/interface/_zope_interface_coptimizations.c +++ b/src/zope/interface/_zope_interface_coptimizations.c @@ -49,6 +49,11 @@ #define USE_HEAP_TYPES 1 #endif +#define BASETYPE_FLAGS \ + Py_TPFLAGS_DEFAULT | \ + Py_TPFLAGS_BASETYPE | \ + Py_TPFLAGS_HAVE_GC + #if PY_VERSION_HEX >= 0x030c0000 /* Add MANAGED_WEAKREF flag for Python >= 3.12, and don't define * the '.tp_weaklistoffset' slot. @@ -57,11 +62,7 @@ * #c.PyTypeObject.tp_weaklistoffset */ #define USE_EXPLICIT_WEAKREFLIST 0 -#define BASETYPE_FLAGS \ - Py_TPFLAGS_DEFAULT | \ - Py_TPFLAGS_BASETYPE | \ - Py_TPFLAGS_MANAGED_WEAKREF | \ - Py_TPFLAGS_HAVE_GC +#define WEAKREFTYPE_FLAGS BASETYPE_FLAGS | Py_TPFLAGS_MANAGED_WEAKREF #else /* No MANAGED_WEAKREF flag for Python < 3.12, and therefore define * the '.tp_weaklistoffset' slot, and the member whose offset it holds. @@ -70,10 +71,7 @@ * #c.PyTypeObject.tp_weaklistoffset */ #define USE_EXPLICIT_WEAKREFLIST 1 -#define BASETYPE_FLAGS \ - Py_TPFLAGS_DEFAULT | \ - Py_TPFLAGS_BASETYPE | \ - Py_TPFLAGS_HAVE_GC +#define WEAKREFTYPE_FLAGS BASETYPE_FLAGS #endif /* Static strings, used to invoke PyObject_GetAttr (only in hot paths) */ @@ -276,12 +274,8 @@ static void SB_dealloc(SB* self) { PyObject_GC_UnTrack((PyObject*)self); + PyObject_ClearWeakRefs(OBJECT(self)); PyTypeObject* tp = Py_TYPE(self); -#if USE_EXPLICIT_WEAKREFLIST - if (self->weakreflist != NULL) { - PyObject_ClearWeakRefs(OBJECT(self)); - } -#endif SB_clear(self); tp->tp_free(OBJECT(self)); #if USE_HEAP_TYPES @@ -419,7 +413,7 @@ static PyTypeObject SB_type_def = { .tp_name = SB__name__, .tp_doc = SB__doc__, .tp_basicsize = sizeof(SB), - .tp_flags = BASETYPE_FLAGS, + .tp_flags = WEAKREFTYPE_FLAGS, .tp_call = (ternaryfunc)SB__call__, .tp_traverse = (traverseproc)SB_traverse, .tp_clear = (inquiry)SB_clear, @@ -450,7 +444,7 @@ static PyType_Slot SB_type_slots[] = { static PyType_Spec SB_type_spec = { .name = SB__name__, .basicsize = sizeof(SB), - .flags = BASETYPE_FLAGS, + .flags = WEAKREFTYPE_FLAGS, .slots = SB_type_slots }; @@ -569,8 +563,7 @@ CPB_clear(CPB* self) { Py_CLEAR(self->_cls); Py_CLEAR(self->_implements); - SB_clear((SB*)self); - return 0; + return SB_clear((SB*)self); } static void