Skip to content

Commit f7bf5f8

Browse files
Py_TPFLAGS_MANAGED_WEAKREF implies Py_TPFLAGS_HAVE_GC too and force checking of it presence
1 parent b57b619 commit f7bf5f8

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

Doc/c-api/typeobj.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,8 @@ and :c:data:`PyType_Type` effectively act as defaults.)
12791279
This bit indicates that instances of the class should be weakly
12801280
referenceable.
12811281

1282+
If this flag is set, :c:macro:`Py_TPFLAGS_HAVE_GC` should also be set.
1283+
12821284
.. versionadded:: 3.12
12831285

12841286
**Inheritance:**

Doc/extending/newtypes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,8 @@ For an object to be weakly referenceable, the extension type must set the
560560
field. The legacy :c:member:`~PyTypeObject.tp_weaklistoffset` field should
561561
be left as zero.
562562

563+
If this flag is set, :c:macro:`Py_TPFLAGS_HAVE_GC` should also be set.
564+
563565
Concretely, here is how the statically declared type object would look::
564566

565567
static PyTypeObject TrivialType = {

Doc/whatsnew/3.15.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,14 @@ New features
310310
(Contributed by Petr Viktorin in :gh:`131510`)
311311

312312

313+
Limited C API changes
314+
---------------------
315+
316+
* If the :c:macro:`Py_TPFLAGS_MANAGED_DICT` and :c:macro:`Py_TPFLAGS_MANAGED_WEAKREF`
317+
flags are set then :c:macro:`Py_TPFLAGS_HAVE_GC` should be set too.
318+
(Contributed by Sergey Miryanov.)
319+
320+
313321
Porting to Python 3.15
314322
----------------------
315323

Include/object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ given type object has a specified feature.
525525
#define Py_TPFLAGS_INLINE_VALUES (1 << 2)
526526

527527
/* Placement of weakref pointers are managed by the VM, not by the type.
528-
* The VM will automatically set tp_weaklistoffset.
528+
* The VM will automatically set tp_weaklistoffset. Implies Py_TPFLAGS_HAVE_GC.
529529
*/
530530
#define Py_TPFLAGS_MANAGED_WEAKREF (1 << 3)
531531

Objects/typeobject.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8804,6 +8804,13 @@ type_ready_preheader(PyTypeObject *type)
88048804
type->tp_name);
88058805
return -1;
88068806
}
8807+
if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC)) {
8808+
PyErr_Format(PyExc_SystemError,
8809+
"type %s has the Py_TPFLAGS_MANAGED_DICT flag "
8810+
"but not Py_TPFLAGS_HAVE_GC flag",
8811+
type->tp_name);
8812+
return -1;
8813+
}
88078814
type->tp_dictoffset = -1;
88088815
}
88098816
if (type->tp_flags & Py_TPFLAGS_MANAGED_WEAKREF) {
@@ -8816,6 +8823,13 @@ type_ready_preheader(PyTypeObject *type)
88168823
type->tp_name);
88178824
return -1;
88188825
}
8826+
if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC)) {
8827+
PyErr_Format(PyExc_SystemError,
8828+
"type %s has the Py_TPFLAGS_MANAGED_WEAKREF flag "
8829+
"but not Py_TPFLAGS_HAVE_GC flag",
8830+
type->tp_name);
8831+
return -1;
8832+
}
88198833
type->tp_weaklistoffset = MANAGED_WEAKREF_OFFSET;
88208834
}
88218835
return 0;

0 commit comments

Comments
 (0)