local variable not destructed if the function raised a exception 
Author Message
 local variable not destructed if the function raised a exception

Hello python-list,

        following is what I did

Python 2.0 (#2, Mar 12 2001, 21:40:11)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "copyright", "credits" or "license" for more information.

Quote:
>>> class c1:

...     def __init__(self):
...             print "constructed " + `id(self)`
...     def __del__(self):
...             print "destructed " + `id(self)`
...
Quote:
>>> def f():

...     c1i = c1()  
...     print "in f"
...     raise KeyError
...
Quote:
>>> f()

constructed 1605572
in f
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 4, in f
KeyError
Quote:
>>> c1i

destructed 1605572
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: There is no variable named 'c1i'
Quote:
>>> def f1():

...     c1i = c1()
...     print "in f"
...
Quote:
>>> f1()

constructed 1605572
in f
destructed 1605572

        in f(),c1i is constructed correctly but after raise the exception(any exception),python
        didn't call c1i's destructor,in f1(),the behavior is just normal,destructor is called after
        f1() returns.anyone tell me why this happen,is this a bug or just a feature?

Best regards,



Sat, 27 Sep 2003 11:44:34 GMT  
 local variable not destructed if the function raised a exception

Quote:

> in f(),c1i is constructed correctly but after raise the exception(any exception),python
> didn't call c1i's destructor,in f1(),the behavior is just normal,destructor is called after
> f1() returns.anyone tell me why this happen,is this a bug or just a feature?

feature.  the traceback object (sys.exc_traceback) contains a reference to
the execution frame, which contains references to all local variables.  under
normal use, the traceback is destroyed (and all locals released) on the next
return or raise.

for more info, see sections 7.4 and 3.2 in the language reference.

Cheers /F



Sat, 27 Sep 2003 14:46:22 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Whether to raise exception or End_of_List function

2. Numerical exceptions not raised properly?

3. Raising an exception from a multithreaded embedded Python interpreter trace function

4. C built and raised exceptions are not being catched in Python code

5. local variable and local variable in block behave differently

6. writing to locals (was RE: execfile: NameError exception thrown for things in locals())

7. openMP: threadprivate not effected and function variables not passed

8. how do i destruct a variable

9. local variable not found

10. compiler option for local variables to default to automatic not working

11. local variables in functions

12. calling function recursively, local variables?

 

 
Powered by phpBB® Forum Software