This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author pablogsal
Recipients eric.snow, leezu, pablogsal, pitrou, rhettinger, tim.peters, vstinner
Date 2020-03-02.15:54:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1583164469.32.0.49705102404.issue39778@roundup.psfhosted.org>
In-reply-to
Content
I concur with Antoine and Tim. The GC already has the machinery to deal with weak references in the correct way (even more after recent bugfixes regarding callbacks). Traversing the weak reference list in incorrect because the object does not "own" the weak references to it, as the weak references can die even if the object is alive. Also, as Tim mentions, the traverse will be called on the head of the list, in the same way if you do object.__weakref__ you will only get the HEAD of the list:

>>> import weakref
>>> class A: ...
>>> a = A()
>>> w1 = weakref.ref(a)
>>> w2 = weakref.ref(a, lambda *args: None) # Use a callback to avoid re-using the original weakref
>>> id(w1)
4328697104
>>> id(w2)
4328758864
>>> id(a.__weakref__)
4328697104

I think that this is not very well documented, as there is no pointers on what should and should not be traversed in https://docs.python.org/3.8/c-api/typeobj.html#c.PyTypeObject.tp_traverse.

I will prepare a PR to the documentation if everybody agrees and another one removing the traverse unless someone sees that something else is at play.
History
Date User Action Args
2020-03-02 15:54:29pablogsalsetrecipients: + pablogsal, tim.peters, rhettinger, pitrou, vstinner, eric.snow, leezu
2020-03-02 15:54:29pablogsalsetmessageid: <1583164469.32.0.49705102404.issue39778@roundup.psfhosted.org>
2020-03-02 15:54:29pablogsallinkissue39778 messages
2020-03-02 15:54:29pablogsalcreate