annotate Lib/pstats.py @ 79691:05c8928362ee

Merge __next__ method link fixes with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 12 Oct 2012 13:46:37 +0300
parents 29d4ac311627
children 7d69d04522e3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
1 """Class for printing reports on profiled python code."""
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
2
71040
633597815463 update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 68585
diff changeset
3 # Written by James Roskind
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # Based on prior profile module by Sjoerd Mullender...
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # which was hacked somewhat by: Guido van Rossum
71040
633597815463 update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 68585
diff changeset
6
633597815463 update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 68585
diff changeset
7 # Copyright Disney Enterprises, Inc. All Rights Reserved.
633597815463 update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 68585
diff changeset
8 # Licensed to PSF under a Contributor Agreement
71013
adc244036ef7 strip trailing ws
Benjamin Peterson <benjamin@python.org>
parents: 71006
diff changeset
9 #
71040
633597815463 update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 68585
diff changeset
10 # Licensed under the Apache License, Version 2.0 (the "License");
633597815463 update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 68585
diff changeset
11 # you may not use this file except in compliance with the License.
633597815463 update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 68585
diff changeset
12 # You may obtain a copy of the License at
71013
adc244036ef7 strip trailing ws
Benjamin Peterson <benjamin@python.org>
parents: 71006
diff changeset
13 #
71040
633597815463 update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 68585
diff changeset
14 # http://www.apache.org/licenses/LICENSE-2.0
71013
adc244036ef7 strip trailing ws
Benjamin Peterson <benjamin@python.org>
parents: 71006
diff changeset
15 #
71040
633597815463 update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 68585
diff changeset
16 # Unless required by applicable law or agreed to in writing, software
633597815463 update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 68585
diff changeset
17 # distributed under the License is distributed on an "AS IS" BASIS,
633597815463 update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 68585
diff changeset
18 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
633597815463 update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 68585
diff changeset
19 # either express or implied. See the License for the specific language
633597815463 update profile license (closes #12417)
Benjamin Peterson <benjamin@python.org>
parents: 68585
diff changeset
20 # governing permissions and limitations under the License.
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36459
diff changeset
23 import sys
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 import os
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 import time
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 import marshal
6519
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 6452
diff changeset
27 import re
60370
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 60168
diff changeset
28 from functools import cmp_to_key
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
16562
623456576d6a __all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents: 16492
diff changeset
30 __all__ = ["Stats"]
623456576d6a __all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents: 16492
diff changeset
31
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 class Stats:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
33 """This class is used for creating reports from data generated by the
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
34 Profile class. It is a "friend" of that class, and imports data either
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
35 by direct access to members of Profile class, or by reading in a dictionary
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
36 that was emitted (via marshal) from the Profile class.
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
37
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
38 The big change from the previous Profiler (in terms of raw functionality)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
39 is that an "add()" method has been provided to combine Stats from
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
40 several distinct profile runs. Both the constructor and the add()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
41 method now take arbitrarily many file names as arguments.
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
42
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
43 All the print methods now take an argument that indicates how many lines
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
44 to print. If the arg is a floating point number between 0 and 1.0, then
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
45 it is taken as a decimal percentage of the available lines to be printed
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
46 (e.g., .1 means print 10% of all available lines). If it is an integer,
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
47 it is taken to mean the number of lines of data that you wish to have
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
48 printed.
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
49
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
50 The sort_stats() method now processes some additional options (i.e., in
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36459
diff changeset
51 addition to the old -1, 0, 1, or 2). It takes an arbitrary number of
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36459
diff changeset
52 quoted strings to select the sort order. For example sort_stats('time',
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36459
diff changeset
53 'name') sorts on the major key of 'internal function time', and on the
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36459
diff changeset
54 minor key of 'the name of the function'. Look at the two tables in
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36459
diff changeset
55 sort_stats() and get_sort_arg_defs(self) for more examples.
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
56
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
57 All methods return self, so you can string together commands like:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
58 Stats('foo', 'goo').strip_dirs().sort_stats('calls').\
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
59 print_stats(5).print_callers(5)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
60 """
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
61
55800
a47aa110fcac Use true kwonly arg instead of **kwds hackaround.
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
62 def __init__(self, *args, stream=None):
a47aa110fcac Use true kwonly arg instead of **kwds hackaround.
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
63 self.stream = stream or sys.stdout
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
64 if not len(args):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
65 arg = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
66 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
67 arg = args[0]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
68 args = args[1:]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
69 self.init(arg)
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 23634
diff changeset
70 self.add(*args)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
71
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
72 def init(self, arg):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
73 self.all_callees = None # calc only if needed
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
74 self.files = []
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
75 self.fcn_list = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
76 self.total_tt = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
77 self.total_calls = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
78 self.prim_calls = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
79 self.max_name_len = 0
65588
c6a0eb7f73bf Make top_level attribute a set instead of a dict with None values.
Georg Brandl <georg@python.org>
parents: 65587
diff changeset
80 self.top_level = set()
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
81 self.stats = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
82 self.sort_arg_dict = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
83 self.load_stats(arg)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
84 try:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
85 self.get_top_level_stats()
65589
46bbbdcd777f Refactor interesting use of try-finally.
Georg Brandl <georg@python.org>
parents: 65588
diff changeset
86 except Exception:
46bbbdcd777f Refactor interesting use of try-finally.
Georg Brandl <georg@python.org>
parents: 65588
diff changeset
87 print("Invalid timing data %s" %
46bbbdcd777f Refactor interesting use of try-finally.
Georg Brandl <georg@python.org>
parents: 65588
diff changeset
88 (self.files[-1] if self.files else ''), file=self.stream)
46bbbdcd777f Refactor interesting use of try-finally.
Georg Brandl <georg@python.org>
parents: 65588
diff changeset
89 raise
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
91 def load_stats(self, arg):
65587
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
92 if arg is None:
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
93 self.stats = {}
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
94 return
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43020
diff changeset
95 elif isinstance(arg, str):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
96 f = open(arg, 'rb')
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
97 self.stats = marshal.load(f)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
98 f.close()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
99 try:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
100 file_stats = os.stat(arg)
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23622
diff changeset
101 arg = time.ctime(file_stats.st_mtime) + " " + arg
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
102 except: # in case this is not unix
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
103 pass
65587
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
104 self.files = [arg]
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
105 elif hasattr(arg, 'create_stats'):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
106 arg.create_stats()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
107 self.stats = arg.stats
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
108 arg.stats = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
109 if not self.stats:
65587
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
110 raise TypeError("Cannot create or construct a %r object from %r"
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 41501
diff changeset
111 % (self.__class__, arg))
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
112 return
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
114 def get_top_level_stats(self):
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
115 for func, (cc, nc, tt, ct, callers) in self.stats.items():
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
116 self.total_calls += nc
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
117 self.prim_calls += cc
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
118 self.total_tt += tt
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
119 if ("jprofile", 0, "profiler") in callers:
65588
c6a0eb7f73bf Make top_level attribute a set instead of a dict with None values.
Georg Brandl <georg@python.org>
parents: 65587
diff changeset
120 self.top_level.add(func)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
121 if len(func_std_string(func)) > self.max_name_len:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
122 self.max_name_len = len(func_std_string(func))
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
124 def add(self, *arg_list):
65587
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
125 if not arg_list:
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
126 return self
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
127 for item in reversed(arg_list):
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
128 if type(self) != type(item):
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
129 item = Stats(item)
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
130 self.files += item.files
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
131 self.total_calls += item.total_calls
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
132 self.prim_calls += item.prim_calls
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
133 self.total_tt += item.total_tt
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
134 for func in item.top_level:
65588
c6a0eb7f73bf Make top_level attribute a set instead of a dict with None values.
Georg Brandl <georg@python.org>
parents: 65587
diff changeset
135 self.top_level.add(func)
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
65587
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
137 if self.max_name_len < item.max_name_len:
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
138 self.max_name_len = item.max_name_len
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
139
65587
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
140 self.fcn_list = None
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
65587
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
142 for func, stat in item.stats.items():
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
143 if func in self.stats:
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
144 old_func_stat = self.stats[func]
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
145 else:
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
146 old_func_stat = (0, 0, 0, 0, {},)
12eb24463610 #10166: rewrite self-recursion to iteration in pstats.Stats.add(). Also add a unittest and a stats test file.
Georg Brandl <georg@python.org>
parents: 63547
diff changeset
147 self.stats[func] = add_func_stats(old_func_stat, stat)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
148 return self
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
29111
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
150 def dump_stats(self, filename):
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
151 """Write the profile data to a file we know how to load back."""
39647
dceea41f4a6d Anna Ravenscroft identified many occurrences of "file" used to open a file
Alex Martelli <aleaxit@gmail.com>
parents: 39575
diff changeset
152 f = open(filename, 'wb')
29111
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
153 try:
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
154 marshal.dump(self.stats, f)
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
155 finally:
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
156 f.close()
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
157
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
158 # list the tuple indices and directions for sorting,
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
159 # along with some printable description
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
160 sort_arg_dict_default = {
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
161 "calls" : (((1,-1), ), "call count"),
79577
511a3c51731e Issue #14900: Add aliases for sorting params for pstat to follow column names from pstat output.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 79571
diff changeset
162 "ncalls" : (((1,-1), ), "call count"),
511a3c51731e Issue #14900: Add aliases for sorting params for pstat to follow column names from pstat output.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 79571
diff changeset
163 "cumtime" : (((3,-1), ), "cumulative time"),
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
164 "cumulative": (((3,-1), ), "cumulative time"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
165 "file" : (((4, 1), ), "file name"),
79577
511a3c51731e Issue #14900: Add aliases for sorting params for pstat to follow column names from pstat output.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 79571
diff changeset
166 "filename" : (((4, 1), ), "file name"),
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
167 "line" : (((5, 1), ), "line number"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
168 "module" : (((4, 1), ), "file name"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
169 "name" : (((6, 1), ), "function name"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
170 "nfl" : (((6, 1),(4, 1),(5, 1),), "name/file/line"),
79571
410e92589164 Issue #14900: Distuguish call count and primitive call count in pstat output.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 71566
diff changeset
171 "pcalls" : (((0,-1), ), "primitive call count"),
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
172 "stdname" : (((7, 1), ), "standard name"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
173 "time" : (((2,-1), ), "internal time"),
79577
511a3c51731e Issue #14900: Add aliases for sorting params for pstat to follow column names from pstat output.
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 79571
diff changeset
174 "tottime" : (((2,-1), ), "internal time"),
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
175 }
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
176
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
177 def get_sort_arg_defs(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
178 """Expand all abbreviations that are unique."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
179 if not self.sort_arg_dict:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
180 self.sort_arg_dict = dict = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
181 bad_list = {}
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
182 for word, tup in self.sort_arg_dict_default.items():
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
183 fragment = word
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
184 while fragment:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
185 if not fragment:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
186 break
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
187 if fragment in dict:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
188 bad_list[fragment] = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
189 break
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
190 dict[fragment] = tup
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
191 fragment = fragment[:-1]
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
192 for word in bad_list:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
193 del dict[word]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
194 return self.sort_arg_dict
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
195
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
196 def sort_stats(self, *field):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
197 if not field:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
198 self.fcn_list = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
199 return self
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
200 if len(field) == 1 and isinstance(field[0], int):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
201 # Be compatible with old profiler
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
202 field = [ {-1: "stdname",
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
203 0: "calls",
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
204 1: "time",
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
205 2: "cumulative"}[field[0]] ]
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
206
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
207 sort_arg_defs = self.get_sort_arg_defs()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
208 sort_tuple = ()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
209 self.sort_type = ""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
210 connector = ""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
211 for word in field:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
212 sort_tuple = sort_tuple + sort_arg_defs[word][0]
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
213 self.sort_type += connector + sort_arg_defs[word][1]
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
214 connector = ", "
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
215
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
216 stats_list = []
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
217 for func, (cc, nc, tt, ct, callers) in self.stats.items():
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
218 stats_list.append((cc, nc, tt, ct) + func +
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
219 (func_std_string(func), func))
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
220
60370
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 60168
diff changeset
221 stats_list.sort(key=cmp_to_key(TupleComp(sort_tuple).compare))
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
222
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
223 self.fcn_list = fcn_list = []
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
224 for tuple in stats_list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
225 fcn_list.append(tuple[-1])
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
226 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
227
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
228 def reverse_order(self):
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
229 if self.fcn_list:
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
230 self.fcn_list.reverse()
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
231 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
232
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
233 def strip_dirs(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
234 oldstats = self.stats
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
235 self.stats = newstats = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
236 max_name_len = 0
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
237 for func, (cc, nc, tt, ct, callers) in oldstats.items():
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
238 newfunc = func_strip_path(func)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
239 if len(func_std_string(newfunc)) > max_name_len:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
240 max_name_len = len(func_std_string(newfunc))
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
241 newcallers = {}
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
242 for func2, caller in callers.items():
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
243 newcallers[func_strip_path(func2)] = caller
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
244
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
245 if newfunc in newstats:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
246 newstats[newfunc] = add_func_stats(
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
247 newstats[newfunc],
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
248 (cc, nc, tt, ct, newcallers))
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
249 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
250 newstats[newfunc] = (cc, nc, tt, ct, newcallers)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
251 old_top = self.top_level
65588
c6a0eb7f73bf Make top_level attribute a set instead of a dict with None values.
Georg Brandl <georg@python.org>
parents: 65587
diff changeset
252 self.top_level = new_top = set()
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
253 for func in old_top:
65588
c6a0eb7f73bf Make top_level attribute a set instead of a dict with None values.
Georg Brandl <georg@python.org>
parents: 65587
diff changeset
254 new_top.add(func_strip_path(func))
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
255
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
256 self.max_name_len = max_name_len
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
257
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
258 self.fcn_list = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
259 self.all_callees = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
260 return self
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
261
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
262 def calc_callees(self):
65589
46bbbdcd777f Refactor interesting use of try-finally.
Georg Brandl <georg@python.org>
parents: 65588
diff changeset
263 if self.all_callees:
46bbbdcd777f Refactor interesting use of try-finally.
Georg Brandl <georg@python.org>
parents: 65588
diff changeset
264 return
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
265 self.all_callees = all_callees = {}
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
266 for func, (cc, nc, tt, ct, callers) in self.stats.items():
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
267 if not func in all_callees:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
268 all_callees[func] = {}
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
269 for func2, caller in callers.items():
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
270 if not func2 in all_callees:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
271 all_callees[func2] = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
272 all_callees[func2][func] = caller
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
273 return
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
274
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
275 #******************************************************************
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
276 # The following functions support actual printing of reports
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
277 #******************************************************************
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
278
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
279 # Optional "amount" is either a line count, or a percentage of lines.
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
280
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
281 def eval_print_amount(self, sel, list, msg):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
282 new_list = list
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
283 if isinstance(sel, str):
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
284 try:
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
285 rex = re.compile(sel)
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
286 except re.error:
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
287 msg += " <Invalid regular expression %r>\n" % sel
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
288 return new_list, msg
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
289 new_list = []
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
290 for func in list:
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
291 if rex.search(func_std_string(func)):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
292 new_list.append(func)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
293 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
294 count = len(list)
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
295 if isinstance(sel, float) and 0.0 <= sel < 1.0:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
296 count = int(count * sel + .5)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
297 new_list = list[:count]
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
298 elif isinstance(sel, int) and 0 <= sel < count:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
299 count = sel
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
300 new_list = list[:count]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
301 if len(list) != len(new_list):
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
302 msg += " List reduced from %r to %r due to restriction <%r>\n" % (
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
303 len(list), len(new_list), sel)
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
304
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
305 return new_list, msg
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
306
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
307 def get_print_list(self, sel_list):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
308 width = self.max_name_len
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
309 if self.fcn_list:
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
310 stat_list = self.fcn_list[:]
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
311 msg = " Ordered by: " + self.sort_type + '\n'
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
312 else:
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
313 stat_list = list(self.stats.keys())
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
314 msg = " Random listing order was used\n"
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
315
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
316 for selection in sel_list:
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
317 stat_list, msg = self.eval_print_amount(selection, stat_list, msg)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
318
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
319 count = len(stat_list)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
320
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
321 if not stat_list:
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
322 return 0, stat_list
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
323 print(msg, file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
324 if count < len(self.stats):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
325 width = 0
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
326 for func in stat_list:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
327 if len(func_std_string(func)) > width:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
328 width = len(func_std_string(func))
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
329 return width+2, stat_list
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
330
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
331 def print_stats(self, *amount):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
332 for filename in self.files:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
333 print(filename, file=self.stream)
65589
46bbbdcd777f Refactor interesting use of try-finally.
Georg Brandl <georg@python.org>
parents: 65588
diff changeset
334 if self.files:
46bbbdcd777f Refactor interesting use of try-finally.
Georg Brandl <georg@python.org>
parents: 65588
diff changeset
335 print(file=self.stream)
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
336 indent = ' ' * 8
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
337 for func in self.top_level:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
338 print(indent, func_get_function_name(func), file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
339
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
340 print(indent, self.total_calls, "function calls", end=' ', file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
341 if self.total_calls != self.prim_calls:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
342 print("(%d primitive calls)" % self.prim_calls, end=' ', file=self.stream)
66295
4e512a949b60 Merged revisions 86580 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65115
diff changeset
343 print("in %.3f seconds" % self.total_tt, file=self.stream)
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
344 print(file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
345 width, list = self.get_print_list(amount)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
346 if list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
347 self.print_title()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
348 for func in list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
349 self.print_line(func)
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
350 print(file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
351 print(file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
352 return self
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
353
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
354 def print_callees(self, *amount):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
355 width, list = self.get_print_list(amount)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
356 if list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
357 self.calc_callees()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
358
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
359 self.print_call_heading(width, "called...")
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
360 for func in list:
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
361 if func in self.all_callees:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
362 self.print_call_line(width, func, self.all_callees[func])
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
363 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
364 self.print_call_line(width, func, {})
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
365 print(file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
366 print(file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
367 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
368
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
369 def print_callers(self, *amount):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
370 width, list = self.get_print_list(amount)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
371 if list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
372 self.print_call_heading(width, "was called by...")
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
373 for func in list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
374 cc, nc, tt, ct, callers = self.stats[func]
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
375 self.print_call_line(width, func, callers, "<-")
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
376 print(file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
377 print(file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
378 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
379
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
380 def print_call_heading(self, name_size, column_title):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
381 print("Function ".ljust(name_size) + column_title, file=self.stream)
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
382 # print sub-header only if we have new-style callers
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
383 subheader = False
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
384 for cc, nc, tt, ct, callers in self.stats.values():
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
385 if callers:
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 40809
diff changeset
386 value = next(iter(callers.values()))
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
387 subheader = isinstance(value, tuple)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
388 break
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
389 if subheader:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
390 print(" "*name_size + " ncalls tottime cumtime", file=self.stream)
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
391
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
392 def print_call_line(self, name_size, source, call_dict, arrow="->"):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
393 print(func_std_string(source).ljust(name_size) + arrow, end=' ', file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
394 if not call_dict:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
395 print(file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
396 return
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
397 clist = sorted(call_dict.keys())
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
398 indent = ""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
399 for func in clist:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
400 name = func_std_string(func)
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
401 value = call_dict[func]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
402 if isinstance(value, tuple):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
403 nc, cc, tt, ct = value
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
404 if nc != cc:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
405 substats = '%d/%d' % (nc, cc)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
406 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
407 substats = '%d' % (nc,)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
408 substats = '%s %s %s %s' % (substats.rjust(7+2*len(indent)),
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
409 f8(tt), f8(ct), name)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
410 left_width = name_size + 1
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
411 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
412 substats = '%s(%r) %s' % (name, value, f8(self.stats[func][3]))
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
413 left_width = name_size + 3
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
414 print(indent*left_width + substats, file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
415 indent = " "
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
416
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
417 def print_title(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
418 print(' ncalls tottime percall cumtime percall', end=' ', file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
419 print('filename:lineno(function)', file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
420
65589
46bbbdcd777f Refactor interesting use of try-finally.
Georg Brandl <georg@python.org>
parents: 65588
diff changeset
421 def print_line(self, func): # hack: should print percentages
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
422 cc, nc, tt, ct, callers = self.stats[func]
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
423 c = str(nc)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
424 if nc != cc:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
425 c = c + '/' + str(cc)
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
426 print(c.rjust(9), end=' ', file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
427 print(f8(tt), end=' ', file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
428 if nc == 0:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
429 print(' '*8, end=' ', file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
430 else:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
431 print(f8(tt/nc), end=' ', file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
432 print(f8(ct), end=' ', file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
433 if cc == 0:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
434 print(' '*8, end=' ', file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
435 else:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
436 print(f8(ct/cc), end=' ', file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
437 print(func_std_string(func), file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
438
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 class TupleComp:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
440 """This class provides a generic function for comparing any two tuples.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
441 Each instance records a list of tuple-indices (from most significant
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
442 to least significant), and sort direction (ascending or decending) for
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
443 each tuple-index. The compare functions can then be used as the function
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
444 argument to the system sort() function when a list of tuples need to be
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
445 sorted in the instances order."""
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
446
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
447 def __init__(self, comp_select_list):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
448 self.comp_select_list = comp_select_list
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
449
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
450 def compare (self, left, right):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
451 for index, direction in self.comp_select_list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
452 l = left[index]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
453 r = right[index]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
454 if l < r:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
455 return -direction
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
456 if l > r:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
457 return direction
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
458 return 0
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
459
45061
b9ee31a26f38 Issue #1771: Remove cmp parameter from list.sort() and builtin.sorted().
Raymond Hettinger <python@rcn.com>
parents: 44834
diff changeset
460
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 #**************************************************************************
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
462 # func_name is a triple (file:string, line:int, name:string)
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
463
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 def func_strip_path(func_name):
29111
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
465 filename, line, name = func_name
f4d406acf1ca - add a dump_stats() method similar to that of the profile.Profile class
Fred Drake <fdrake@acm.org>
parents: 28089
diff changeset
466 return os.path.basename(filename), line, name
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
467
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 def func_get_function_name(func):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
469 return func[2]
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
470
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 def func_std_string(func_name): # match what old profile produced
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
472 if func_name[:2] == ('~', 0):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
473 # special case for built-in functions
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
474 name = func_name[2]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
475 if name.startswith('<') and name.endswith('>'):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
476 return '{%s}' % name[1:-1]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
477 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
478 return name
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
479 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
480 return "%s:%d(%s)" % func_name
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
481
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 #**************************************************************************
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 # The following functions combine statists for pairs functions.
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 # The bulk of the processing involves correctly handling "call" lists,
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
485 # such as callers and callees.
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
486 #**************************************************************************
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
487
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
488 def add_func_stats(target, source):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
489 """Add together all the stats for two profile entries."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
490 cc, nc, tt, ct, callers = source
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
491 t_cc, t_nc, t_tt, t_ct, t_callers = target
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
492 return (cc+t_cc, nc+t_nc, tt+t_tt, ct+t_ct,
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
493 add_callers(t_callers, callers))
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
494
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 def add_callers(target, source):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
496 """Combine two caller lists in a single list."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
497 new_callers = {}
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
498 for func, caller in target.items():
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
499 new_callers[func] = caller
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
500 for func, caller in source.items():
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
501 if func in new_callers:
63550
3068e00a3207 Merged revisions 83531 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63441
diff changeset
502 if isinstance(caller, tuple):
3068e00a3207 Merged revisions 83531 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63441
diff changeset
503 # format used by cProfile
3068e00a3207 Merged revisions 83531 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63441
diff changeset
504 new_callers[func] = tuple([i[0] + i[1] for i in
3068e00a3207 Merged revisions 83531 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63441
diff changeset
505 zip(caller, new_callers[func])])
3068e00a3207 Merged revisions 83531 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63441
diff changeset
506 else:
3068e00a3207 Merged revisions 83531 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63441
diff changeset
507 # format used by profile
3068e00a3207 Merged revisions 83531 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63441
diff changeset
508 new_callers[func] += caller
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
509 else:
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
510 new_callers[func] = caller
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
511 return new_callers
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
512
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 def count_calls(callers):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
514 """Sum the caller statistics to get total number of calls received."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
515 nc = 0
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
516 for calls in callers.values():
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
517 nc += calls
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
518 return nc
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
519
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 #**************************************************************************
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 # The following functions support printing of reports
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 #**************************************************************************
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
523
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 def f8(x):
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
525 return "%8.3f" % x
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
526
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
527 #**************************************************************************
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
528 # Statistics browser added by ESR, April 2001
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
529 #**************************************************************************
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
530
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
531 if __name__ == '__main__':
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
532 import cmd
17510
fd0683cb7301 Import readline when possible to make the commaninterpreter UI nicer.
Eric S. Raymond <esr@thyrsus.com>
parents: 17467
diff changeset
533 try:
fd0683cb7301 Import readline when possible to make the commaninterpreter UI nicer.
Eric S. Raymond <esr@thyrsus.com>
parents: 17467
diff changeset
534 import readline
17800
c19980c03e2d When guarding an import, only catch ImportError.
Fred Drake <fdrake@acm.org>
parents: 17634
diff changeset
535 except ImportError:
17510
fd0683cb7301 Import readline when possible to make the commaninterpreter UI nicer.
Eric S. Raymond <esr@thyrsus.com>
parents: 17467
diff changeset
536 pass
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
537
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
538 class ProfileBrowser(cmd.Cmd):
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
539 def __init__(self, profile=None):
18855
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18048
diff changeset
540 cmd.Cmd.__init__(self)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
541 self.prompt = "% "
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
542 self.stats = None
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
543 self.stream = sys.stdout
23622
5edd80726d96 Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
544 if profile is not None:
65115
b9a75dd7420d Merged revisions 82805-82806,83523-83527,83536,83538,83542,83546-83548,83550-83555,83558,83560 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63550
diff changeset
545 self.do_read(profile)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
546
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
547 def generic(self, fn, line):
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
548 args = line.split()
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
549 processed = []
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
550 for term in args:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
551 try:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
552 processed.append(int(term))
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
553 continue
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
554 except ValueError:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
555 pass
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
556 try:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
557 frac = float(term)
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
558 if frac > 1 or frac < 0:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
559 print("Fraction argument must be in [0, 1]", file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
560 continue
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
561 processed.append(frac)
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
562 continue
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
563 except ValueError:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
564 pass
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
565 processed.append(term)
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
566 if self.stats:
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 23634
diff changeset
567 getattr(self.stats, fn)(*processed)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
568 else:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
569 print("No statistics object is loaded.", file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
570 return 0
17634
0bc3377d0d72 Added more help, and recovery from misspelled sort key arguments.
Eric S. Raymond <esr@thyrsus.com>
parents: 17514
diff changeset
571 def generic_help(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
572 print("Arguments may be:", file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
573 print("* An integer maximum number of entries to print.", file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
574 print("* A decimal fractional number between 0 and 1, controlling", file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
575 print(" what fraction of selected entries to print.", file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
576 print("* A regular expression; only entries with function names", file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
577 print(" that match it are printed.", file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
578
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
579 def do_add(self, line):
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
580 if self.stats:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
581 self.stats.add(line)
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
582 else:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
583 print("No statistics object is loaded.", file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
584 return 0
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
585 def help_add(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
586 print("Add profile info from given file to current statistics object.", file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
587
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
588 def do_callees(self, line):
17514
773c64d38ac6 Should resolve [ #416039 ] pstats browser crashes.
Eric S. Raymond <esr@thyrsus.com>
parents: 17510
diff changeset
589 return self.generic('print_callees', line)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
590 def help_callees(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
591 print("Print callees statistics from the current stat object.", file=self.stream)
17634
0bc3377d0d72 Added more help, and recovery from misspelled sort key arguments.
Eric S. Raymond <esr@thyrsus.com>
parents: 17514
diff changeset
592 self.generic_help()
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
593
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
594 def do_callers(self, line):
17514
773c64d38ac6 Should resolve [ #416039 ] pstats browser crashes.
Eric S. Raymond <esr@thyrsus.com>
parents: 17510
diff changeset
595 return self.generic('print_callers', line)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
596 def help_callers(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
597 print("Print callers statistics from the current stat object.", file=self.stream)
17634
0bc3377d0d72 Added more help, and recovery from misspelled sort key arguments.
Eric S. Raymond <esr@thyrsus.com>
parents: 17514
diff changeset
598 self.generic_help()
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
599
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
600 def do_EOF(self, line):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
601 print("", file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
602 return 1
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
603 def help_EOF(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
604 print("Leave the profile brower.", file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
605
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
606 def do_quit(self, line):
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
607 return 1
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
608 def help_quit(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
609 print("Leave the profile brower.", file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
610
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
611 def do_read(self, line):
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
612 if line:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
613 try:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
614 self.stats = Stats(line)
44543
674c9b89d124 The final occurrences of exception slicing.
Georg Brandl <georg@python.org>
parents: 43577
diff changeset
615 except IOError as err:
674c9b89d124 The final occurrences of exception slicing.
Georg Brandl <georg@python.org>
parents: 43577
diff changeset
616 print(err.args[1], file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
617 return
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
618 except Exception as err:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
619 print(err.__class__.__name__ + ':', err, file=self.stream)
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
620 return
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
621 self.prompt = line + "% "
18048
14dc75a5600a Patch #416220: Fix misplaced paren.
Martin v. Löwis <martin@v.loewis.de>
parents: 17800
diff changeset
622 elif len(self.prompt) > 2:
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
623 line = self.prompt[:-2]
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
624 self.do_read(line)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
625 else:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
626 print("No statistics object is current -- cannot reload.", file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
627 return 0
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
628 def help_read(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
629 print("Read in profile data from a specified file.", file=self.stream)
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
630 print("Without argument, reload the current file.", file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
631
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
632 def do_reverse(self, line):
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
633 if self.stats:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
634 self.stats.reverse_order()
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
635 else:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
636 print("No statistics object is loaded.", file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
637 return 0
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
638 def help_reverse(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
639 print("Reverse the sort order of the profiling report.", file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
640
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
641 def do_sort(self, line):
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
642 if not self.stats:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
643 print("No statistics object is loaded.", file=self.stream)
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
644 return
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
645 abbrevs = self.stats.get_sort_arg_defs()
60168
66db03a70767 Merged revisions 79603 via svnmerge from
Andrew M. Kuchling <amk@amk.ca>
parents: 55800
diff changeset
646 if line and all((x in abbrevs) for x in line.split()):
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 23634
diff changeset
647 self.stats.sort_stats(*line.split())
17634
0bc3377d0d72 Added more help, and recovery from misspelled sort key arguments.
Eric S. Raymond <esr@thyrsus.com>
parents: 17514
diff changeset
648 else:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
649 print("Valid sort keys (unique prefixes are accepted):", file=self.stream)
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
650 for (key, value) in Stats.sort_arg_dict_default.items():
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
651 print("%s -- %s" % (key, value[1]), file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
652 return 0
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
653 def help_sort(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
654 print("Sort profile data according to specified keys.", file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
655 print("(Typing `sort' without arguments lists valid keys.)", file=self.stream)
18877
0e212e88d33d Patch #445538: add completion for pstats.py sort cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18855
diff changeset
656 def complete_sort(self, text, *args):
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
657 return [a for a in Stats.sort_arg_dict_default if a.startswith(text)]
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
658
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
659 def do_stats(self, line):
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
660 return self.generic('print_stats', line)
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
661 def help_stats(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
662 print("Print statistics from the current stat object.", file=self.stream)
17634
0bc3377d0d72 Added more help, and recovery from misspelled sort key arguments.
Eric S. Raymond <esr@thyrsus.com>
parents: 17514
diff changeset
663 self.generic_help()
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
664
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
665 def do_strip(self, line):
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
666 if self.stats:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
667 self.stats.strip_dirs()
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
668 else:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
669 print("No statistics object is loaded.", file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
670 def help_strip(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
671 print("Strip leading path information from filenames in the report.", file=self.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
672
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
673 def help_help(self):
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
674 print("Show help for a given command.", file=self.stream)
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
675
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
676 def postcmd(self, stop, line):
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
677 if stop:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
678 return stop
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
679 return None
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
680
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
681 if len(sys.argv) > 1:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
682 initprofile = sys.argv[1]
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
683 else:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
684 initprofile = None
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
685 try:
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
686 browser = ProfileBrowser(initprofile)
75629
3a5a0e7d38c5 Issue #13839: When invoked on the command-line, the pstats module now accepts several filenames of profile stat files and merges them all.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73345
diff changeset
687 for profile in sys.argv[2:]:
3a5a0e7d38c5 Issue #13839: When invoked on the command-line, the pstats module now accepts several filenames of profile stat files and merges them all.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73345
diff changeset
688 browser.do_add(profile)
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
689 print("Welcome to the profile statistics browser.", file=browser.stream)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
690 browser.cmdloop()
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
691 print("Goodbye.", file=browser.stream)
17462
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
692 except KeyboardInterrupt:
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
693 pass
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
694
e8741dc36ad1 Added a test main to the pstats library that can help you browse profile dumps.
Eric S. Raymond <esr@thyrsus.com>
parents: 16562
diff changeset
695 # That's all, folks.