Re: Destructors and exceptions

From: Terry Reedy (tjreedy_at_udel.edu)
Date: 06/07/04


Date: Mon, 7 Jun 2004 11:26:44 -0400
To: python-list@python.org


"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?

When the last reference to an object disappears, the interpreter *may* but
is *not required* to forget or destruct the object, if indeed the concept
is meaningful. What happens is implementation dependent. CPython usually
cleans up immediately, which is sooner than with Jython. I am not sure
what human interpreters do. Write once, never erase storage of objects
(keeping a complete audit trail of objects created) would also be legal.

If you want to force the issue, give your class a close method, as with
file objects, and call it explicitly. Then code in try:finally:, which is
designed for this purpose.
try:
   process
finally:
   cleanup

Terry J. Reedy