Bug in Python 1.5.2 exception handling?

Dave Cole djc at itga.com.au
Tue Dec 14 19:38:52 EST 1999


It looks like function locals are not deleted if that function is
terminated by an exception.  They seem to hang around until they are
unref'ed when the function is next called.

Consider the following program:

- bogus.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class c:
    def __init__(self, val):
        self.val = val
    def __del__(self):
        print val, 'deleted'

def check_return(val):
    print 'return:',
    o = c(val)

def check_raise(val):
    print 'raise:',
    o = c(val)
    raise ValueError

for val in range(3):
    check_return(val)
for val in range(3):
    try:
        check_raise(val)
    except:
        pass
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This is what I get when I run it:

heresy:~% python bogus.py 
return: 0 deleted
return: 1 deleted
return: 2 deleted
raise: raise: 1 deleted
raise: 2 deleted
2 deleted
heresy:~% 

- Dave



More information about the Python-list mailing list