pythonreflectionscoping

Is it possible to see variable scope of just called function in Python?


I have rather strange question about function variable scope and return. Is there any way to inspect the scope of called function in the caller after called function returns value?

Use case is simple: in Flask views I would like to bypass locals() to my templates. I can define what template I need by convention and having return a dictionary in every view bothers me.


Solution

  • After the function returns, its scope no longer exists. This makes it problematic to get the scope after the function returns.

    However, using Python's trace or profile capability, it's possible to run some code just as a function is about to return, and extract the locals from its stack frame at that time. These can then be squirreled away somewhere and returned along with (or instead of) the return value of the called function using a wrapper function.

    Here is a decorator that can be used for this nefarious purpose. Keep in mind that this implementation is a horrible hack and it would be bad style to use it for mere convenience. I could probably think of legitimate uses... give me a few days. Also, it may not work with non-CPython implementations (probably won't, in fact).

    import sys, functools
    
    def givelocals(func):
        
        localsdict = {}
    
        def profilefunc(frame, event, arg):
            if event == "call":
                localsdict.clear()
            elif event == "return":
                localsdict.update(frame.f_locals)
            return profilefunc    
    
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            oldprofilefunc = sys.getprofile()
            sys.setprofile(profilefunc)
            try:
                return func(*args, **kwargs), dict(localsdict)
            except Exception as e:
                e.locals = dict(localsdict)
                raise
            finally:
                sys.setprofile(oldprofilefunc)
    
        return wrapper
    

    Example:

    @givelocals
    def foo(x, y):
        a = x + y
        return x * y
    
    >>> foo(3, 4)
    (12, {'y': 4, 'x': 3, 'a': 7})
    

    If you have some arbitrary function you want to use it with that you can't decorate because it's in a module you didn't write, you can create the wrapper on the fly and call it:

    def foo(x, y):
        a = x + y
        return x * y
    
    >>> givelocals(foo)(3, 4)
    (12, {'y': 4, 'x': 3, 'a': 7})
    

    Or store the wrapper and call it later:

    locals_foo = givelocals(foo)
    >>> locals_foo(3, 4)
    (12, {'y': 4, 'x': 3, 'a': 7})
    

    The wrapper returns a tuple of the actual return value and the locals dictionary. If an exception is raised, the .locals attribute of the exception object is set to the locals dict.

    Keep in mind that usually Python frees the memory used by the local variables when the function returns. Retaining these values will cause your program to use more memory (for as long as there are references to them), so it'd be a good idea to clean them up when you no longer need them.

    One last note: I'm using Python's profile functionality here because it's invoked only at function calls and returns. If you're using a Python version prior to 2.6, you don't have profiling, so you'd need to use tracing instead. The profile function will also work as a trace function as written, you'd just need to use gettrace() and settrace() rather than the corresponding profile-related functions. However, since tracing is called for each line, the wrapped function may be noticeably slower.