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

  • Re: Easy questions from a python beginner
    ... with guaranteed effect (either unassigned exception ... function locals the same way as globals, through a namespace dictionary -- in ... "When a name is not found at all, a NameError exception is raised. ... If the name refers to a local variable that has not been bound, a UnboundLocalError exception is raised. ...
    (comp.lang.python)
  • Re: Is it possible to throw an exception into parent thread?
    ... Is it possible to get the exception back to the UI thread is the ... idly waiting for a windows message, it may be deep down in the middle ... of a loop, it could be anywhere. ...
    (microsoft.public.dotnet.framework)
  • Re: Issue implementing Runtime.exec() with StreamGobbler
    ... stderr = errorGobbler.getResult; ... the entire loop. ... call to the executer must either return or throw an exception. ...
    (comp.lang.java.programmer)
  • Re: PEP 3107 and stronger typing (note: probably a newbie question)
    ... going to write thick boilerplate to log the values of your locals and ... which will give you a stack trace with the values of the locals in ... The only reason to trap an exception is either to redirect the ...
    (comp.lang.python)
  • Re: Getting output from embedded python program
    ... but I think your problem is related to the globals and ... locals args to PyRun_String. ... might want to check for the exception value which could give you more ... Kim wrote: ...
    (comp.lang.python)