473,326 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

cleanup after exceptions

Hi,

I'm a little confused why objects
are not deleted after they go
out of scope due to an exception?

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.

cheers,
Pádraig.

Jul 18 '05 #1
4 1351
On Thu, Dec 18, 2003 at 07:11:12PM +0000, Pa*****@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

Jul 18 '05 #2
Jp Calderone wrote:
On Thu, Dec 18, 2003 at 07:11:12PM +0000, Pa*****@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,


OK I can see that, but why doesn't a pass on the exception release it?
This is demonstrated with:

#!/usr/bin/env python

import time

class c:
def __del__(self):
print "del"

def f():
C=c()
exception=throw

try:
f()
except:
pass

time.sleep(3)

which itself holds a reference to all the
locales from that function.
don't know what you mean by this

Calling sys.exc_clear()
This isn't in version 2.2.2 at least
(possibly followed by gc.collect()) should force
the cleanup you expect.


thanks,
Pádraig.

Jul 18 '05 #3
Jp Calderone wrote:
On Thu, Dec 18, 2003 at 07:11:12PM +0000, Pa*****@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


OK I can see that, but why doesn't a pass on the exception release it?
This is demonstrated with:

#!/usr/bin/env python

import time

class c:
def __del__(self):
print "del"

def f():
C=c()
exception=throw

try:
f()
except:
pass

time.sleep(3)

which itself holds a reference to all the
locales from that function.
don't know what you mean by this

Calling sys.exc_clear()
This isn't in version 2.2.2 at least
(possibly followed by gc.collect()) should force
the cleanup you expect.


thanks,
Pádraig.

Jul 18 '05 #4
Jp Calderone wrote:
On Thu, Dec 18, 2003 at 07:11:12PM +0000, Pa*****@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.


I tried out your recipe, but with no luck:

Python 2.3.2 (#1, Oct 21 2003, 10:03:19)
[GCC 3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
class T: .... def __del__(self):
.... print "now i'm gone"
.... def f(): .... t = T()
.... raise Exception
.... import sys, gc
f() Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in f
Exception sys.exc_clear()
gc.collect() 0

The only way to clear the reference I've found so far is a bit unorthodox:
raise Exception now i'm gone
Traceback (most recent call last):
File "<stdin>", line 1, in ?
Exception

There seems to be some dark corner of the exception infrastructure that
exc_clear() doesn't touch.

However, I think it's about time to direct the OP to the solution of the
"real" problem, i. e. ensuring that a resource is released when an
exception occurs:
def g(): .... t = T()
.... try:
.... raise Exception
.... finally:
.... del t
.... g() now i'm gone
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in g
Exception


Whether immediate garbage collection occurs, is an implementation detail.
The code will be more portable if try...finally is used in such cases.

Peter
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: use dmgass at hotmail dot com | last post by:
I'm writing a module and when it is imported by a script I want some code automatically executed when the importing script is finished executing. I'd like it to execute before interactive mode is...
2
by: Mike N | last post by:
Coming from a C++ background, are destructors called for all local objects after a C# exception? C++: myproc() { CSingleLock protect(&m_myCritsec, FALSE); if (!protect.Lock()) {
2
by: Emil Astrom | last post by:
Hi! I wonder if there's a way to retrieve information about thrown, not yet handled exceptions. My situation is similar to the code below: class MySession : IDisposable { : : Dispose()
1
by: Jason S | last post by:
I haven't used try/catch/finally very much in Javascript. My function (let's call it try_it()) needs to call a function that could throw an exception (let's call it dangerous()) with some setup()...
69
by: MQ | last post by:
Hi all I am just wondering how most people implement cleanup in C functions. In particular, if the function opens a number of resources, these need to be released properly should an error occur...
5
by: Kenneth Porter | last post by:
I've read this article and have some followup questions. http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thr ead/9d5324ce02f4d89b/ I'm working on an embedded robotics...
3
by: Petr Pavlu | last post by:
Hello, I have two questions how the functions should be written. I read the FAQ but didn't find any answer. If there is any please point me out. I. Cleanup code Consider I have to open file1,...
6
by: Peter Michaux | last post by:
I just ran some circular memory leak tests in IE6, O9, S3, FF2 and it seems to me they all benefit from doing the same kind of circular memory leak cleanup that IE requires. My tests were very...
4
by: IanWright | last post by:
I've got a section of a program that I can't quite get to work. I'm fairly sure its something very simple/trivial but it looks correct to me, so if someone could help me fix the problem, and explain...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.