[Python-ideas] Better support for finalization with weakrefs

Lefavor, Matthew (GSFC-582.0)[MICROTEL LLC] matthew.lefavor at nasa.gov
Wed Aug 1 17:41:15 CEST 2012


If finalizations are objects, is there any reason you can't make the finalization object have an unregister method, like below? Or does this not address the problem?

    >>> class Kenny: pass
    ...
    >>> kenny = Kenny()
    >>> f = finalize(kenny, print, "you killed kenny!")
    >>> f
    <finalize.finalize object at 0xffed4f2c>
    >>> f.unregister()
    >>> del kenny
    >>>

Matthew Lefavor

From: Brett Cannon <brett at python.org<mailto:brett at python.org>>
Date: Wednesday, August 1, 2012 11:25 AM
To: "ironfroggy at gmail.com<mailto:ironfroggy at gmail.com>" <ironfroggy at gmail.com<mailto:ironfroggy at gmail.com>>
Cc: Richard Oudkerk <shibturn at gmail.com<mailto:shibturn at gmail.com>>, "python-ideas at python.org<mailto:python-ideas at python.org>" <python-ideas at python.org<mailto:python-ideas at python.org>>
Subject: Re: [Python-ideas] Better support for finalization with weakrefs



On Tue, Jul 31, 2012 at 9:28 AM, Calvin Spealman <ironfroggy at gmail.com<mailto:ironfroggy at gmail.com>> wrote:
On Mon, Jul 30, 2012 at 12:44 PM, Richard Oudkerk <shibturn at gmail.com<mailto:shibturn at gmail.com>> wrote:
> I would like to see better support for the use of weakrefs callbacks
> for object finalization.
>
> The current issues with weakref callbacks include:
>
> 1. They are rather low level, and working out how to use them
>    correctly requires a bit of head scratching.  One must find
>    somewhere to store the weakref till after the referent is dead, and
>    without accidentally keeping the referent alive.  Then one must
>    ensure that the callback frees the weakref (without leaving any
>    remnant ref-cycles).
>
>    When it is an option, using a __del__ method is far less hassle.
>
> 2. Callbacks invoked during interpreter shutdown are troublesome.  For
>    instance they can throw exceptions because module globals have been
>    replaced by None.
>
> 3. Sometimes you want the callback to be called at program exit even
>    if the referent is still alive.
>
> 4. Exceptions thrown in callbacks do not produce a traceback.  This
>    can make debugging rather awkward.  (Maybe this is because printing
>    tracebacks is problematic during interpreter shutdown?)
>
> (Note that problems 2-4 also apply to using __del__ methods.)
>
> If possible I would like to see the weakref module provide a finalize
> class to address these issues.  Trivial usage might be
>
>     >>> class Kenny: pass
>     ...
>     >>> kenny = Kenny()
>     >>> finalize(kenny, print, "you killed kenny!")
>     <finalize.finalize object at 0xffed4f2c>
>     >>> del kenny
>     you killed kenny!
>
> Prototype at https://gist.github.com/3208245

I like how simple it is, but it might be too simple. That is,
shouldn't we have a way to unregister the callback?

You can't unregister a __del__ method, so why should that stop this from moving forward? And if you really need that then you just have the cleanup code check something to see if it should run or not or just use some form of delegation.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120801/6919c2fa/attachment.html>


More information about the Python-ideas mailing list