wrapping all class methods

Robin Becker robin at jessikat.fsnet.co.uk
Sat Nov 16 07:35:45 EST 2002


In article <pan.2002.11.16.12.39.49.262808.1346 at club-internet.fr>, Pedro Rodriguez <pedro_rodriguez at club-internet.fr> writes

This might be true for a proper program, but in this context
I don't know which exceptions are being silently ignored further
up the chain. So this was an attempt at a quick and dirty diagnostic
 to discover which exceptions were being raised in the backend and
 where they were called from. Anygui has lots of exception handling
 trapping (especially attribute traps) so errors in the backend can
 often go unnoticed.

In practice we do reraise the error so a better example might be

################ tbwrap.py v2
def tracebackAdvice(origMethod, methodName):
        def wrap_traceback_method(*args, **kwargs):
                inst = args[0]
                try:
                        origMethod(*args, **kwargs)
                except:
                        print '[%s.%s]' % (inst.__class__.__name__, methodName)
                        from traceback import print_exc
                        print_exc()
                        print 'stack info'
                        import sys
                        f = sys._getframe(1)
                        while f:
                                co = f.f_code
                                print 'name:%s file:%s co_lno:%d lasti:%d f_lno:%d' % (co.co_name, co.co_filename, co.co_firstlineno,
f.f_lasti, f.f_lineno)
                                f = f.f_back
                        raise

        return wrap_traceback_method

def _allMethods(C):
        from inspect import ismethod, getmembers
        M = []
        m = M.append
        for methodname,method in getmembers(C):
                if ismethod(method): m(methodname)
        return tuple(M)

def tracebackWrap(C, *M):
                for m in M or _allMethods(C):
                        setattr(C, m, tracebackAdvice(getattr(C, m), m))

if __name__=='__main__':
        class A(object):
                def f(self, x):
                        assert x>=0
                        print "real f", x
        tracebackWrap(A, "f")
        def L2():
                a = A()
                a.f(1)
                a.f(-1)
        def L1():
                L2()
        def L0():
                try:
                        L1()
                except:
                        pass
        L0()
###################################

>Apologies if this is off-topic.
>
>>                 try:
>>                         origMethod(*args, **kwargs)
>
>This should be:
>                         return origMethod(*args, **kwargs)
>
>>                 except:
>Shouldn't you catch the actual exception:
>
>                  except Exception, e:
>
>in order to let it go its way up at the end of the exception block:
>                          raise e
>
>Notice that by doing so I get the following trace:
>
>$ python2.2 tbwrap.py
>real f 1
>[A.f]
>Traceback (most recent call last):
>  File "tbwrap.py", line 5, in wrap_traceback_method
>    return origMethod(*args, **kwargs)
>  File "tbwrap.py", line 36, in f
>    assert x>=0
>AssertionError
>stack info
>name:L2 file:tbwrap.py co_lno:39 lasti:43 f_lno:42
>name:L1 file:tbwrap.py co_lno:43 lasti:9 f_lno:44
>name:L0 file:tbwrap.py co_lno:45 lasti:9 f_lno:46
>name:? file:tbwrap.py co_lno:1 lasti:138 f_lno:47
>Traceback (most recent call last):
>  File "tbwrap.py", line 47, in ?
>    L0()
>  File "tbwrap.py", line 46, in L0
>    L1()
>  File "tbwrap.py", line 44, in L1
>    L2()
>  File "tbwrap.py", line 42, in L2
>    a.f(-1)
>  File "tbwrap.py", line 17, in wrap_traceback_method
>    raise e
>AssertionError
>
>Pedro

-- 
Robin Becker



More information about the Python-list mailing list