49
49
#define USE_HEAP_TYPES 1
50
50
#endif
51
51
52
- /* Add MANAGED_WEAKREF flag for Python >= 3.12 */
53
52
#if PY_VERSION_HEX >= 0x030c0000
53
+ /* Add MANAGED_WEAKREF flag for Python >= 3.12, and don't define
54
+ * the '.tp_weaklistoffset' slot.
55
+ *
56
+ * See: https://docs.python.org/3/c-api/typeobj.html
57
+ * #c.PyTypeObject.tp_weaklistoffset
58
+ */
59
+ #define USE_EXPLICIT_WEAKREFLIST 0
54
60
#define LEAFTYPE_FLAGS \
55
61
Py_TPFLAGS_DEFAULT | \
56
62
Py_TPFLAGS_MANAGED_WEAKREF | \
61
67
Py_TPFLAGS_MANAGED_WEAKREF | \
62
68
Py_TPFLAGS_HAVE_GC
63
69
#else
70
+ /* No MANAGED_WEAKREF flag for Python < 3.12, and therefore define
71
+ * the '.tp_weaklistoffset' slot, and the member whose offset it holds.
72
+ *
73
+ * See: https://docs.python.org/3/c-api/typeobj.html
74
+ * #c.PyTypeObject.tp_weaklistoffset
75
+ */
76
+ #define USE_EXPLICIT_WEAKREFLIST 1
64
77
#define LEAFTYPE_FLAGS \
65
78
Py_TPFLAGS_DEFAULT | \
66
79
Py_TPFLAGS_HAVE_GC
@@ -217,7 +230,7 @@ typedef struct
217
230
make any assumptions about contents.
218
231
*/
219
232
PyObject * _implied ;
220
- #if PY_VERSION_HEX < 0x030c0000
233
+ #if USE_EXPLICIT_WEAKREFLIST
221
234
PyObject * weakreflist ;
222
235
#endif
223
236
/*
@@ -271,7 +284,7 @@ SB_dealloc(SB* self)
271
284
{
272
285
PyObject_GC_UnTrack ((PyObject * )self );
273
286
PyTypeObject * tp = Py_TYPE (self );
274
- #if PY_VERSION_HEX < 0x030c0000
287
+ #if USE_EXPLICIT_WEAKREFLIST
275
288
if (self -> weakreflist != NULL ) {
276
289
PyObject_ClearWeakRefs (OBJECT (self ));
277
290
}
@@ -393,7 +406,7 @@ static PyMemberDef SB_members[] = {
393
406
{ "_v_attrs" , T_OBJECT_EX , offsetof(SB , _v_attrs ), 0 , "" },
394
407
{ "__iro__" , T_OBJECT_EX , offsetof(SB , __iro__ ), 0 , "" },
395
408
{ "__sro__" , T_OBJECT_EX , offsetof(SB , __sro__ ), 0 , "" },
396
- #if PY_VERSION_HEX < 0x030c0000
409
+ #if USE_EXPLICIT_WEAKREFLIST
397
410
{ "__weaklistoffset__" , T_OBJECT_EX , offsetof(SB , weakreflist ), 0 , "" },
398
411
#endif
399
412
{ NULL },
@@ -418,7 +431,9 @@ static PyTypeObject SB_type_def = {
418
431
.tp_traverse = (traverseproc )SB_traverse ,
419
432
.tp_clear = (inquiry )SB_clear ,
420
433
.tp_dealloc = (destructor )SB_dealloc ,
421
- .tp_weaklistoffset = offsetof(SB , weakreflist ), /* XXX */
434
+ #if USE_EXPLICIT_WEAKREFLIST
435
+ .tp_weaklistoffset = offsetof(SB , weakreflist ),
436
+ #endif
422
437
.tp_methods = SB_methods ,
423
438
.tp_members = SB_members ,
424
439
};
0 commit comments