Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: call 'PyObject_ClearWeakRefs' unconditionally #324

Merged
merged 7 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading