[New-bugs-announce] [issue5641] Local variables not freed when Exception raises in function called from cycle

Glin report at bugs.python.org
Wed Apr 1 10:49:38 CEST 2009


New submission from Glin <glin at seznam.cz>:

Situation:
You have a while cycle. inside of it a try-except block and in this
try-except block calling a function. Inside of this function raises an
exception, with is caught in the try-except block.

What happens:
Local variables of the function are not freed. (OK, they are freed when
the program leaves a function inside of which is the while cycle, or
when another exception raises and is caught, but it's not helpful when
you have some server program based on infinite while loop or working
with a large amount of data).

Example:
Example program is attached.

Output of the example program:

While start
job() start
Creating 1
Catched AttributeError
While end
While start
job() start
Creating 2
job() end
Deleting 2
While end
While start
job() start
Creating 3
job() end
Deleting 3
While end
...

As you can see, a variable 'a' created in the first call (which throws
an exception) of the 'job' function will never get freed. 

Imagine that in 'job' function you create a large amount of data, or
worse you create a database connection, which will be opened forever.

Tested on Python 2.5.2 and 2.7(svn). On the contrary, Python 3.0 does
not have this issue (it frees variables of the 'job' function at the end
of except: block.)

As Python 2.X will be another long time with us, I think it should be
fixed to behave correctly (that is as in Python 3.)

----------
components: Interpreter Core
files: notfreed.py
messages: 84989
nosy: Glin
severity: normal
status: open
title: Local variables not freed when Exception raises in function called from cycle
type: resource usage
versions: Python 2.4, Python 2.5, Python 2.6, Python 2.7
Added file: http://bugs.python.org/file13535/notfreed.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5641>
_______________________________________


More information about the New-bugs-announce mailing list