annotate Lib/pstats.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 0fbc56b53848
children 410e92589164 fa82071bb7e1
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"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
162 "cumulative": (((3,-1), ), "cumulative time"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
163 "file" : (((4, 1), ), "file name"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
164 "line" : (((5, 1), ), "line number"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
165 "module" : (((4, 1), ), "file name"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
166 "name" : (((6, 1), ), "function name"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
167 "nfl" : (((6, 1),(4, 1),(5, 1),), "name/file/line"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
168 "pcalls" : (((0,-1), ), "call count"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
169 "stdname" : (((7, 1), ), "standard name"),
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
170 "time" : (((2,-1), ), "internal time"),
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
171 }
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
173 def get_sort_arg_defs(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
174 """Expand all abbreviations that are unique."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
175 if not self.sort_arg_dict:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
176 self.sort_arg_dict = dict = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
177 bad_list = {}
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
178 for word, tup in self.sort_arg_dict_default.items():
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
179 fragment = word
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
180 while fragment:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
181 if not fragment:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
182 break
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
183 if fragment in dict:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
184 bad_list[fragment] = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
185 break
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
186 dict[fragment] = tup
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
187 fragment = fragment[:-1]
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
188 for word in bad_list:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
189 del dict[word]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
190 return self.sort_arg_dict
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
192 def sort_stats(self, *field):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
193 if not field:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
194 self.fcn_list = 0
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
195 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
196 if len(field) == 1 and isinstance(field[0], int):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
197 # Be compatible with old profiler
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
198 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
199 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
200 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
201 2: "cumulative"}[field[0]] ]
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
203 sort_arg_defs = self.get_sort_arg_defs()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
204 sort_tuple = ()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
205 self.sort_type = ""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
206 connector = ""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
207 for word in field:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
208 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
209 self.sort_type += connector + sort_arg_defs[word][1]
15870
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
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
212 stats_list = []
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
213 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
214 stats_list.append((cc, nc, tt, ct) + func +
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
215 (func_std_string(func), func))
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
216
60370
3ce66c6922ab Forward port total_ordering() and cmp_to_key().
Raymond Hettinger <python@rcn.com>
parents: 60168
diff changeset
217 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
218
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
219 self.fcn_list = fcn_list = []
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
220 for tuple in stats_list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
221 fcn_list.append(tuple[-1])
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
222 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
223
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
224 def reverse_order(self):
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
225 if self.fcn_list:
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
226 self.fcn_list.reverse()
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
227 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
228
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
229 def strip_dirs(self):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
230 oldstats = self.stats
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
231 self.stats = newstats = {}
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
232 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
233 for func, (cc, nc, tt, ct, callers) in oldstats.items():
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
234 newfunc = func_strip_path(func)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
235 if len(func_std_string(newfunc)) > max_name_len:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
236 max_name_len = len(func_std_string(newfunc))
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
237 newcallers = {}
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
238 for func2, caller in callers.items():
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
239 newcallers[func_strip_path(func2)] = caller
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
240
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
241 if newfunc in newstats:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
242 newstats[newfunc] = add_func_stats(
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
243 newstats[newfunc],
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
244 (cc, nc, tt, ct, newcallers))
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
245 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
246 newstats[newfunc] = (cc, nc, tt, ct, newcallers)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
247 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
248 self.top_level = new_top = set()
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
249 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
250 new_top.add(func_strip_path(func))
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
251
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
252 self.max_name_len = max_name_len
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
253
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
254 self.fcn_list = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
255 self.all_callees = None
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
256 return self
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
257
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
258 def calc_callees(self):
65589
46bbbdcd777f Refactor interesting use of try-finally.
Georg Brandl <georg@python.org>
parents: 65588
diff changeset
259 if self.all_callees:
46bbbdcd777f Refactor interesting use of try-finally.
Georg Brandl <georg@python.org>
parents: 65588
diff changeset
260 return
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
261 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
262 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
263 if not func in all_callees:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
264 all_callees[func] = {}
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
265 for func2, caller in callers.items():
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
266 if not func2 in all_callees:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
267 all_callees[func2] = {}
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
268 all_callees[func2][func] = caller
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
269 return
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
270
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
271 #******************************************************************
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
272 # The following functions support actual printing of reports
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
273 #******************************************************************
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
274
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
275 # 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
276
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
277 def eval_print_amount(self, sel, list, msg):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
278 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
279 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
280 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
281 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
282 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
283 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
284 return new_list, msg
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
285 new_list = []
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
286 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
287 if rex.search(func_std_string(func)):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
288 new_list.append(func)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
289 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
290 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
291 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
292 count = int(count * sel + .5)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
293 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
294 elif isinstance(sel, int) and 0 <= sel < count:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
295 count = sel
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
296 new_list = list[:count]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
297 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
298 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
299 len(list), len(new_list), sel)
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
300
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
301 return new_list, msg
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
302
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
303 def get_print_list(self, sel_list):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
304 width = self.max_name_len
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
305 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
306 stat_list = self.fcn_list[:]
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
307 msg = " Ordered by: " + self.sort_type + '\n'
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
308 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
309 stat_list = list(self.stats.keys())
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
310 msg = " Random listing order was used\n"
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
311
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
312 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
313 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
314
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
315 count = len(stat_list)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
316
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 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
318 return 0, stat_list
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
319 print(msg, file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
320 if count < len(self.stats):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
321 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
322 for func in stat_list:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
323 if len(func_std_string(func)) > width:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
324 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
325 return width+2, stat_list
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
326
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
327 def print_stats(self, *amount):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
328 for filename in self.files:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
329 print(filename, file=self.stream)
65589
46bbbdcd777f Refactor interesting use of try-finally.
Georg Brandl <georg@python.org>
parents: 65588
diff changeset
330 if self.files:
46bbbdcd777f Refactor interesting use of try-finally.
Georg Brandl <georg@python.org>
parents: 65588
diff changeset
331 print(file=self.stream)
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
332 indent = ' ' * 8
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
333 for func in self.top_level:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
334 print(indent, func_get_function_name(func), file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
335
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
336 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
337 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
338 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
339 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
340 print(file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
341 width, list = self.get_print_list(amount)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
342 if list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
343 self.print_title()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
344 for func in list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
345 self.print_line(func)
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
346 print(file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
347 print(file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
348 return self
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
349
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
350 def print_callees(self, *amount):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
351 width, list = self.get_print_list(amount)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
352 if list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
353 self.calc_callees()
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
354
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
355 self.print_call_heading(width, "called...")
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
356 for func in list:
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
357 if func in self.all_callees:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
358 self.print_call_line(width, func, self.all_callees[func])
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
359 else:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
360 self.print_call_line(width, func, {})
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
361 print(file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
362 print(file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
363 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
364
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
365 def print_callers(self, *amount):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
366 width, list = self.get_print_list(amount)
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
367 if list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
368 self.print_call_heading(width, "was called by...")
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
369 for func in list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
370 cc, nc, tt, ct, callers = self.stats[func]
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
371 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
372 print(file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
373 print(file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
374 return self
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
375
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
376 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
377 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
378 # print sub-header only if we have new-style callers
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
379 subheader = False
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
380 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
381 if callers:
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 40809
diff changeset
382 value = next(iter(callers.values()))
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
383 subheader = isinstance(value, tuple)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
384 break
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
385 if subheader:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
386 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
387
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
388 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
389 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
390 if not call_dict:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
391 print(file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
392 return
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
393 clist = sorted(call_dict.keys())
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
394 indent = ""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
395 for func in clist:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
396 name = func_std_string(func)
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
397 value = call_dict[func]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
398 if isinstance(value, tuple):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
399 nc, cc, tt, ct = value
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
400 if nc != cc:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
401 substats = '%d/%d' % (nc, cc)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
402 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
403 substats = '%d' % (nc,)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
404 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
405 f8(tt), f8(ct), name)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
406 left_width = name_size + 1
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
407 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
408 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
409 left_width = name_size + 3
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
410 print(indent*left_width + substats, file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
411 indent = " "
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
412
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
413 def print_title(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
414 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
415 print('filename:lineno(function)', file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
416
65589
46bbbdcd777f Refactor interesting use of try-finally.
Georg Brandl <georg@python.org>
parents: 65588
diff changeset
417 def print_line(self, func): # hack: should print percentages
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
418 cc, nc, tt, ct, callers = self.stats[func]
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
419 c = str(nc)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
420 if nc != cc:
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
421 c = c + '/' + str(cc)
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
422 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
423 print(f8(tt), end=' ', file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
424 if nc == 0:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
425 print(' '*8, end=' ', file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
426 else:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
427 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
428 print(f8(ct), end=' ', file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
429 if cc == 0:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
430 print(' '*8, end=' ', file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
431 else:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
432 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
433 print(func_std_string(func), file=self.stream)
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
434
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 class TupleComp:
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
436 """This class provides a generic function for comparing any two tuples.
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
437 Each instance records a list of tuple-indices (from most significant
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
438 to least significant), and sort direction (ascending or decending) for
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
439 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
440 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
441 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
442
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
443 def __init__(self, comp_select_list):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
444 self.comp_select_list = comp_select_list
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
445
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
446 def compare (self, left, right):
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
447 for index, direction in self.comp_select_list:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
448 l = left[index]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
449 r = right[index]
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
450 if l < r:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
451 return -direction
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
452 if l > r:
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
453 return direction
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
454 return 0
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
455
45061
b9ee31a26f38 Issue #1771: Remove cmp parameter from list.sort() and builtin.sorted().
Raymond Hettinger <python@rcn.com>
parents: 44834
diff changeset
456
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 #**************************************************************************
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
458 # 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
459
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 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
461 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
462 return os.path.basename(filename), line, name
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_get_function_name(func):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
465 return func[2]
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
466
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 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
468 if func_name[:2] == ('~', 0):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
469 # special case for built-in functions
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
470 name = func_name[2]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
471 if name.startswith('<') and name.endswith('>'):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
472 return '{%s}' % name[1:-1]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
473 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
474 return name
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
475 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents: 34097
diff changeset
476 return "%s:%d(%s)" % func_name
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
477
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 #**************************************************************************
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
479 # The following functions combine statists for pairs functions.
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 # The bulk of the processing involves correctly handling "call" lists,
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
481 # such as callers and callees.
1679
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
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 10581
diff changeset
484 def add_func_stats(target, source):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
485 """Add together all the stats for two profile entries."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
486 cc, nc, tt, ct, callers = source
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
487 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
488 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
489 add_callers(t_callers, callers))
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
490
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 def add_callers(target, source):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
492 """Combine two caller lists in a single list."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
493 new_callers = {}
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
494 for func, caller in target.items():
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
495 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
496 for func, caller in source.items():
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20309
diff changeset
497 if func in new_callers:
63550
3068e00a3207 Merged revisions 83531 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63441
diff changeset
498 if isinstance(caller, tuple):
3068e00a3207 Merged revisions 83531 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63441
diff changeset
499 # format used by cProfile
3068e00a3207 Merged revisions 83531 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63441
diff changeset
500 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
501 zip(caller, new_callers[func])])
3068e00a3207 Merged revisions 83531 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63441
diff changeset
502 else:
3068e00a3207 Merged revisions 83531 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63441
diff changeset
503 # format used by profile
3068e00a3207 Merged revisions 83531 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63441
diff changeset
504 new_callers[func] += caller
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
505 else:
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
506 new_callers[func] = caller
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
507 return new_callers
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
508
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 def count_calls(callers):
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
510 """Sum the caller statistics to get total number of calls received."""
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
511 nc = 0
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
512 for calls in callers.values():
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
513 nc += calls
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 13620
diff changeset
514 return nc
1679
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
515
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 #**************************************************************************
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 # The following functions support printing of reports
5290428bad37 Companion module for new profile.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 #**************************************************************************
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 def f8(x):
20308
ca2bc5cdaa8e Widespread random code cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 19216
diff changeset
521 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
522
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
523 #**************************************************************************
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
524 # 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
525 #**************************************************************************
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 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
528 import cmd
17510
fd0683cb7301 Import readline when possible to make the commaninterpreter UI nicer.
Eric S. Raymond <esr@thyrsus.com>
parents: 17467
diff changeset
529 try:
fd0683cb7301 Import readline when possible to make the commaninterpreter UI nicer.
Eric S. Raymond <esr@thyrsus.com>
parents: 17467
diff changeset
530 import readline
17800
c19980c03e2d When guarding an import, only catch ImportError.
Fred Drake <fdrake@acm.org>
parents: 17634
diff changeset
531 except ImportError:
17510
fd0683cb7301 Import readline when possible to make the commaninterpreter UI nicer.
Eric S. Raymond <esr@thyrsus.com>
parents: 17467
diff changeset
532 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
533
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
534 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
535 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
536 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
537 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
538 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
539 self.stream = sys.stdout
23622
5edd80726d96 Replace boolean test with is None.
Raymond Hettinger <python@rcn.com>
parents: 23620
diff changeset
540 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
541 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
542
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
543 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
544 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
545 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
546 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
547 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
548 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
549 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
550 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
551 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
552 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
553 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
554 if frac > 1 or frac < 0:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
555 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
556 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
557 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
558 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
559 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
560 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
561 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
562 if self.stats:
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 23634
diff changeset
563 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
564 else:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
565 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
566 return 0
17634
0bc3377d0d72 Added more help, and recovery from misspelled sort key arguments.
Eric S. Raymond <esr@thyrsus.com>
parents: 17514
diff changeset
567 def generic_help(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
568 print("Arguments may be:", file=self.stream)
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
569 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
570 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
571 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
572 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
573 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
574
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
575 def do_add(self, line):
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
576 if self.stats:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
577 self.stats.add(line)
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
578 else:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
579 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
580 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
581 def help_add(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
582 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
583
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 def do_callees(self, line):
17514
773c64d38ac6 Should resolve [ #416039 ] pstats browser crashes.
Eric S. Raymond <esr@thyrsus.com>
parents: 17510
diff changeset
585 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
586 def help_callees(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
587 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
588 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
589
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 do_callers(self, line):
17514
773c64d38ac6 Should resolve [ #416039 ] pstats browser crashes.
Eric S. Raymond <esr@thyrsus.com>
parents: 17510
diff changeset
591 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
592 def help_callers(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
593 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
594 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
595
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 do_EOF(self, line):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
597 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
598 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
599 def help_EOF(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
600 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
601
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 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
603 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
604 def help_quit(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
605 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
606
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 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
608 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
609 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
610 self.stats = Stats(line)
44543
674c9b89d124 The final occurrences of exception slicing.
Georg Brandl <georg@python.org>
parents: 43577
diff changeset
611 except IOError as err:
674c9b89d124 The final occurrences of exception slicing.
Georg Brandl <georg@python.org>
parents: 43577
diff changeset
612 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
613 return
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
614 except Exception as err:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
615 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
616 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
617 self.prompt = line + "% "
18048
14dc75a5600a Patch #416220: Fix misplaced paren.
Martin v. Löwis <martin@v.loewis.de>
parents: 17800
diff changeset
618 elif len(self.prompt) > 2:
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
619 line = self.prompt[:-2]
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
620 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
621 else:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
622 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
623 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
624 def help_read(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
625 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
626 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
627
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 do_reverse(self, line):
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
629 if self.stats:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
630 self.stats.reverse_order()
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
631 else:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
632 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
633 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
634 def help_reverse(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
635 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
636
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 def do_sort(self, line):
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
638 if not self.stats:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
639 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
640 return
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
641 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
642 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
643 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
644 else:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
645 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
646 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
647 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
648 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
649 def help_sort(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
650 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
651 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
652 def complete_sort(self, text, *args):
23634
dbee2f628a90 Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents: 23625
diff changeset
653 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
654
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
655 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
656 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
657 def help_stats(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
658 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
659 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
660
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 do_strip(self, line):
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
662 if self.stats:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
663 self.stats.strip_dirs()
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
664 else:
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
665 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
666 def help_strip(self):
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
667 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
668
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
669 def help_help(self):
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 45061
diff changeset
670 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
671
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 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
673 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
674 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
675 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
676
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 import sys
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 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
679 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
680 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
681 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
682 try:
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
683 browser = ProfileBrowser(initprofile)
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
684 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
685 browser.cmdloop()
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
686 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
687 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
688 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
689
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
690 # That's all, folks.