4343# Global variables
4444func_norm_dict = {}
4545func_norm_counter = 0
46- pid_string = `os.getpid()`
47-
48-
49- # Optimized intermodule references
50- ostimes = os .times
46+ if hasattr (os , 'getpid' ):
47+ pid_string = `os.getpid()`
48+ else :
49+ pid_string = ''
5150
5251
5352# Sample timer for use with
@@ -137,7 +136,7 @@ def help():
137136#**************************************************************************
138137class Profile :
139138
140- def __init__ (self , * arg ):
139+ def __init__ (self , timer = None ):
141140 self .timings = {}
142141 self .cur = None
143142 self .cmd = ""
@@ -148,18 +147,22 @@ def __init__(self, *arg):
148147 'exception' : self .trace_dispatch_exception , \
149148 }
150149
151- if not arg :
152- self .timer = os .times
153- self .dispatcher = self .trace_dispatch
150+ if not timer :
151+ if hasattr (os , 'times' ):
152+ self .timer = os .times
153+ self .dispatcher = self .trace_dispatch
154+ else :
155+ self .timer = time .time
156+ self .dispatcher = self .trace_dispatch_i
154157 else :
155- self .timer = arg [ 0 ]
158+ self .timer = timer
156159 t = self .timer () # test out timer function
157160 try :
158161 if len (t ) == 2 :
159162 self .dispatcher = self .trace_dispatch
160163 else :
161- self .dispatcher = self .trace_dispatch_r
162- except :
164+ self .dispatcher = self .trace_dispatch_l
165+ except TypeError :
163166 self .dispatcher = self .trace_dispatch_i
164167 self .t = self .get_time ()
165168 self .simulate_call ('profiler' )
@@ -373,9 +376,9 @@ def func_normalize(self, func_name):
373376 return func_norm_dict [func_name ]
374377 if type (func_name ) == type ("" ):
375378 long_name = string .split (func_name )
376- file_name = long_name [6 ][1 :- 2 ]
379+ file_name = long_name [- 3 ][1 :- 2 ]
377380 func = long_name [2 ]
378- lineno = long_name [8 ][:- 1 ]
381+ lineno = long_name [- 1 ][:- 1 ]
379382 if '?' == func : # Until I find out how to may 'em...
380383 file_name = 'python'
381384 func_norm_counter = func_norm_counter + 1
@@ -398,7 +401,7 @@ def run(self, cmd):
398401
399402 def runctx (self , cmd , globals , locals ):
400403 self .set_cmd (cmd )
401- sys .setprofile (self .trace_dispatch )
404+ sys .setprofile (self .dispatcher )
402405 try :
403406 exec cmd in globals , locals
404407 finally :
@@ -407,7 +410,7 @@ def runctx(self, cmd, globals, locals):
407410 # This method is more useful to profile a single function call.
408411 def runcall (self , func , * args ):
409412 self .set_cmd (`func` )
410- sys .setprofile (self .trace_dispatch )
413+ sys .setprofile (self .dispatcher )
411414 try :
412415 apply (func , args )
413416 finally :
0 commit comments