Re: Destructors and exceptions

From: Humpty Dumpty (oliver.schoenborn_at_utoronto.ca)
Date: 06/08/04


Date: Mon, 7 Jun 2004 23:34:08 -0400


"David Turner" <dkturner@telkomsa.net> wrote in message
news:e251b7ba.0406070651.1c98c09d@posting.google.com...
>

> 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.

There is no Python equivalent of C++'s "destructor garanteed to be called
upon scope exit", for a couple of reasons: scope exit only destroys
references to objects, not the objects themselves; destruction of objects is
left to the garbage collector and you have no influence on it. In
particular, the gc is not required to release resources, so finalizers (the
__del__ method, closest to C++'s destructor) may not get called. This means
__del__ is pretty much useless (AFAIMC), and you can't rely on them being
called before program exit (or ever, for that matter).

I agree, it is a real pitty that Python doesn't have a way of doing what you
mention, other than try-finally, which makes code more difficult to read. A
new specifier, e.g. "scoped", would be a required addtion to Python:
interpreter would garantee that __del__ of scoped objects would be called on
scope exit, and raise an exception if attempt to alias.

Oliver



Relevant Pages

  • Re: Executable PE Import table missing ExitProcess?
    ... exe Import list for kernel32.dll? ... And if it's not in the import table, then how the heck does the program exit? ... Your question has been addressed elsewhere, but as an aside: a process does not need to import ExitProcess() to be able to exit cleanly. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Destroying modal Dialog from outside the Dialog class
    ... This looks like a general design flaw to me. ... program exit. ... Prev by Date: ...
    (microsoft.public.vc.mfc)
  • Re: becoming a daemon
    ... what is the reason to create child and then ... so that the parent won't get SIGCLDs anymore. ... _exit is the system call; exitis the libc wrapper that also ...
    (comp.unix.programmer)
  • Re: exit options in Debug mode
    ... completely when my program encounters the exit() command (as opposed to ... staying open until user press a key). ... The reason I'm doing this instead of just building in Release mode is ...
    (microsoft.public.vc.language)
  • Re: OleDbDataAdapter appears to use incorrect path to DB
    ... "Scott M." wrote in ... The reason i do this is so I exit the function and I ... >> return Nothing because this will signify that the function failed. ...
    (microsoft.public.dotnet.framework.adonet)