annotate Lib/cProfile.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 2ad8d7cbceab
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
1 #! /usr/bin/env python
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
2
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
3 """Python interface for the 'lsprof' profiler.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
4 Compatible with the 'profile' module.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
5 """
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
6
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
7 __all__ = ["run", "runctx", "help", "Profile"]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
8
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
9 import _lsprof
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
10
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
11 # ____________________________________________________________
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
12 # Simple interface
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
13
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
14 def run(statement, filename=None, sort=-1):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
15 """Run statement under profiler optionally saving results in filename
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
16
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
17 This function takes a single argument that can be passed to the
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
18 "exec" statement, and an optional file name. In all cases this
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
19 routine attempts to "exec" its first argument and gather profiling
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
20 statistics from the execution. If no file name is present, then this
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
21 function automatically prints a simple profiling report, sorted by the
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
22 standard name string (file/line/function-name) that is presented in
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
23 each line.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
24 """
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
25 prof = Profile()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
26 result = None
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
27 try:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
28 try:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
29 prof = prof.run(statement)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
30 except SystemExit:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
31 pass
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
32 finally:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
33 if filename is not None:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
34 prof.dump_stats(filename)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
35 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
36 result = prof.print_stats(sort)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
37 return result
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
38
64713
2ad8d7cbceab Merged revisions 83524,84776 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 36459
diff changeset
39 def runctx(statement, globals, locals, filename=None, sort=-1):
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
40 """Run statement under profiler, supplying your own globals and locals,
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
41 optionally saving results in filename.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
42
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
43 statement and filename have the same semantics as profile.run
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
44 """
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
45 prof = Profile()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
46 result = None
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
47 try:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
48 try:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
49 prof = prof.runctx(statement, globals, locals)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
50 except SystemExit:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
51 pass
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
52 finally:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
53 if filename is not None:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
54 prof.dump_stats(filename)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
55 else:
64713
2ad8d7cbceab Merged revisions 83524,84776 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 36459
diff changeset
56 result = prof.print_stats(sort)
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
57 return result
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
58
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
59 # Backwards compatibility.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
60 def help():
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
61 print "Documentation for the profile/cProfile modules can be found "
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
62 print "in the Python Library Reference, section 'The Python Profiler'."
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
63
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
64 # ____________________________________________________________
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
65
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
66 class Profile(_lsprof.Profiler):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
67 """Profile(custom_timer=None, time_unit=None, subcalls=True, builtins=True)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
68
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
69 Builds a profiler object using the specified timer function.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
70 The default timer is a fast built-in one based on real time.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
71 For custom timer functions returning integers, time_unit can
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
72 be a float specifying a scale (i.e. how long each integer unit
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
73 is, in seconds).
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
74 """
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
75
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
76 # Most of the functionality is in the base class.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
77 # This subclass only adds convenient and backward-compatible methods.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
78
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
79 def print_stats(self, sort=-1):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
80 import pstats
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
81 pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
82
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
83 def dump_stats(self, file):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
84 import marshal
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
85 f = open(file, 'wb')
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
86 self.create_stats()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
87 marshal.dump(self.stats, f)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
88 f.close()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
89
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
90 def create_stats(self):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
91 self.disable()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
92 self.snapshot_stats()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
93
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
94 def snapshot_stats(self):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
95 entries = self.getstats()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
96 self.stats = {}
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
97 callersdicts = {}
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
98 # call information
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
99 for entry in entries:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
100 func = label(entry.code)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
101 nc = entry.callcount # ncalls column of pstats (before '/')
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
102 cc = nc - entry.reccallcount # ncalls column of pstats (after '/')
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
103 tt = entry.inlinetime # tottime column of pstats
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
104 ct = entry.totaltime # cumtime column of pstats
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
105 callers = {}
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
106 callersdicts[id(entry.code)] = callers
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
107 self.stats[func] = cc, nc, tt, ct, callers
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
108 # subcall information
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
109 for entry in entries:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
110 if entry.calls:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
111 func = label(entry.code)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
112 for subentry in entry.calls:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
113 try:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
114 callers = callersdicts[id(subentry.code)]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
115 except KeyError:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
116 continue
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
117 nc = subentry.callcount
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
118 cc = nc - subentry.reccallcount
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
119 tt = subentry.inlinetime
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
120 ct = subentry.totaltime
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
121 if func in callers:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
122 prev = callers[func]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
123 nc += prev[0]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
124 cc += prev[1]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
125 tt += prev[2]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
126 ct += prev[3]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
127 callers[func] = nc, cc, tt, ct
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
128
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
129 # The following two methods can be called by clients to use
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
130 # a profiler to profile a statement, given as a string.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
131
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
132 def run(self, cmd):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
133 import __main__
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
134 dict = __main__.__dict__
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
135 return self.runctx(cmd, dict, dict)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
136
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
137 def runctx(self, cmd, globals, locals):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
138 self.enable()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
139 try:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
140 exec cmd in globals, locals
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
141 finally:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
142 self.disable()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
143 return self
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
144
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
145 # This method is more useful to profile a single function call.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
146 def runcall(self, func, *args, **kw):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
147 self.enable()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
148 try:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
149 return func(*args, **kw)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
150 finally:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
151 self.disable()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
152
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
153 # ____________________________________________________________
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
154
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
155 def label(code):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
156 if isinstance(code, str):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
157 return ('~', 0, code) # built-in functions ('~' sorts at the end)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
158 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
159 return (code.co_filename, code.co_firstlineno, code.co_name)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
160
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
161 # ____________________________________________________________
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
162
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
163 def main():
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
164 import os, sys
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
165 from optparse import OptionParser
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
166 usage = "cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
167 parser = OptionParser(usage=usage)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
168 parser.allow_interspersed_args = False
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
169 parser.add_option('-o', '--outfile', dest="outfile",
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
170 help="Save stats to <outfile>", default=None)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
171 parser.add_option('-s', '--sort', dest="sort",
64713
2ad8d7cbceab Merged revisions 83524,84776 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 36459
diff changeset
172 help="Sort order when printing to stdout, based on pstats.Stats class",
2ad8d7cbceab Merged revisions 83524,84776 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 36459
diff changeset
173 default=-1)
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
174
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
175 if not sys.argv[1:]:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
176 parser.print_usage()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
177 sys.exit(2)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
178
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
179 (options, args) = parser.parse_args()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
180 sys.argv[:] = args
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
181
64713
2ad8d7cbceab Merged revisions 83524,84776 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 36459
diff changeset
182 if len(args) > 0:
2ad8d7cbceab Merged revisions 83524,84776 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 36459
diff changeset
183 progname = args[0]
2ad8d7cbceab Merged revisions 83524,84776 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 36459
diff changeset
184 sys.path.insert(0, os.path.dirname(progname))
2ad8d7cbceab Merged revisions 83524,84776 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 36459
diff changeset
185 with open(progname, 'rb') as fp:
2ad8d7cbceab Merged revisions 83524,84776 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 36459
diff changeset
186 code = compile(fp.read(), progname, 'exec')
2ad8d7cbceab Merged revisions 83524,84776 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 36459
diff changeset
187 globs = {
2ad8d7cbceab Merged revisions 83524,84776 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 36459
diff changeset
188 '__file__': progname,
2ad8d7cbceab Merged revisions 83524,84776 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 36459
diff changeset
189 '__name__': '__main__',
2ad8d7cbceab Merged revisions 83524,84776 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 36459
diff changeset
190 '__package__': None,
2ad8d7cbceab Merged revisions 83524,84776 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 36459
diff changeset
191 }
2ad8d7cbceab Merged revisions 83524,84776 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 36459
diff changeset
192 runctx(code, globs, None, options.outfile, options.sort)
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
193 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
194 parser.print_usage()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
195 return parser
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
196
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
197 # When invoked as main program, invoke the profiler on a script
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
198 if __name__ == '__main__':
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
199 main()