[Python-Dev] Stack frames

Jack Jansen Jack.Jansen@cwi.nl
Fri, 1 Aug 2003 16:23:08 +0200


Everything you use appears to be documented in the Python Language  
Reference,
("Frame Objects" in "Internal Types"), and the heading of that section  
explicitly
states that these may change in future versions of the interpreter. So,  
while
you are theoretically not safe in practice you are probably reasonably  
safe.
sys.exc_traceback is also still there, even though sys.exc_info() was  
added
5 years ago as the preferred interface.

On Friday, Aug 1, 2003, at 15:06 Europe/Amsterdam, M.-A. Lemburg wrote:

> I'm currently developing an application that needs to execute
> code from a library in a way that gives each call into that
> system access to a set of call-specific variables.
>
> The problem is that I can't change the library all that much,
> e.g. add some context parameter to all calls, so I have to use
> some other technique for accessing the call-specific variables.
>
> I've come up with a trick that uses Python call stacks which
> works quite well. My only concern is whether this technique will
> continue to work in future versions of Python. Here's the
> helper I'm using:
>
> import sys
>
> def acquire_from_call_stack(varname):
>
>     # Start from the caller of the function calling this helper
>     frame = sys._getframe(2)
>     while frame is not None:
>         if frame.f_locals.has_key(varname):
>             value = frame.f_locals[varname]
>             frame = None
>             return value
>         frame = frame.f_back
>     raise AttributeError, varname
>
> Any idea as to what might happen to stack frames in the
> future ?
>
> --  
> Marc-Andre Lemburg
> eGenix.com
>
> Professional Python Software directly from the Source  (#1, Aug 01  
> 2003)
> >>> Python/Zope Products & Consulting ...          
> http://www.egenix.com/
> >>> mxODBC, mxDateTime, mxTextTools ...         
> http://python.egenix.com/
> _______________________________________________________________________ 
> _
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev@python.org
> http://mail.python.org/mailman/listinfo/python-dev
>
--
Jack Jansen, <Jack.Jansen@cwi.nl>, http://www.cwi.nl/~jack
If I can't dance I don't want to be part of your revolution -- Emma  
Goldman