[Python-bugs-list] [ python-Bugs-660707 ] Problems with reference counting and exec

noreply@sourceforge.net noreply@sourceforge.net
Fri, 03 Jan 2003 03:37:59 -0800


Bugs item #660707, was opened at 2003-01-01 16:26
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=660707&group_id=5470

Category: Python Interpreter Core
Group: Python 2.2
>Status: Closed
Resolution: None
Priority: 5
Submitted By: Daniel (kamek)
Assigned to: Nobody/Anonymous (nobody)
Summary: Problems with reference counting and exec

Initial Comment:
When using exec to parse an arbitrary string, Python 
seems to have some problems on keeping the reference 
count. The sample below shows a problem when the 
specified string is not a valid Python expression, and 
would be better for testing in the Python interpreter (~ 
preceding lines represents indentation):

#BEGIN

clas MyClass:
~def __del__(self):
~~print 'Deleting object'

def f():
~# create a new instance
~obj = MyClass()
~# now, raise an exception
~exec "any invalid line"

f()

#END

We get an exception, but in no moment we see the 
destructor being called. The most curious, then, is that if 
we raise another exception from the interpreter (just type 
anything), we get the destructor right before the 
exception information.

There's also a reference count problem that happens 
even when the line for exec is valid, but I couldn't 
manage to reproduce it outside of my project yet.

----------------------------------------------------------------------

>Comment By: Just van Rossum (jvr)
Date: 2003-01-03 12:37

Message:
Logged In: YES 
user_id=92689

This is expected behavior: an exception causes a traceback,
and the traceback object will hold a reference to the frame,
which holds a reference to the local variables. So as long
as the traceback object lives your __del__ will not be
called. It has nothing to do with exec per se.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=660707&group_id=5470