Re: Destructors and exceptions

From: Christos (tzot_at_sil-tec.gr)
Date: 06/08/04


Date: Tue, 08 Jun 2004 12:19:15 +0300

On 7 Jun 2004 07:51:23 -0700, rumours say that dkturner@telkomsa.net
(David Turner) might have written:

>Hi all
>
>I noticed something interesting while testing some RAII concepts
>ported from C++ in Python. I haven't managed to find any information
>about it on the web, hence this post.
>
>The problem is that when an exception is raised, the destruction of
>locals appears to be deferred to program exit. Am I missing
>something? Is this behaviour by design? If so, is there any reason
>for it? The only rationale I can think of is to speed up exception
>handling; but as this approach breaks many safe programming idioms, I
>see it as a poor trade.

[snip code]

ISTR that when an exception is raised, there is a reference to the code
frame (and therefore to its locals) kept somewhere; you can access this
info through the sys.exc_info call. Have you tried running
sys.exc_clear after the exception was raised?

Apart from that, advice from others not to rely on finalisation of
objects to clean up still applies.

-- 
TZOTZIOY, I speak England very best,
"I have a cunning plan, m'lord" --Sean Bean as Odysseus/Ulysses


Relevant Pages