annotate Lib/cProfile.py @ 79691:05c8928362ee

Merge __next__ method link fixes with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 12 Oct 2012 13:46:37 +0300
parents 3c9232a684ae
children cb876235f29d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59522
fcfe324663a6 convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents: 55833
diff changeset
1 #! /usr/bin/env python3
36459
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
55833
4ddc0598df79 Remove the just-removed "help" from __all__.
Georg Brandl <georg@python.org>
parents: 55818
diff changeset
7 __all__ = ["run", "runctx", "Profile"]
36459
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
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: 43577
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:
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: 43577
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 # ____________________________________________________________
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
60
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
61 class Profile(_lsprof.Profiler):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
62 """Profile(custom_timer=None, time_unit=None, subcalls=True, builtins=True)
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 Builds a profiler object using the specified timer function.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
65 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
66 For custom timer functions returning integers, time_unit can
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
67 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
68 is, in seconds).
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
69 """
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
70
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
71 # Most of the functionality is in the base class.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
72 # This subclass only adds convenient and backward-compatible methods.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
73
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
74 def print_stats(self, sort=-1):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
75 import pstats
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
76 pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
77
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
78 def dump_stats(self, file):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
79 import marshal
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
80 f = open(file, 'wb')
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
81 self.create_stats()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
82 marshal.dump(self.stats, f)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
83 f.close()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
84
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
85 def create_stats(self):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
86 self.disable()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
87 self.snapshot_stats()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
88
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
89 def snapshot_stats(self):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
90 entries = self.getstats()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
91 self.stats = {}
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
92 callersdicts = {}
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
93 # call information
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
94 for entry in entries:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
95 func = label(entry.code)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
96 nc = entry.callcount # ncalls column of pstats (before '/')
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
97 cc = nc - entry.reccallcount # ncalls column of pstats (after '/')
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
98 tt = entry.inlinetime # tottime column of pstats
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
99 ct = entry.totaltime # cumtime column of pstats
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
100 callers = {}
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
101 callersdicts[id(entry.code)] = callers
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
102 self.stats[func] = cc, nc, tt, ct, callers
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
103 # subcall information
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
104 for entry in entries:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
105 if entry.calls:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
106 func = label(entry.code)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
107 for subentry in entry.calls:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
108 try:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
109 callers = callersdicts[id(subentry.code)]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
110 except KeyError:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
111 continue
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
112 nc = subentry.callcount
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
113 cc = nc - subentry.reccallcount
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
114 tt = subentry.inlinetime
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
115 ct = subentry.totaltime
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
116 if func in callers:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
117 prev = callers[func]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
118 nc += prev[0]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
119 cc += prev[1]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
120 tt += prev[2]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
121 ct += prev[3]
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
122 callers[func] = nc, cc, tt, ct
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
123
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
124 # The following two methods can be called by clients to use
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
125 # a profiler to profile a statement, given as a string.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
126
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
127 def run(self, cmd):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
128 import __main__
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
129 dict = __main__.__dict__
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
130 return self.runctx(cmd, dict, dict)
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 runctx(self, cmd, globals, locals):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
133 self.enable()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
134 try:
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 36459
diff changeset
135 exec(cmd, globals, locals)
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
136 finally:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
137 self.disable()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
138 return self
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
139
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
140 # This method is more useful to profile a single function call.
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
141 def runcall(self, func, *args, **kw):
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
142 self.enable()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
143 try:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
144 return func(*args, **kw)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
145 finally:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
146 self.disable()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
147
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
148 # ____________________________________________________________
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
149
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
150 def label(code):
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 42552
diff changeset
151 if isinstance(code, str):
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
152 return ('~', 0, code) # built-in functions ('~' sorts at the end)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
153 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
154 return (code.co_filename, code.co_firstlineno, code.co_name)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
155
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
156 # ____________________________________________________________
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
157
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
158 def main():
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
159 import os, sys
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
160 from optparse import OptionParser
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
161 usage = "cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
162 parser = OptionParser(usage=usage)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
163 parser.allow_interspersed_args = False
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
164 parser.add_option('-o', '--outfile', dest="outfile",
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
165 help="Save stats to <outfile>", default=None)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
166 parser.add_option('-s', '--sort', dest="sort",
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: 43577
diff changeset
167 help="Sort order when printing to stdout, based on pstats.Stats class",
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: 43577
diff changeset
168 default=-1)
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
169
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
170 if not sys.argv[1:]:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
171 parser.print_usage()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
172 sys.exit(2)
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
173
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
174 (options, args) = parser.parse_args()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
175 sys.argv[:] = args
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
176
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: 43577
diff changeset
177 if len(args) > 0:
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: 43577
diff changeset
178 progname = args[0]
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: 43577
diff changeset
179 sys.path.insert(0, os.path.dirname(progname))
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: 43577
diff changeset
180 with open(progname, 'rb') as fp:
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: 43577
diff changeset
181 code = compile(fp.read(), progname, 'exec')
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: 43577
diff changeset
182 globs = {
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: 43577
diff changeset
183 '__file__': progname,
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: 43577
diff changeset
184 '__name__': '__main__',
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: 43577
diff changeset
185 '__package__': 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: 43577
diff changeset
186 '__cached__': 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: 43577
diff changeset
187 }
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: 43577
diff changeset
188 runctx(code, globs, None, options.outfile, options.sort)
36459
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
189 else:
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
190 parser.print_usage()
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
191 return parser
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
192
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
193 # When invoked as main program, invoke the profiler on a script
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
194 if __name__ == '__main__':
3abf33264146 Added the cProfile module.
Armin Rigo <arigo@tunes.org>
parents:
diff changeset
195 main()