summaryrefslogtreecommitdiffstats
path: root/Lib/cProfile.py
diff options
context:
space:
mode:
authorFlorent Xicluna <florent.xicluna@gmail.com>2010-09-13 17:36:36 (GMT)
committerFlorent Xicluna <florent.xicluna@gmail.com>2010-09-13 17:36:36 (GMT)
commit928474561c736d87787c99d1c7198ab062318baa (patch)
tree3b8a91dfadff204052d542266762a948a9a385c4 /Lib/cProfile.py
parent9d407ca9da87a7e75b8bfe6062850e8975eca529 (diff)
downloadcpython-928474561c736d87787c99d1c7198ab062318baa.zip
cpython-928474561c736d87787c99d1c7198ab062318baa.tar.gz
cpython-928474561c736d87787c99d1c7198ab062318baa.tar.bz2
Merged revisions 83524,84776 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r83524 | georg.brandl | 2010-08-02 14:20:23 +0200 (lun., 02 août 2010) | 1 line #9428: fix running scripts from profile/cProfile with their own name and the right namespace. Same fix as for trace.py in #1690103. ........ r84776 | florent.xicluna | 2010-09-13 18:35:02 +0200 (lun., 13 sept. 2010) | 1 line Make test.regrtest.__file__ absolute, this was not always the case when running profile or trace, for example. (issue #9323) ........
Diffstat (limited to 'Lib/cProfile.py')
-rwxr-xr-xLib/cProfile.py21
1 files changed, 15 insertions, 6 deletions
diff --git a/Lib/cProfile.py b/Lib/cProfile.py
index 19d5804..b2efd04 100755
--- a/Lib/cProfile.py
+++ b/Lib/cProfile.py
@@ -36,7 +36,7 @@ def run(statement, filename=None, sort=-1):
result = prof.print_stats(sort)
return result
-def runctx(statement, globals, locals, filename=None):
+def runctx(statement, globals, locals, filename=None, sort=-1):
"""Run statement under profiler, supplying your own globals and locals,
optionally saving results in filename.
@@ -53,7 +53,7 @@ def runctx(statement, globals, locals, filename=None):
if filename is not None:
prof.dump_stats(filename)
else:
- result = prof.print_stats()
+ result = prof.print_stats(sort)
return result
# Backwards compatibility.
@@ -169,7 +169,8 @@ def main():
parser.add_option('-o', '--outfile', dest="outfile",
help="Save stats to <outfile>", default=None)
parser.add_option('-s', '--sort', dest="sort",
- help="Sort order when printing to stdout, based on pstats.Stats class", default=-1)
+ help="Sort order when printing to stdout, based on pstats.Stats class",
+ default=-1)
if not sys.argv[1:]:
parser.print_usage()
@@ -178,9 +179,17 @@ def main():
(options, args) = parser.parse_args()
sys.argv[:] = args
- if (len(sys.argv) > 0):
- sys.path.insert(0, os.path.dirname(sys.argv[0]))
- run('execfile(%r)' % (sys.argv[0],), options.outfile, options.sort)
+ if len(args) > 0:
+ progname = args[0]
+ sys.path.insert(0, os.path.dirname(progname))
+ with open(progname, 'rb') as fp:
+ code = compile(fp.read(), progname, 'exec')
+ globs = {
+ '__file__': progname,
+ '__name__': '__main__',
+ '__package__': None,
+ }
+ runctx(code, globs, None, options.outfile, options.sort)
else:
parser.print_usage()
return parser