8000 #9209 and #7781: fix two crashes in pstats interactive browser. · python/cpython@b1a97af · GitHub
[go: up one dir, main page]

Skip to content

Commit b1a97af

Browse files
committed
#9209 and #7781: fix two crashes in pstats interactive browser.
1 parent 9f8dc44 commit b1a97af

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

Lib/pstats.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Based on prior profile module by Sjoerd Mullender...
66
# which was hacked somewhat by: Guido van Rossum
77
#
8-
# see profile.doc and profile.py for more info.
8+
# see profile.py for more info.
99

1010
# Copyright 1994, by InfoSeek Corporation, all rights reserved.
1111
# Written by James Roskind
@@ -66,7 +66,7 @@ class Stats:
6666
minor key of 'the name of the function'. Look at the two tables in
6767
sort_stats() and get_sort_arg_defs(self) for more examples.
6868
69-
All methods return self, so you can string together commands like:
69+
All methods return self, so you can string together commands like:
7070
Stats('foo', 'goo').strip_dirs().sort_stats('calls').\
7171
print_stats(5).print_callers(5)
7272
"""
@@ -138,7 +138,7 @@ def add(self, *arg_list):
138138
if not arg_list: return self
139139
if len(arg_list) > 1: self.add(*arg_list[1:])
140140
other = arg_list[0]
141-
if type(self) != type(other) or self.__class__ != other.__class__:
141+
if type(self) != type(other):
142142
other = Stats(other)
143143
self.files += other.files
144144
self.total_calls += other.total_calls
@@ -206,12 +206,12 @@ def sort_stats(self, *field):
206206
if not field:
207207
self.fcn_list = 0
208208
return self
209-
if len(field) == 1 and type(field[0]) == type(1):
209+
if len(field) == 1 and isinstance(field[0], int):
210210
# Be compatible with old profiler
211211
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]] ]
215215

216216
sort_arg_defs = self.get_sort_arg_defs()
217217
sort_tuple = ()
@@ -288,48 +288,53 @@ def calc_callees(self):
288288

289289
def eval_print_amount(self, sel, list, msg):
290290
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
292297
new_list = []
293298
for func in list:
294-
if re.search(sel, func_std_string(func)):
299+
if rex.search(func_std_string(func)):
295300
new_list.append(func)
296301
else:
297302
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:
299304
count = int(count * sel + .5)
300305
new_list = list[:count]
301-
elif type(sel) == type(1) and 0 <= sel < count:
306+
elif isinstance(sel, int) and 0 <= sel < count:
302307
count = sel
303308
new_list = list[:count]
304309
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)
307312

308313
return new_list, msg
309314

310315
def get_print_list(self, sel_list):
311316
width = self.max_name_len
312317
if self.fcn_list:
313-
list = self.fcn_list[:]
318+
stat_list = self.fcn_list[:]
314319
msg = " Ordered by: " + self.sort_type + '\n'
315320
else:
316-
list = self.stats.keys()
321+
stat_list = list(self.stats.keys())
317322
msg = " Random listing order was used\n"
318323

319324
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)
321326

322-
count = len(list)
327+
count = len(stat_list)
323328

324-
if not list:
325-
return 0, list
329+
if not stat_list:
330+
return 0, stat_list
326331
print(msg, file=self.stream)
327332
if count < len(self.stats):
328333
width = 0
329-
for func in list:
334+
for func in stat_list:
330335
if len(func_std_string(func)) > width:
331336
width = len(func_std_string(func))
332-
return width+2, list
337+
return width+2, stat_list
333338

334339
def print_stats(self, *amount):
335340
for filename in self.files:
@@ -536,12 +541,10 @@ class ProfileBrowser(cmd.Cmd):
536541
def __init__(self, profile=None):
537542
cmd.Cmd.__init__(self)
538543
self.prompt = "% "
544+
self.stats = None
545+
self.stream = sys.stdout
539546
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)
545548

546549
def generic(self, fn, line):
547550
args = line.split()

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ Extensions
2929
Library
3030
-------
3131

32+
- Issue #7781: Fix restricting stats by entry counts in the pstats
33+
interactive browser.
34+
35+
- Issue #9209: Do not crash in the pstats interactive browser on invalid
36+
regular expressions.
37+
3238
- Update collections.OrderedDict to match the implementation in Py2.7
3339
(based on lists instead of weakly referenced Link objects).
3440

0 commit comments

Comments
 (0)
0