ยปCore Development>Code coverage>Lib/pstats.py

Python code coverage for Lib/pstats.py

#countcontent
1n/a"""Class for printing reports on profiled python code."""
2n/a
3n/a# Written by James Roskind
4n/a# Based on prior profile module by Sjoerd Mullender...
5n/a# which was hacked somewhat by: Guido van Rossum
6n/a
7n/a# Copyright Disney Enterprises, Inc. All Rights Reserved.
8n/a# Licensed to PSF under a Contributor Agreement
9n/a#
10n/a# Licensed under the Apache License, Version 2.0 (the "License");
11n/a# you may not use this file except in compliance with the License.
12n/a# You may obtain a copy of the License at
13n/a#
14n/a# http://www.apache.org/licenses/LICENSE-2.0
15n/a#
16n/a# Unless required by applicable law or agreed to in writing, software
17n/a# distributed under the License is distributed on an "AS IS" BASIS,
18n/a# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
19n/a# either express or implied. See the License for the specific language
20n/a# governing permissions and limitations under the License.
21n/a
22n/a
23n/aimport sys
24n/aimport os
25n/aimport time
26n/aimport marshal
27n/aimport re
28n/afrom functools import cmp_to_key
29n/a
30n/a__all__ = ["Stats"]
31n/a
32n/aclass Stats:
33n/a """This class is used for creating reports from data generated by the
34n/a Profile class. It is a "friend" of that class, and imports data either
35n/a by direct access to members of Profile class, or by reading in a dictionary
36n/a that was emitted (via marshal) from the Profile class.
37n/a
38n/a The big change from the previous Profiler (in terms of raw functionality)
39n/a is that an "add()" method has been provided to combine Stats from
40n/a several distinct profile runs. Both the constructor and the add()
41n/a method now take arbitrarily many file names as arguments.
42n/a
43n/a All the print methods now take an argument that indicates how many lines
44n/a to print. If the arg is a floating point number between 0 and 1.0, then
45n/a it is taken as a decimal percentage of the available lines to be printed
46n/a (e.g., .1 means print 10% of all available lines). If it is an integer,
47n/a it is taken to mean the number of lines of data that you wish to have
48n/a printed.
49n/a
50n/a The sort_stats() method now processes some additional options (i.e., in
51n/a addition to the old -1, 0, 1, or 2). It takes an arbitrary number of
52n/a quoted strings to select the sort order. For example sort_stats('time',
53n/a 'name') sorts on the major key of 'internal function time', and on the
54n/a minor key of 'the name of the function'. Look at the two tables in
55n/a sort_stats() and get_sort_arg_defs(self) for more examples.
56n/a
57n/a All methods return self, so you can string together commands like:
58n/a Stats('foo', 'goo').strip_dirs().sort_stats('calls').\
59n/a print_stats(5).print_callers(5)
60n/a """
61n/a
62n/a def __init__(self, *args, stream=None):
63n/a self.stream = stream or sys.stdout
64n/a if not len(args):
65n/a arg = None
66n/a else:
67n/a arg = args[0]
68n/a args = args[1:]
69n/a self.init(arg)
70n/a self.add(*args)
71n/a
72n/a def init(self, arg):
73n/a self.all_callees = None # calc only if needed
74n/a self.files = []
75n/a self.fcn_list = None
76n/a self.total_tt = 0
77n/a self.total_calls = 0
78n/a self.prim_calls = 0
79n/a self.max_name_len = 0
80n/a self.top_level = set()
81n/a self.stats = {}
82n/a self.sort_arg_dict = {}
83n/a self.load_stats(arg)
84n/a try:
85n/a self.get_top_level_stats()
86n/a except Exception:
87n/a print("Invalid timing data %s" %
88n/a (self.files[-1] if self.files else ''), file=self.stream)
89n/a raise
90n/a
91n/a def load_stats(self, arg):
92n/a if arg is None:
93n/a self.stats = {}
94n/a return
95n/a elif isinstance(arg, str):
96n/a with open(arg, 'rb') as f:
97n/a self.stats = marshal.load(f)
98n/a try:
99n/a file_stats = os.stat(arg)
100n/a arg = time.ctime(file_stats.st_mtime) + " " + arg
101n/a except: # in case this is not unix
102n/a pass
103n/a self.files = [arg]
104n/a elif hasattr(arg, 'create_stats'):
105n/a arg.create_stats()
106n/a self.stats = arg.stats
107n/a arg.stats = {}
108n/a if not self.stats:
109n/a raise TypeError("Cannot create or construct a %r object from %r"
110n/a % (self.__class__, arg))
111n/a return
112n/a
113n/a def get_top_level_stats(self):
114n/a for func, (cc, nc, tt, ct, callers) in self.stats.items():
115n/a self.total_calls += nc
116n/a self.prim_calls += cc
117n/a self.total_tt += tt
118n/a if ("jprofile", 0, "profiler") in callers:
119n/a self.top_level.add(func)
120n/a if len(func_std_string(func)) > self.max_name_len:
121n/a self.max_name_len = len(func_std_string(func))
122n/a
123n/a def add(self, *arg_list):
124n/a if not arg_list:
125n/a return self
126n/a for item in reversed(arg_list):
127n/a if type(self) != type(item):
128n/a item = Stats(item)
129n/a self.files += item.files
130n/a self.total_calls += item.total_calls
131n/a self.prim_calls += item.prim_calls
132n/a self.total_tt += item.total_tt
133n/a for func in item.top_level:
134n/a self.top_level.add(func)
135n/a
136n/a if self.max_name_len < item.max_name_len:
137n/a self.max_name_len = item.max_name_len
138n/a
139n/a self.fcn_list = None
140n/a
141n/a for func, stat in item.stats.items():
142n/a if func in self.stats:
143n/a old_func_stat = self.stats[func]
144n/a else:
145n/a old_func_stat = (0, 0, 0, 0, {},)
146n/a self.stats[func] = add_func_stats(old_func_stat, stat)
147n/a return self
148n/a
149n/a def dump_stats(self, filename):
150n/a """Write the profile data to a file we know how to load back."""
151n/a with open(filename, 'wb') as f:
152n/a marshal.dump(self.stats, f)
153n/a
154n/a # list the tuple indices and directions for sorting,
155n/a # along with some printable description
156n/a sort_arg_dict_default = {
157n/a "calls" : (((1,-1), ), "call count"),
158n/a "ncalls" : (((1,-1), ), "call count"),
159n/a "cumtime" : (((3,-1), ), "cumulative time"),
160n/a "cumulative": (((3,-1), ), "cumulative time"),
161n/a "file" : (((4, 1), ), "file name"),
162n/a "filename" : (((4, 1), ), "file name"),
163n/a "line" : (((5, 1), ), "line number"),
164n/a "module" : (((4, 1), ), "file name"),
165n/a "name" : (((6, 1), ), "function name"),
166n/a "nfl" : (((6, 1),(4, 1),(5, 1),), "name/file/line"),
167n/a "pcalls" : (((0,-1), ), "primitive call count"),
168n/a "stdname" : (((7, 1), ), "standard name"),
169n/a "time" : (((2,-1), ), "internal time"),
170n/a "tottime" : (((2,-1), ), "internal time"),
171n/a }
172n/a
173n/a def get_sort_arg_defs(self):
174n/a """Expand all abbreviations that are unique."""
175n/a if not self.sort_arg_dict:
176n/a self.sort_arg_dict = dict = {}
177n/a bad_list = {}
178n/a for word, tup in self.sort_arg_dict_default.items():
179n/a fragment = word
180n/a while fragment:
181n/a if not fragment:
182n/a break
183n/a if fragment in dict:
184n/a bad_list[fragment] = 0
185n/a break
186n/a dict[fragment] = tup
187n/a fragment = fragment[:-1]
188n/a for word in bad_list:
189n/a del dict[word]
190n/a return self.sort_arg_dict
191n/a
192n/a def sort_stats(self, *field):
193n/a if not field:
194n/a self.fcn_list = 0
195n/a return self
196n/a if len(field) == 1 and isinstance(field[0], int):
197n/a # Be compatible with old profiler
198n/a field = [ {-1: "stdname",
199n/a 0: "calls",
200n/a 1: "time",
201n/a 2: "cumulative"}[field[0]] ]
202n/a
203n/a sort_arg_defs = self.get_sort_arg_defs()
204n/a sort_tuple = ()
205n/a self.sort_type = ""
206n/a connector = ""
207n/a for word in field:
208n/a sort_tuple = sort_tuple + sort_arg_defs[word][0]
209n/a self.sort_type += connector + sort_arg_defs[word][1]
210n/a connector = ", "
211n/a
212n/a stats_list = []
213n/a for func, (cc, nc, tt, ct, callers) in self.stats.items():
214n/a stats_list.append((cc, nc, tt, ct) + func +
215n/a (func_std_string(func), func))
216n/a
217n/a stats_list.sort(key=cmp_to_key(TupleComp(sort_tuple).compare))
218n/a
219n/a self.fcn_list = fcn_list = []
220n/a for tuple in stats_list:
221n/a fcn_list.append(tuple[-1])
222n/a return self
223n/a
224n/a def reverse_order(self):
225n/a if self.fcn_list:
226n/a self.fcn_list.reverse()
227n/a return self
228n/a
229n/a def strip_dirs(self):
230n/a oldstats = self.stats
231n/a self.stats = newstats = {}
232n/a max_name_len = 0
233n/a for func, (cc, nc, tt, ct, callers) in oldstats.items():
234n/a newfunc = func_strip_path(func)
235n/a if len(func_std_string(newfunc)) > max_name_len:
236n/a max_name_len = len(func_std_string(newfunc))
237n/a newcallers = {}
238n/a for func2, caller in callers.items():
239n/a newcallers[func_strip_path(func2)] = caller
240n/a
241n/a if newfunc in newstats:
242n/a newstats[newfunc] = add_func_stats(
243n/a newstats[newfunc],
244n/a (cc, nc, tt, ct, newcallers))
245n/a else:
246n/a newstats[newfunc] = (cc, nc, tt, ct, newcallers)
247n/a old_top = self.top_level
248n/a self.top_level = new_top = set()
249n/a for func in old_top:
250n/a new_top.add(func_strip_path(func))
251n/a
252n/a self.max_name_len = max_name_len
253n/a
254n/a self.fcn_list = None
255n/a self.all_callees = None
256n/a return self
257n/a
258n/a def calc_callees(self):
259n/a if self.all_callees:
260n/a return
261n/a self.all_callees = all_callees = {}
262n/a for func, (cc, nc, tt, ct, callers) in self.stats.items():
263n/a if not func in all_callees:
264n/a all_callees[func] = {}
265n/a for func2, caller in callers.items():
266n/a if not func2 in all_callees:
267n/a all_callees[func2] = {}
268n/a all_callees[func2][func] = caller
269n/a return
270n/a
271n/a #******************************************************************
272n/a # The following functions support actual printing of reports
273n/a #******************************************************************
274n/a
275n/a # Optional "amount" is either a line count, or a percentage of lines.
276n/a
277n/a def eval_print_amount(self, sel, list, msg):
278n/a new_list = list
279n/a if isinstance(sel, str):
280n/a try:
281n/a rex = re.compile(sel)
282n/a except re.error:
283n/a msg += " <Invalid regular expression %r>\n" % sel
284n/a return new_list, msg
285n/a new_list = []
286n/a for func in list:
287n/a if rex.search(func_std_string(func)):
288n/a new_list.append(func)
289n/a else:
290n/a count = len(list)
291n/a if isinstance(sel, float) and 0.0 <= sel < 1.0:
292n/a count = int(count * sel + .5)
293n/a new_list = list[:count]
294n/a elif isinstance(sel, int) and 0 <= sel < count:
295n/a count = sel
296n/a new_list = list[:count]
297n/a if len(list) != len(new_list):
298n/a msg += " List reduced from %r to %r due to restriction <%r>\n" % (
299n/a len(list), len(new_list), sel)
300n/a
301n/a return new_list, msg
302n/a
303n/a def get_print_list(self, sel_list):
304n/a width = self.max_name_len
305n/a if self.fcn_list:
306n/a stat_list = self.fcn_list[:]
307n/a msg = " Ordered by: " + self.sort_type + '\n'
308n/a else:
309n/a stat_list = list(self.stats.keys())
310n/a msg = " Random listing order was used\n"
311n/a
312n/a for selection in sel_list:
313n/a stat_list, msg = self.eval_print_amount(selection, stat_list, msg)
314n/a
315n/a count = len(stat_list)
316n/a
317n/a if not stat_list:
318n/a return 0, stat_list
319n/a print(msg, file=self.stream)
320n/a if count < len(self.stats):
321n/a width = 0
322n/a for func in stat_list:
323n/a if len(func_std_string(func)) > width:
324n/a width = len(func_std_string(func))
325n/a return width+2, stat_list
326n/a
327n/a def print_stats(self, *amount):
328n/a for filename in self.files:
329n/a print(filename, file=self.stream)
330n/a if self.files:
331n/a print(file=self.stream)
332n/a indent = ' ' * 8
333n/a for func in self.top_level:
334n/a print(indent, func_get_function_name(func), file=self.stream)
335n/a
336n/a print(indent, self.total_calls, "function calls", end=' ', file=self.stream)
337n/a if self.total_calls != self.prim_calls:
338n/a print("(%d primitive calls)" % self.prim_calls, end=' ', file=self.stream)
339n/a print("in %.3f seconds" % self.total_tt, file=self.stream)
340n/a print(file=self.stream)
341n/a width, list = self.get_print_list(amount)
342n/a if list:
343n/a self.print_title()
344n/a for func in list:
345n/a self.print_line(func)
346n/a print(file=self.stream)
347n/a print(file=self.stream)
348n/a return self
349n/a
350n/a def print_callees(self, *amount):
351n/a width, list = self.get_print_list(amount)
352n/a if list:
353n/a self.calc_callees()
354n/a
355n/a self.print_call_heading(width, "called...")
356n/a for func in list:
357n/a if func in self.all_callees:
358n/a self.print_call_line(width, func, self.all_callees[func])
359n/a else:
360n/a self.print_call_line(width, func, {})
361n/a print(file=self.stream)
362n/a print(file=self.stream)
363n/a return self
364n/a
365n/a def print_callers(self, *amount):
366n/a width, list = self.get_print_list(amount)
367n/a if list:
368n/a self.print_call_heading(width, "was called by...")
369n/a for func in list:
370n/a cc, nc, tt, ct, callers = self.stats[func]
371n/a self.print_call_line(width, func, callers, "<-")
372n/a print(file=self.stream)
373n/a print(file=self.stream)
374n/a return self
375n/a
376n/a def print_call_heading(self, name_size, column_title):
377n/a print("Function ".ljust(name_size) + column_title, file=self.stream)
378n/a # print sub-header only if we have new-style callers
379n/a subheader = False
380n/a for cc, nc, tt, ct, callers in self.stats.values():
381n/a if callers:
382n/a value = next(iter(callers.values()))
383n/a subheader = isinstance(value, tuple)
384n/a break
385n/a if subheader:
386n/a print(" "*name_size + " ncalls tottime cumtime", file=self.stream)
387n/a
388n/a def print_call_line(self, name_size, source, call_dict, arrow="->"):
389n/a print(func_std_string(source).ljust(name_size) + arrow, end=' ', file=self.stream)
390n/a if not call_dict:
391n/a print(file=self.stream)
392n/a return
393n/a clist = sorted(call_dict.keys())
394n/a indent = ""
395n/a for func in clist:
396n/a name = func_std_string(func)
397n/a value = call_dict[func]
398n/a if isinstance(value, tuple):
399n/a nc, cc, tt, ct = value
400n/a if nc != cc:
401n/a substats = '%d/%d' % (nc, cc)
402n/a else:
403n/a substats = '%d' % (nc,)
404n/a substats = '%s %s %s %s' % (substats.rjust(7+2*len(indent)),
405n/a f8(tt), f8(ct), name)
406n/a left_width = name_size + 1
407n/a else:
408n/a substats = '%s(%r) %s' % (name, value, f8(self.stats[func][3]))
409n/a left_width = name_size + 3
410n/a print(indent*left_width + substats, file=self.stream)
411n/a indent = " "
412n/a
413n/a def print_title(self):
414n/a print(' ncalls tottime percall cumtime percall', end=' ', file=self.stream)
415n/a print('filename:lineno(function)', file=self.stream)
416n/a
417n/a def print_line(self, func): # hack: should print percentages
418n/a cc, nc, tt, ct, callers = self.stats[func]
419n/a c = str(nc)
420n/a if nc != cc:
421n/a c = c + '/' + str(cc)
422n/a print(c.rjust(9), end=' ', file=self.stream)
423n/a print(f8(tt), end=' ', file=self.stream)
424n/a if nc == 0:
425n/a print(' '*8, end=' ', file=self.stream)
426n/a else:
427n/a print(f8(tt/nc), end=' ', file=self.stream)
428n/a print(f8(ct), end=' ', file=self.stream)
429n/a if cc == 0:
430n/a print(' '*8, end=' ', file=self.stream)
431n/a else:
432n/a print(f8(ct/cc), end=' ', file=self.stream)
433n/a print(func_std_string(func), file=self.stream)
434n/a
435n/aclass TupleComp:
436n/a """This class provides a generic function for comparing any two tuples.
437n/a Each instance records a list of tuple-indices (from most significant
438n/a to least significant), and sort direction (ascending or decending) for
439n/a each tuple-index. The compare functions can then be used as the function
440n/a argument to the system sort() function when a list of tuples need to be
441n/a sorted in the instances order."""
442n/a
443n/a def __init__(self, comp_select_list):
444n/a self.comp_select_list = comp_select_list
445n/a
446n/a def compare (self, left, right):
447n/a for index, direction in self.comp_select_list:
448n/a l = left[index]
449n/a r = right[index]
450n/a if l < r:
451n/a return -direction
452n/a if l > r:
453n/a return direction
454n/a return 0
455n/a
456n/a
457n/a#**************************************************************************
458n/a# func_name is a triple (file:string, line:int, name:string)
459n/a
460n/adef func_strip_path(func_name):
461n/a filename, line, name = func_name
462n/a return os.path.basename(filename), line, name
463n/a
464n/adef func_get_function_name(func):
465n/a return func[2]
466n/a
467n/adef func_std_string(func_name): # match what old profile produced
468n/a if func_name[:2] == ('~', 0):
469n/a # special case for built-in functions
470n/a name = func_name[2]
471n/a if name.startswith('<') and name.endswith('>'):
472n/a return '{%s}' % name[1:-1]
473n/a else:
474n/a return name
475n/a else:
476n/a return "%s:%d(%s)" % func_name
477n/a
478n/a#**************************************************************************
479n/a# The following functions combine statists for pairs functions.
480n/a# The bulk of the processing involves correctly handling "call" lists,
481n/a# such as callers and callees.
482n/a#**************************************************************************
483n/a
484n/adef add_func_stats(target, source):
485n/a """Add together all the stats for two profile entries."""
486n/a cc, nc, tt, ct, callers = source
487n/a t_cc, t_nc, t_tt, t_ct, t_callers = target
488n/a return (cc+t_cc, nc+t_nc, tt+t_tt, ct+t_ct,
489n/a add_callers(t_callers, callers))
490n/a
491n/adef add_callers(target, source):
492n/a """Combine two caller lists in a single list."""
493n/a new_callers = {}
494n/a for func, caller in target.items():
495n/a new_callers[func] = caller
496n/a for func, caller in source.items():
497n/a if func in new_callers:
498n/a if isinstance(caller, tuple):
499n/a # format used by cProfile
500n/a new_callers[func] = tuple([i[0] + i[1] for i in
501n/a zip(caller, new_callers[func])])
502n/a else:
503n/a # format used by profile
504n/a new_callers[func] += caller
505n/a else:
506n/a new_callers[func] = caller
507n/a return new_callers
508n/a
509n/adef count_calls(callers):
510n/a """Sum the caller statistics to get total number of calls received."""
511n/a nc = 0
512n/a for calls in callers.values():
513n/a nc += calls
514n/a return nc
515n/a
516n/a#**************************************************************************
517n/a# The following functions support printing of reports
518n/a#**************************************************************************
519n/a
520n/adef f8(x):
521n/a return "%8.3f" % x
522n/a
523n/a#**************************************************************************
524n/a# Statistics browser added by ESR, April 2001
525n/a#**************************************************************************
526n/a
527n/aif __name__ == '__main__':
528n/a import cmd
529n/a try:
530n/a import readline
531n/a except ImportError:
532n/a pass
533n/a
534n/a class ProfileBrowser(cmd.Cmd):
535n/a def __init__(self, profile=None):
536n/a cmd.Cmd.__init__(self)
537n/a self.prompt = "% "
538n/a self.stats = None
539n/a self.stream = sys.stdout
540n/a if profile is not None:
541n/a self.do_read(profile)
542n/a
543n/a def generic(self, fn, line):
544n/a args = line.split()
545n/a processed = []
546n/a for term in args:
547n/a try:
548n/a processed.append(int(term))
549n/a continue
550n/a except ValueError:
551n/a pass
552n/a try:
553n/a frac = float(term)
554n/a if frac > 1 or frac < 0:
555n/a print("Fraction argument must be in [0, 1]", file=self.stream)
556n/a continue
557n/a processed.append(frac)
558n/a continue
559n/a except ValueError:
560n/a pass
561n/a processed.append(term)
562n/a if self.stats:
563n/a getattr(self.stats, fn)(*processed)
564n/a else:
565n/a print("No statistics object is loaded.", file=self.stream)
566n/a return 0
567n/a def generic_help(self):
568n/a print("Arguments may be:", file=self.stream)
569n/a print("* An integer maximum number of entries to print.", file=self.stream)
570n/a print("* A decimal fractional number between 0 and 1, controlling", file=self.stream)
571n/a print(" what fraction of selected entries to print.", file=self.stream)
572n/a print("* A regular expression; only entries with function names", file=self.stream)
573n/a print(" that match it are printed.", file=self.stream)
574n/a
575n/a def do_add(self, line):
576n/a if self.stats:
577n/a try:
578n/a self.stats.add(line)
579n/a except IOError as e:
580n/a print("Failed to load statistics for %s: %s" % (line, e), file=self.stream)
581n/a else:
582n/a print("No statistics object is loaded.", file=self.stream)
583n/a return 0
584n/a def help_add(self):
585n/a print("Add profile info from given file to current statistics object.", file=self.stream)
586n/a
587n/a def do_callees(self, line):
588n/a return self.generic('print_callees', line)
589n/a def help_callees(self):
590n/a print("Print callees statistics from the current stat object.", file=self.stream)
591n/a self.generic_help()
592n/a
593n/a def do_callers(self, line):
594n/a return self.generic('print_callers', line)
595n/a def help_callers(self):
596n/a print("Print callers statistics from the current stat object.", file=self.stream)
597n/a self.generic_help()
598n/a
599n/a def do_EOF(self, line):
600n/a print("", file=self.stream)
601n/a return 1
602n/a def help_EOF(self):
603n/a print("Leave the profile brower.", file=self.stream)
604n/a
605n/a def do_quit(self, line):
606n/a return 1
607n/a def help_quit(self):
608n/a print("Leave the profile brower.", file=self.stream)
609n/a
610n/a def do_read(self, line):
611n/a if line:
612n/a try:
613n/a self.stats = Stats(line)
614n/a except OSError as err:
615n/a print(err.args[1], file=self.stream)
616n/a return
617n/a except Exception as err:
618n/a print(err.__class__.__name__ + ':', err, file=self.stream)
619n/a return
620n/a self.prompt = line + "% "
621n/a elif len(self.prompt) > 2:
622n/a line = self.prompt[:-2]
623n/a self.do_read(line)
624n/a else:
625n/a print("No statistics object is current -- cannot reload.", file=self.stream)
626n/a return 0
627n/a def help_read(self):
628n/a print("Read in profile data from a specified file.", file=self.stream)
629n/a print("Without argument, reload the current file.", file=self.stream)
630n/a
631n/a def do_reverse(self, line):
632n/a if self.stats:
633n/a self.stats.reverse_order()
634n/a else:
635n/a print("No statistics object is loaded.", file=self.stream)
636n/a return 0
637n/a def help_reverse(self):
638n/a print("Reverse the sort order of the profiling report.", file=self.stream)
639n/a
640n/a def do_sort(self, line):
641n/a if not self.stats:
642n/a print("No statistics object is loaded.", file=self.stream)
643n/a return
644n/a abbrevs = self.stats.get_sort_arg_defs()
645n/a if line and all((x in abbrevs) for x in line.split()):
646n/a self.stats.sort_stats(*line.split())
647n/a else:
648n/a print("Valid sort keys (unique prefixes are accepted):", file=self.stream)
649n/a for (key, value) in Stats.sort_arg_dict_default.items():
650n/a print("%s -- %s" % (key, value[1]), file=self.stream)
651n/a return 0
652n/a def help_sort(self):
653n/a print("Sort profile data according to specified keys.", file=self.stream)
654n/a print("(Typing `sort' without arguments lists valid keys.)", file=self.stream)
655n/a def complete_sort(self, text, *args):
656n/a return [a for a in Stats.sort_arg_dict_default if a.startswith(text)]
657n/a
658n/a def do_stats(self, line):
659n/a return self.generic('print_stats', line)
660n/a def help_stats(self):
661n/a print("Print statistics from the current stat object.", file=self.stream)
662n/a self.generic_help()
663n/a
664n/a def do_strip(self, line):
665n/a if self.stats:
666n/a self.stats.strip_dirs()
667n/a else:
668n/a print("No statistics object is loaded.", file=self.stream)
669n/a def help_strip(self):
670n/a print("Strip leading path information from filenames in the report.", file=self.stream)
671n/a
672n/a def help_help(self):
673n/a print("Show help for a given command.", file=self.stream)
674n/a
675n/a def postcmd(self, stop, line):
676n/a if stop:
677n/a return stop
678n/a return None
679n/a
680n/a if len(sys.argv) > 1:
681n/a initprofile = sys.argv[1]
682n/a else:
683n/a initprofile = None
684n/a try:
685n/a browser = ProfileBrowser(initprofile)
686n/a for profile in sys.argv[2:]:
687n/a browser.do_add(profile)
688n/a print("Welcome to the profile statistics browser.", file=browser.stream)
689n/a browser.cmdloop()
690n/a print("Goodbye.", file=browser.stream)
691n/a except KeyboardInterrupt:
692n/a pass
693n/a
694n/a# That's all, folks.