diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2020-03-03 02:50:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-03 02:50:40 (GMT) |
commit | 6df421fe87a9418d6c59f89dbc5d5573b6826855 (patch) | |
tree | 4c1cf1bc83f6d9fbdf8deb608866dbbb1bdd6284 | |
parent | 4991cf47c487500fdedc34f0a2eb4b7468a67c3c (diff) | |
download | cpython-6df421fe87a9418d6c59f89dbc5d5573b6826855.zip cpython-6df421fe87a9418d6c59f89dbc5d5573b6826855.tar.gz cpython-6df421fe87a9418d6c59f89dbc5d5573b6826855.tar.bz2 |
bpo-39778: Add clarification about tp_traverse and ownership (GH-18754)
Automerge-Triggered-By: @pablogsal
-rw-r--r-- | Doc/c-api/typeobj.rst | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst index ff0e70e..ce4e8c9 100644 --- a/Doc/c-api/typeobj.rst +++ b/Doc/c-api/typeobj.rst @@ -1192,7 +1192,7 @@ and :c:type:`PyType_Type` effectively act as defaults.) 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 @@ -1212,6 +1212,18 @@ and :c:type:`PyType_Type` effectively act as defaults.) 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. |