[Python-bugs-list] [ python-Bugs-448352 ] returning stack frames scrambles globals
noreply@sourceforge.net
noreply@sourceforge.net
Mon, 06 Aug 2001 13:44:47 -0700
Bugs item #448352, was opened at 2001-08-06 01:27
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=448352&group_id=5470
Category: Python Interpreter Core
Group: Python 2.1.1
Status: Open
Resolution: None
Priority: 5
Submitted By: Andreas Just (andijust)
>Assigned to: Guido van Rossum (gvanrossum)
Summary: returning stack frames scrambles globals
Initial Comment:
When function return references to stack frame objects, various data in global or local namespaces
are scrambled.
I'm adding an example, where returning a stack frame prevents a classes "__del__" method from
being called. In addition, when assigning the returned stack frame lists to a global variable, I've got
the list of imported modules destroyed.
I'm aware, that this might not be a real bug - maybe it's a case of "this has to be this way", as
those objects are changed same time when they are used as return values. If it is, just drop this
bug.
Andreas
-------------------------------------------------
Program example:
(This short script will prevent the "__del__" method from being called, when stack() returns
references to the original frame objects and will work fine, when returning just some copies.)
#!/usr/bin/env python
import sys
import os
class FrameObjectCopy :
"""empty class for use in member function Logging.stack()"""
def stack(create_error) :
frame = sys._getframe().f_back
if create_error :
# returning the frame runs into error
return frame
else :
#
# returning a copy will work fine
frame_copy = FrameObjectCopy()
for attrname in dir(frame) :
setattr(frame_copy,attrname,getattr(frame,attrname))
return frame_copy
class test :
def __init__(self,create_error) :
print "***** init() *****"
s = stack(create_error)
def __del__(self) :
print "***** del() *****"
print
print "Running correctly - __init__() and __del__() methods are called"
a=test(0)
del a
print
print "Running into error - only __init__() is called"
a=test(1)
del a
----------------------------------------------------------------------
Comment By: Guido van Rossum (gvanrossum)
Date: 2001-08-06 05:41
Message:
Logged In: YES
user_id=6380
The first problem you mention is indeed "that's how it
works". The frame keeps references to the local variables;
if you make the frame a local variable of itself, you create
a circular reference. I don't believe frames are currently
garbage-collected; they will be in 2.2 I believe.
I don't understand the second problem you are describing;
you have probably managed to confused yourself, but if not,
please post a code fragment that reproduces the problem here
(and describe it better -- I'm not quite sure what you are
observing).
----------------------------------------------------------------------
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=448352&group_id=5470