Caller's frame, was: Re: Q: Interactive input

Fredrik Lundh fredrik at effbot.org
Tue Nov 7 14:31:41 EST 2000


Jan Kybic wrote:
> OK, thanks. But how do I make a function out of it? Imagine I want to
> replace the five lines above with a call to one function, keyboard().
> In this function, how do I retrieve the caller's frame, to pass it to
> exec, so that it has access to the variables at the level where
> keyboard() was called from? I looked in the documentation and could
> not find anything, so I would be grateful for any pointers.

throw an exception and examine the traceback stack (either
directly or via traceback.extract_tb()).

btw, the easiest way to accept commands is to use the stuff
in the code module:

def keyboard():
    import code, sys
    try:
        raise None
    except:
        frame = sys.exc_info()[2].tb_frame.f_back
    code.interact(local=frame.f_locals)

</F>





More information about the Python-list mailing list