1 # -*- coding: utf-8 -*-
3 Ce module sert à tracer les appels aux methodes pendant
6 Au début de la zone à tracer faire : Trace.begin_trace()
7 à la fin de la zone faire : Trace.end_trace()
27 def begin_trace(filtre=None,upcall=0):
29 if filtre: filter=filtre
31 sys.settrace(trace_dispatch)
39 def compute_level(frame):
40 """Calcule le niveau dans la pile d'execution"""
42 while frame is not None:
48 frame=sys._getframe(1)
49 level=compute_level(frame)
50 print level*cara,frame.f_code.co_name, " : ",frame.f_code.co_filename,frame.f_lineno
52 print level*' ',"-> appele par : ",frame.f_code.co_name,frame.f_code.co_filename,frame.f_lineno
54 def dispatch_call(frame, arg):
55 """ Cette fonction est appelée par trace_dispatch
56 pour tracer les appels à des fonctions ou méthodes
59 level = ldec*(compute_level(frame)-1)
60 name = frame.f_code.co_name
61 if not name: name = '???'
63 print level*cara +' call', name, frame.f_code.co_filename,frame.f_lineno
66 print level*' ',"-> appele par : ",f_back.f_code.co_name,f_back.f_code.co_filename,f_back.f_lineno
68 print "Pb dans dispatch_call: ",frame
71 def dispatch_exception(frame, arg):
72 """ Cette fonction est appelée par trace_dispatch
73 pour tracer les exceptions
76 dec = cara*ldec*(compute_level(frame)+0)
77 name = frame.f_code.co_name
78 if not name: name = '???'
80 print dec,name,'exception',frame.f_code.co_filename,frame.f_lineno,arg[0],arg[1]
82 print "Pb dans dispatch_exception: ",frame
85 def dispatch_return(frame, arg):
86 """ Cette fonction est appelée par trace_dispatch
87 pour tracer les retours de fonction
89 dec = cara*ldec*compute_level(frame)
90 name = frame.f_code.co_name
91 if not name: name = '???'
92 print dec,name,'return', arg
95 def dispatch_line(frame, arg):
96 """ Cette fonction est appelée par trace_dispatch
97 pour tracer les lignes de source
100 name = frame.f_code.co_name
101 if not name: name = '???'
102 fn=frame.f_code.co_filename
103 line = linecache.getline(fn, frame.f_lineno)
104 dec = cara*ldec*compute_level(frame)
105 print dec,name,':',line.strip(),frame.f_lineno
106 return trace_dispatch
108 def trace_dispatch(frame,event,arg):
109 """ Cette fonction sert à tracer tous les appels
110 à des fonctions ou à des méthodes.
112 if _call and event == 'call': return dispatch_call(frame, arg)
113 if _return and event == 'return': return dispatch_return(frame, arg)
114 if _line and event == 'line': return dispatch_line(frame, arg)
115 if _exception and event == 'exception': return dispatch_exception(frame, arg)
116 return trace_dispatch
148 return not frame.f_code.co_name == 'a'
150 begin_trace(filtre=filter,upcall=1)
162 if __name__ == "__main__":