From d48a23e54c5cdf55e57d7ef88c78cf315aac34f9 Mon Sep 17 00:00:00 2001 From: Christian Caremoli <> Date: Mon, 19 Nov 2007 14:25:41 +0000 Subject: [PATCH] CCAR: ajout traced.py pour tracer les appels de methodes --- Misc/traced.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Misc/traced.py diff --git a/Misc/traced.py b/Misc/traced.py new file mode 100644 index 00000000..cece5acf --- /dev/null +++ b/Misc/traced.py @@ -0,0 +1,47 @@ +indent = 0 +indStr = ' ' + +def srepr(*argl,**argd): + #parse the arguments and create a string representation + args = [] + for item in argl: + args.append('%s' % str(item)) + for key,item in argd.items(): + args.append('%s=%s' % (key,str(item))) + argstr = ','.join(args) + return argstr + +def logfunc(f): + def _method(*argl,**argd): + global indent + print indStr*indent+f.__module__+"."+f.__name__+"("+srepr(*argl,**argd)+")" + indent += 1 + r=f(*argl,**argd) + indent -= 1 + print indStr*indent+str(r) + return r + return _method + +def logmeth(f): + def _method(self,*argl,**argd): + global indent + print indStr*indent+str(self)+"."+f.__name__+"("+srepr(*argl,**argd)+")" + indent += 1 + r=f(self,*argl,**argd) + indent -= 1 + print indStr*indent+str(r) + return r + return _method + +if __name__ == "__main__": + @logfunc + def g(a,b): + return a + class A: + @logmeth + def h(self,a): + return g(a,a) + + g(1,2) + a=A() + a.h(1) -- 2.39.2