[Python-ideas] Better support for finalization with weakrefs

Richard Oudkerk shibturn at gmail.com
Wed Aug 1 18:27:23 CEST 2012




On 01/08/2012 4:41pm, Lefavor, Matthew (GSFC-582.0)[MICROTEL LLC] wrote:
> 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

I have a patch (with tests and docs) at

     http://bugs.python.org/issue15528

It has a pop() method for unregistering the callback.
So if f was created using

     f = finalize(obj, func, *args, **kwds)

then f.pop() returns a tuple

     (wr, func, args, kwds)

where wr is a weakref to obj.

There is also a get() method which returns the same info, but does
not unregister the callback.

Once the finalizer is dead, f(), f.get(), f.pop() all return None.

Richard





More information about the Python-ideas mailing list