[Python-ideas] Better support for finalization with weakrefs

Antoine Pitrou solipsis at pitrou.net
Mon Jul 30 19:27:34 CEST 2012


Hi,

On Mon, 30 Jul 2012 17:44:09 +0100
Richard Oudkerk <shibturn at gmail.com>
wrote:
> 
> 2. Callbacks invoked during interpreter shutdown are troublesome.  For
>     instance they can throw exceptions because module globals have been
>     replaced by None.

A solution was proposed a long time ago at
http://bugs.python.org/issue812369

> 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?)

No, it's just because PyErr_WriteUnraisable() doesn't print the
traceback. We could just fix that.

> (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!

Looks good!

Regards

Antoine.


-- 
Software development and contracting: http://pro.pitrou.net





More information about the Python-ideas mailing list