local variable not destructed if the function raised a exception

javalist javalist at 21cn.com
Mon Apr 9 23:44:34 EDT 2001


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.
>>> class c1:
...     def __init__(self):
...             print "constructed " + `id(self)`
...     def __del__(self):
...             print "destructed " + `id(self)`
... 
>>> def f():
...     c1i = c1()  
...     print "in f"
...     raise KeyError
... 
>>> f()
constructed 1605572
in f
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 4, in f
KeyError
>>> c1i
destructed 1605572
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: There is no variable named 'c1i'
>>> def f1():
...     c1i = c1()
...     print "in f"
... 
>>> 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,
 javalist                          mailto:javalist at 21cn.com






More information about the Python-list mailing list