[Python-ideas] Protecting finally clauses of interruptions

Paul Colomiets paul at colomiets.name
Mon Apr 2 21:43:52 CEST 2012


I'd like to propose a way to protect `finally` clauses from
interruptions (either by KeyboardInterrupt or by timeout, or any other

I think frame may be extended to have `f_in_finally` attribute (or
pick a better name). Internally it should probably be implemented as a
counter of nested finally clauses, but interface should probably
expose only boolean attribute. For `__exit__` method some flag in
`co_flags` should be introduced, which says that for whole function
`f_in_finally` should be true.

Having this attribute you can then inspect stack and check whether
it's safe to interrupt it or not. Coroutine library which interrupts
by timeout, can then sleep a bit and try again (probably for finite
number of retries). For signal handler there are also several options
to wait when thread escapes finally clause: use another thread, use
alert signal, use sys.settrace, or exit only inside main loop.

To be clear: I do not propose to change default SIGINT behavior, only
to implement a frame flag, and give library developers experiment with
the rest.


More information about the Python-ideas mailing list