Welcome, guest | Sign In | My Account | Store | Cart
class persistent_locals2(object):
    def __init__(self, func):
        self._locals = {}
        self.func = func

    def __call__(self, *args, **kwargs):
        def tracer(frame, event, arg):
            if event=='return':
                self._locals = frame.f_locals.copy()

        # tracer is activated on next call, return or exception
        sys.setprofile(tracer)
        try:
            # trace the function call
            res = self.func(*args, **kwargs)
        finally:
            # disable tracer and replace with old one
            sys.setprofile(None)
        return res

    def clear_locals(self):
        self._locals = {}

    @property
    def locals(self):
        return self._locals

Diff to Previous Revision

--- revision 2 2010-07-07 16:48:43
+++ revision 3 2010-07-07 16:53:35
@@ -1,4 +1,3 @@
-# persistent_locals2 has been co-authored with Andrea Maffezzoli
 class persistent_locals2(object):
     def __init__(self, func):
         self._locals = {}

History