cleanup after exceptions
Jp Calderone
exarkun at intarweb.us
Thu Dec 18 14:43:45 EST 2003
On Thu, Dec 18, 2003 at 07:11:12PM +0000, Padraig at Linux.ie wrote:
> Hi,
>
> I'm a little confused why objects
> are not deleted after they go
> out of scope due to an exception?
Because objects don't go out of scope. Only variables do. Objects remain
"alive" as long as there are any references to them.
>
> For e.g.
>
> >>> import time
> >>>
> >>> def f():
> >>> myfile=open("file.test","w")
> >>> myfile.write("not flushed\n")
> >>> exception=throw
> >>>
> >>> f()
> >>> time.sleep(10)
>
>
> The file is not written/closed until
> the python interpreter exits.
> The same thing applies to other objects.
In this case, the traceback still holds a reference to the frame from
which the exception was raised, which itself holds a reference to all the
locales from that function.
Calling sys.exc_clear() (possibly followed by gc.collect()) should force
the cleanup you expect.
Jp
More information about the Python-list
mailing list