]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39778: Add clarification about tp_traverse and ownership (GH-18754)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 3 Mar 2020 03:03:56 +0000 (19:03 -0800)
committerGitHub <noreply@github.com>
Tue, 3 Mar 2020 03:03:56 +0000 (19:03 -0800)
Automerge-Triggered-By: @pablogsal
(cherry picked from commit 6df421fe87a9418d6c59f89dbc5d5573b6826855)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Doc/c-api/typeobj.rst

index 71b45a6e7158fa4cb37fdd3c2e7c226c74ccc51b..057187e4ba84604e3a7471a75d3054bbd0c5b079 100644 (file)
@@ -496,7 +496,7 @@ type objects) *must* have the :attr:`ob_size` field.
    The :c:member:`~PyTypeObject.tp_traverse` pointer is used by the garbage collector to detect
    reference cycles. A typical implementation of a :c:member:`~PyTypeObject.tp_traverse` function
    simply calls :c:func:`Py_VISIT` on each of the instance's members that are Python
-   objects For example, this is function :c:func:`local_traverse` from the
+   objects that the instance owns. For example, this is function :c:func:`local_traverse` from the
    :mod:`_thread` extension module::
 
       static int
@@ -516,6 +516,18 @@ type objects) *must* have the :attr:`ob_size` field.
    debugging aid you may want to visit it anyway just so the :mod:`gc` module's
    :func:`~gc.get_referents` function will include it.
 
+   .. warning::
+       When implementing :c:member:`~PyTypeObject.tp_traverse`, only the members
+       that the instance *owns* (by having strong references to them) must be
+       visited. For instance, if an object supports weak references via the
+       :c:member:`~PyTypeObject.tp_weaklist` slot, the pointer supporting
+       the linked list (what *tp_weaklist* points to) must **not** be
+       visited as the instance does not directly own the weak references to itself
+       (the weakreference list is there to support the weak reference machinery,
+       but the instance has no strong reference to the elements inside it, as they
+       are allowed to be removed even if the instance is still alive).
+
+
    Note that :c:func:`Py_VISIT` requires the *visit* and *arg* parameters to
    :c:func:`local_traverse` to have these specific names; don't name them just
    anything.