Skip to content

Commit

Permalink
fix: handle weakrefs consistently acrosss Python versions (#324)
Browse files Browse the repository at this point in the history
Closes #323.
---------

Co-authored-by: Jens Vagelpohl <[email protected]>
Co-authored-by: David Glick <[email protected]>
  • Loading branch information
3 people authored Oct 21, 2024
1 parent d246ed1 commit 8f42047
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
7.1.1 (unreleased)
==================

- Fix segmentation faults in `weakrefobject.c` on Python 3.12 and 3.13.
(`#323 <https://github.com/zopefoundation/zope.interface/issues/323>`_)

7.1.0 (2024-10-10)
==================
Expand Down
29 changes: 11 additions & 18 deletions src/zope/interface/_zope_interface_coptimizations.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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) */
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
};

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8f42047

Please sign in to comment.