Instrumenting a class to see how it is used
I'm trying to reverse-engineer some pretty complex code. One thing that would help me a lot is if I could instrument a key class, so that I'd get a report of when and how each method was called and any properties or attributes were accessed during a typical run. I could do this relatively easily by just adding print calls to each method/attribute, but I was wondering - is there a library that does this sort of wrapping already? I tried writing my own but got bogged down in infinite recursion in _getattribute__ and couldn't see an easy way out. If anyone has any suggestions, I'd be interested. Thanks, Paul. -- http://mail.python.org/mailman/listinfo/python-list
Re: Instrumenting a class to see how it is used
This may get you started (warning: not really tested). $ echo instr.py from warnings import warn oget = object.__getattribute__ tget = type.__getattribute__ class Instr(object): class __metaclass__(type): def __getattribute__(cls, name): clsname = tget(cls, '__name__') warn('accessing %s.%s' % (clsname, name), stacklevel=2) return tget(cls, name) def __getattribute__(self, name): warn('accessing %s.%s' % (self, name), stacklevel=2) return oget(self, name) Then change the base classes of the class you want to instrument, i.e. class MyClass(MyBase) - class MyClass(MyBase, Instr) and see what happens. It should work in simple cases. It will log a lot, so will have to adapt this. -- http://mail.python.org/mailman/listinfo/python-list