metaclass

Greg Chapman glchapman at earthlink.net
Fri Apr 12 23:24:52 EDT 2002


On Fri, 12 Apr 2002 12:45:21 -0500, "Mark McEahern" <mark at mceahern.com> wrote:

>I'm trying to translate the tracing metaclass provided here:
>
>  http://www.python.org/doc/essays/metaclasses/Trace.py
>
>to use the new __metaclass__ construct in Python 2.2, but I can't seem to
>get off the ground.
>
>For starters, suppose I simply want the metaclass to switch out one class
>with another.
>
>  # This should return an instance of Proxy instead of Switched.
>  # Eventually, Proxy will be able to trace calls to Switched
>  # before passing them on to Switched.
>  s = Switched()

I got a bit boggled looking at the Trace metaclass.  However, if I understand it
correctly, it may not be necessary in 2.2 to use a metaclass to trace all method
calls.   I believe the following class should work (though it will not log
syntactic operations which result in method calls -- e.g., getting an attribute
will not log calls to __getattribute__.  It also doesn't log calls to __init__).
Not tested (beyond the test routine in Trace.py):

class Traced(object):
    def __trace_call__(self, fp, fmt, *args):
        fp.write((fmt+'\n') % args)

    def __getattribute__(self, name):
        res = super(Traced, self).__getattribute__(name)
        if isinstance(res, types.MethodType):
            if self.__trace_output__ and (name != '__trace_call__'):
                fullname = self.__class__.__name__ + "." + name
                res = TracingWrapper(fullname, res, self)
        return res

class TracingWrapper(object):
    def __init__(self, name, func, inst):
        self.__name__ = name
        self.func = func
        self.inst = inst

    def __call__(self, *args, **kw):
        self.inst.__trace_call__(self.inst.__trace_output__,
                                 "calling %s, inst=%s, args=%s, kw=%s",
                                 self.__name__, self.inst, args, kw)
        try:
            rv = self.func(*args, **kw)
        except:
            t, v, tb = sys.exc_info()
            self.inst.__trace_call__(self.inst.__trace_output__,
                                     "returning from %s with exception %s: %s",
                                     self.__name__, t, v)
            raise t, v, tb
        else:
            self.inst.__trace_call__(self.inst.__trace_output__,
                                     "returning from %s with value %s",
                                     self.__name__, rv)
            return rv


class C(Traced):
    def __init__(self, x=0): self.x = x
    def m1(self, x): self.x = x
    def m2(self, y): return self.x + y
    __trace_output__ = sys.stdout

(etc.)
---
Greg Chapman



More information about the Python-list mailing list