[Python-Dev] PEP-419: Protecting cleanup statements from interruptions

Paul Colomiets paul at colomiets.name
Mon Apr 9 09:05:33 CEST 2012

Hi Benjamin,

On Mon, Apr 9, 2012 at 12:42 AM, Benjamin Peterson <benjamin at python.org> wrote:
> 2012/4/8 Paul Colomiets <paul at colomiets.name>:
>> Function 'sys.setcleanuphook'
>> -----------------------------
>> A new function for the ``sys`` module is proposed.  This function sets
>> a callback which is executed every time ``f_in_cleanup`` becomes
>> false.  Callbacks get a frame object as their sole argument, so that
>> they can figure out where they are called from.
> Calling a function every time you leave a finally block? Isn't that a
> bit expensive?

For signal handler it isn't, because you set it only when signal happens,
and remove it when it first happens (in the common case)

For yield-based coroutines, there is a similar overhead of trampoline
at each yield and each return, and exit from finally doesn't happen more
often than return.

For both greenlets and yield-based coroutines it is intented to be used
for exceptional situation (when timeout happens *and* coroutine
currently in finally block), so can be turned off when unneeded
(and even turned on only for this specific coroutine).

When hook is not set it's only checking of single pointer for NULL
value at each exit from finally. This overhead should be negligible.


More information about the Python-Dev mailing list