Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f399fb

Browse files
committed
more robust coding, adapted for mac
1 parent 59c473b commit 4f399fb

2 files changed

Lines changed: 23 additions & 23 deletions

File tree

Lib/profile.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@
4343
# Global variables
4444
func_norm_dict = {}
4545
func_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
#**************************************************************************
138137
class 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:

Lib/pstats.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,10 @@ def load_stats(self, arg):
116116
except: # in case this is not unix
117117
pass
118118
self.files = [ arg ]
119-
elif type(arg) == type(self):
120-
try:
121-
arg.create_stats()
122-
self.stats = arg.stats
123-
arg.stats = {}
124-
except:
125-
pass
119+
elif hasattr(arg, 'create_stats'):
120+
arg.create_stats()
121+
self.stats = arg.stats
122+
arg.stats = {}
126123
if not self.stats:
127124
raise TypeError, "Cannot create or construct a " \
128125
+ `self.__class__` \

0 commit comments

Comments
 (0)