summaryrefslogtreecommitdiffstats
path: root/Doc/c-api
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-05-29 03:21:26 (GMT)
committerGitHub <noreply@github.com>2021-05-29 03:21:26 (GMT)
commit11049bece15b6fa08fa6d74aea8eb6bc2ebadfe5 (patch)
treed3057cd8a0904a5b7b5e37a8bcfa5cb605e32ebb /Doc/c-api
parent1757ddd227d2aaee592162812cf735b1ef400622 (diff)
downloadcpython-11049bece15b6fa08fa6d74aea8eb6bc2ebadfe5.zip
cpython-11049bece15b6fa08fa6d74aea8eb6bc2ebadfe5.tar.gz
cpython-11049bece15b6fa08fa6d74aea8eb6bc2ebadfe5.tar.bz2
bpo-44263: Better explain the GC contract for PyType_FromSpecWithBases (GH-26442) (GH-26443)
(cherry picked from commit 8b55bc3f93a655bc803bff79725d5fe3f124e2f0) Co-authored-by: Pablo Galindo <Pablogsal@gmail.com> Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Diffstat (limited to 'Doc/c-api')
-rw-r--r--Doc/c-api/gcsupport.rst11
-rw-r--r--Doc/c-api/type.rst7
2 files changed, 18 insertions, 0 deletions
diff --git a/Doc/c-api/gcsupport.rst b/Doc/c-api/gcsupport.rst
index 55ed9d4..338365b 100644
--- a/Doc/c-api/gcsupport.rst
+++ b/Doc/c-api/gcsupport.rst
@@ -33,6 +33,17 @@ Constructors for container types must conform to two rules:
#. Once all the fields which may contain references to other containers are
initialized, it must call :c:func:`PyObject_GC_Track`.
+ .. warning::
+ If a type adds the Py_TPFLAGS_HAVE_GC, then it *must* implement at least
+ a :c:member:`~PyTypeObject.tp_traverse` handler or explicitly use one
+ from its subclass or subclasses.
+
+ Some APIs like :c:func:`PyType_FromSpecWithBases` or
+ :c:func:`PyType_FromSpec` will automatically populate the
+ :c:member:`~PyTypeObject.tp_flags`, :c:member:`~PyTypeObject.tp_traverse`
+ and :c:member:`~PyTypeObject.tp_clear` fields if the type inherits from a
+ class that implements the garbage collector protocol and the child class
+ does *not* include the :const:`Py_TPFLAGS_HAVE_GC` flag.
.. c:function:: TYPE* PyObject_GC_New(TYPE, PyTypeObject *type)
diff --git a/Doc/c-api/type.rst b/Doc/c-api/type.rst
index bdb636d..9d24f39 100644
--- a/Doc/c-api/type.rst
+++ b/Doc/c-api/type.rst
@@ -169,6 +169,13 @@ The following functions and structs are used to create
The associated module is not inherited by subclasses; it must be specified
for each class individually.
+ If some of the bases in *bases* implements the GC protocol and the type being
+ created does not include the :const:`Py_TPFLAGS_HAVE_GC` in the flags included in
+ *spec*, then the GC protocol will be automatically implemented from its parents. On
+ the contrary, if the type being created does include :const:`Py_TPFLAGS_HAVE_GC` in
+ its flags then it *must* implement the GC protocol itself by at least including a slot
+ for :c:member:`~PyTypeObject.tp_traverse` in *spec*.
+
This function calls :c:func:`PyType_Ready` on the new type.
.. versionadded:: 3.9