22import sys
33import inspect
44import os .path
5+ import scorep .trace_dummy
56
67try :
78 import threading
@@ -20,34 +21,29 @@ def _unsettrace():
2021 sys .settrace (None )
2122 threading .settrace (None )
2223
23- global_trace = None
24+ global_trace = scorep . trace_dummy . ScorepTraceDummy ()
2425
2526
2627class ScorepTrace :
2728 def __init__ (self , scorep_bindings , trace = True ):
2829 """
29- @param trace true if there shall be any tracing at all
30+ @param trace true if the tracing shall be initialised.
31+ Please note, that it is still possible to enable the tracing later using register()
3032 """
3133 global global_trace
3234 global_trace = self
3335
3436 self .pathtobasename = {} # for memoizing os.path.basename
35- self .donothing = False
36- self .trace = trace
3737 self .scorep_bindings = scorep_bindings
38- if trace :
39- self .globaltrace = self .globaltrace_lt
40- self .localtrace = self .localtrace_trace
41- else :
42- self .donothing = True
38+ self .globaltrace = self .globaltrace_lt
39+ self .localtrace = self .localtrace_trace
40+ self .no_init_trace = not trace
4341
4442 def register (self ):
45- if not self .donothing :
46- _settrace (self .globaltrace )
43+ _settrace (self .globaltrace )
4744
4845 def unregister (self ):
49- if not self .donothing :
50- _unsettrace ()
46+ _unsettrace ()
5147
5248 def run (self , cmd ):
5349 #import __main__
@@ -60,23 +56,21 @@ def runctx(self, cmd, globals=None, locals=None):
6056 globals = {}
6157 if locals is None :
6258 locals = {}
63- if not self .donothing :
64- _settrace ( self .globaltrace )
59+ if not self .no_init_trace :
60+ self .register ( )
6561 try :
6662 exec (cmd , globals , locals )
6763 finally :
68- if not self .donothing :
69- _unsettrace ()
64+ self .unregister ()
7065
7166 def runfunc (self , func , * args , ** kw ):
7267 result = None
73- if not self .donothing :
74- sys . settrace ( self .globaltrace )
68+ if not self .no_init_trace :
69+ self .register ( )
7570 try :
7671 result = func (* args , ** kw )
7772 finally :
78- if not self .donothing :
79- sys .settrace (None )
73+ self .unregister ()
8074 return result
8175
8276 def globaltrace_lt (self , frame , why , arg ):
@@ -96,7 +90,7 @@ def globaltrace_lt(self, frame, why, arg):
9690 else :
9791 full_file_name = "None"
9892 line_number = frame .f_lineno
99- if self . trace and not code .co_name == "_unsettrace" and not modulename == "scorep.trace" :
93+ if not code .co_name == "_unsettrace" and not modulename == "scorep.trace" :
10094 self .scorep_bindings .region_begin (
10195 modulename , code .co_name , full_file_name , line_number )
10296 return self .localtrace
@@ -109,8 +103,7 @@ def localtrace_trace(self, frame, why, arg):
109103 modulename = frame .f_globals .get ('__name__' , None )
110104 if modulename is None :
111105 modulename = "None"
112- if self .trace :
113- self .scorep_bindings .region_end (modulename , code .co_name )
106+ self .scorep_bindings .region_end (modulename , code .co_name )
114107 return self .localtrace
115108
116109 def user_region_begin (self , name , file_name = None , line_number = None ):
0 commit comments