|
5 | 5 | # Based on prior profile module by Sjoerd Mullender... |
6 | 6 | # which was hacked somewhat by: Guido van Rossum |
7 | 7 | # |
8 | | -# see profile.doc and profile.py for more info. |
| 8 | +# see profile.py for more info. |
9 | 9 |
|
10 | 10 | # Copyright 1994, by InfoSeek Corporation, all rights reserved. |
11 | 11 | # Written by James Roskind |
@@ -66,7 +66,7 @@ class Stats: |
66 | 66 | minor key of 'the name of the function'. Look at the two tables in |
67 | 67 | sort_stats() and get_sort_arg_defs(self) for more examples. |
68 | 68 |
|
69 | | - All methods return self, so you can string together commands like: |
| 69 | + All methods return self, so you can string together commands like: |
70 | 70 | Stats('foo', 'goo').strip_dirs().sort_stats('calls').\ |
71 | 71 | print_stats(5).print_callers(5) |
72 | 72 | """ |
@@ -138,7 +138,7 @@ def add(self, *arg_list): |
138 | 138 | if not arg_list: return self |
139 | 139 | if len(arg_list) > 1: self.add(*arg_list[1:]) |
140 | 140 | other = arg_list[0] |
141 | | - if type(self) != type(other) or self.__class__ != other.__class__: |
| 141 | + if type(self) != type(other): |
142 | 142 | other = Stats(other) |
143 | 143 | self.files += other.files |
144 | 144 | self.total_calls += other.total_calls |
@@ -206,12 +206,12 @@ def sort_stats(self, *field): |
206 | 206 | if not field: |
207 | 207 | self.fcn_list = 0 |
208 | 208 | return self |
209 | | - if len(field) == 1 and type(field[0]) == type(1): |
| 209 | + if len(field) == 1 and isinstance(field[0], int): |
210 | 210 | # Be compatible with old profiler |
211 | 211 | field = [ {-1: "stdname", |
212 | | - 0:"calls", |
213 | | - 1:"time", |
214 | | - 2: "cumulative" } [ field[0] ] ] |
| 212 | + 0: "calls", |
| 213 | + 1: "time", |
| 214 | + 2: "cumulative"}[field[0]] ] |
215 | 215 |
|
216 | 216 | sort_arg_defs = self.get_sort_arg_defs() |
217 | 217 | sort_tuple = () |
@@ -288,48 +288,53 @@ def calc_callees(self): |
288 | 288 |
|
289 | 289 | def eval_print_amount(self, sel, list, msg): |
290 | 290 | new_list = list |
291 | | - if type(sel) == type(""): |
| 291 | + if isinstance(sel, str): |
| 292 | + try: |
| 293 | + rex = re.compile(sel) |
| 294 | + except re.error: |
| 295 | + msg += " <Invalid regular expression %r>\n" % sel |
| 296 | + return new_list, msg |
292 | 297 | new_list = [] |
293 | 298 | for func in list: |
294 | | - if re.search(sel, func_std_string(func)): |
| 299 | + if rex.search(func_std_string(func)): |
295 | 300 | new_list.append(func) |
296 | 301 | else: |
297 | 302 | count = len(list) |
298 | | - if type(sel) == type(1.0) and 0.0 <= sel < 1.0: |
| 303 | + if isinstance(sel, float) and 0.0 <= sel < 1.0: |
299 | 304 | count = int(count * sel + .5) |
300 | 305 | new_list = list[:count] |
301 | | - elif type(sel) == type(1) and 0 <= sel < count: |
| 306 | + elif isinstance(sel, int) and 0 <= sel < count: |
302 | 307 | count = sel |
303 | 308 | new_list = list[:count] |
304 | 309 | if len(list) != len(new_list): |
305 | | - msg = msg + " List reduced from %r to %r due to restriction <%r>\n" % ( |
306 | | - len(list), len(new_list), sel) |
| 310 | + msg += " List reduced from %r to %r due to restriction <%r>\n" % ( |
| 311 | + len(list), len(new_list), sel) |
307 | 312 |
|
308 | 313 | return new_list, msg |
309 | 314 |
|
310 | 315 | def get_print_list(self, sel_list): |
311 | 316 | width = self.max_name_len |
312 | 317 | if self.fcn_list: |
313 | | - list = self.fcn_list[:] |
| 318 | + stat_list = self.fcn_list[:] |
314 | 319 | msg = " Ordered by: " + self.sort_type + '\n' |
315 | 320 | else: |
316 | | - list = self.stats.keys() |
| 321 | + stat_list = list(self.stats.keys()) |
317 | 322 | msg = " Random listing order was used\n" |
318 | 323 |
|
319 | 324 | for selection in sel_list: |
320 | | - list, msg = self.eval_print_amount(selection, list, msg) |
| 325 | + stat_list, msg = self.eval_print_amount(selection, stat_list, msg) |
321 | 326 |
|
322 | | - count = len(list) |
| 327 | + count = len(stat_list) |
323 | 328 |
|
324 | | - if not list: |
325 | | - return 0, list |
| 329 | + if not stat_list: |
| 330 | + return 0, stat_list |
326 | 331 | print(msg, file=self.stream) |
327 | 332 | if count < len(self.stats): |
328 | 333 | width = 0 |
329 | | - for func in list: |
| 334 | + for func in stat_list: |
330 | 335 | if len(func_std_string(func)) > width: |
331 | 336 | width = len(func_std_string(func)) |
332 | | - return width+2, list |
| 337 | + return width+2, stat_list |
333 | 338 |
|
334 | 339 | def print_stats(self, *amount): |
335 | 340 | for filename in self.files: |
@@ -536,12 +541,10 @@ class ProfileBrowser(cmd.Cmd): |
536 | 541 | def __init__(self, profile=None): |
537 | 542 | cmd.Cmd.__init__(self) |
538 | 543 | self.prompt = "% " |
| 544 | + self.stats = None |
| 545 | + self.stream = sys.stdout |
539 | 546 | if profile is not None: |
540 | | - self.stats = Stats(profile) |
541 | | - self.stream = self.stats.stream |
542 | | - else: |
543 | | - self.stats = None |
544 | | - self.stream = sys.stdout |
| 547 | + self.do_read(profile) |
545 | 548 |
|
546 | 549 | def generic(self, fn, line): |
547 | 550 | args = line.split() |
|
0 commit comments