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 ryan.a.heisler
Recipients docs@python, ryan.a.heisler
Date 2021-01-17.04:12:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1610856738.09.0.530478209911.issue42945@roundup.psfhosted.org>
In-reply-to
Content
In the documentation for `weakref.finalize` (https://docs.python.org/3.9/library/weakref.html#weakref.finalize), it says:

"Note It is important to ensure that func, args and kwargs do not own any references to obj, either directly or indirectly, since otherwise obj will never be garbage collected. In particular, func should not be a bound method of obj."

However, at the bottom of the document, in the section called "Comparing finalizers with __del__() methods" (https://docs.python.org/3.8/library/weakref.html#comparing-finalizers-with-del-methods), the following code is part of an example of how to use `weakref.finalize`:

class TempDir:
    def __init__(self):
        self.name = tempfile.mkdtemp()
        self._finalizer = weakref.finalize(self, shutil.rmtree, self.name)

I believe this code violates the rule that func, args, and kwargs should not have a reference to obj. In the example, obj is the instance of TempDir, and one of the arguments to finalize's callback is an attribute of the same instance of TempDir.

I do not know how to fix this example code. I found it while trying to figure out how to use `weakref.finalize`.
History
Date User Action Args
2021-01-17 04:12:18ryan.a.heislersetrecipients: + ryan.a.heisler, docs@python
2021-01-17 04:12:18ryan.a.heislersetmessageid: <1610856738.09.0.530478209911.issue42945@roundup.psfhosted.org>
2021-01-17 04:12:18ryan.a.heislerlinkissue42945 messages
2021-01-17 04:12:17ryan.a.heislercreate