diff options
author | Georg Brandl <georg@python.org> | 2010-08-02 12:20:23 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-08-02 12:20:23 (GMT) |
commit | 8e43fbfffa38b36e907bb847f5d09b3a9bc1bcb4 (patch) | |
tree | 6b57a091cf25070ed6b98e97c425ac01eb8536bd /Lib/profile.py | |
parent | b1a97afadb5d6100613c19409882eba0be991b41 (diff) | |
download | cpython-8e43fbfffa38b36e907bb847f5d09b3a9bc1bcb4.zip cpython-8e43fbfffa38b36e907bb847f5d09b3a9bc1bcb4.tar.gz cpython-8e43fbfffa38b36e907bb847f5d09b3a9bc1bcb4.tar.bz2 |
#9428: fix running scripts from profile/cProfile with their own name and the right namespace. Same fix as for trace.py in #1690103.
Diffstat (limited to 'Lib/profile.py')
-rwxr-xr-x | Lib/profile.py | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/Lib/profile.py b/Lib/profile.py index 0255bba..d6511b71 100755 --- a/Lib/profile.py +++ b/Lib/profile.py @@ -75,7 +75,7 @@ def run(statement, filename=None, sort=-1): else: return prof.print_stats(sort) -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. @@ -90,7 +90,7 @@ def runctx(statement, globals, locals, filename=None): if filename is not None: prof.dump_stats(filename) else: - return prof.print_stats() + return prof.print_stats(sort) if hasattr(os, "times"): def _get_time_times(timer=os.times): @@ -582,20 +582,28 @@ 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() sys.exit(2) (options, args) = parser.parse_args() - - if (len(args) > 0): - sys.argv[:] = args - sys.path.insert(0, os.path.dirname(sys.argv[0])) - with open(sys.argv[0], 'rb') as fp: - script = fp.read() - run('exec(%r)' % script, options.outfile, options.sort) + sys.argv[:] = args + + 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, + '__cached__': None, + } + runctx(code, globs, None, options.outfile, options.sort) else: parser.print_usage() return parser |