Kaydet (Commit) 8e43fbff authored tarafından Georg Brandl's avatar Georg Brandl

#9428: fix running scripts from profile/cProfile with their own name and the…

#9428: fix running scripts from profile/cProfile with their own name and the right namespace.  Same fix as for trace.py in #1690103.
üst b1a97afa
...@@ -36,7 +36,7 @@ def run(statement, filename=None, sort=-1): ...@@ -36,7 +36,7 @@ def run(statement, filename=None, sort=-1):
result = prof.print_stats(sort) result = prof.print_stats(sort)
return result 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, """Run statement under profiler, supplying your own globals and locals,
optionally saving results in filename. optionally saving results in filename.
...@@ -53,7 +53,7 @@ def runctx(statement, globals, locals, filename=None): ...@@ -53,7 +53,7 @@ def runctx(statement, globals, locals, filename=None):
if filename is not None: if filename is not None:
prof.dump_stats(filename) prof.dump_stats(filename)
else: else:
result = prof.print_stats() result = prof.print_stats(sort)
return result return result
# ____________________________________________________________ # ____________________________________________________________
...@@ -164,7 +164,8 @@ def main(): ...@@ -164,7 +164,8 @@ def main():
parser.add_option('-o', '--outfile', dest="outfile", parser.add_option('-o', '--outfile', dest="outfile",
help="Save stats to <outfile>", default=None) help="Save stats to <outfile>", default=None)
parser.add_option('-s', '--sort', dest="sort", 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:]: if not sys.argv[1:]:
parser.print_usage() parser.print_usage()
...@@ -173,14 +174,18 @@ def main(): ...@@ -173,14 +174,18 @@ def main():
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
sys.argv[:] = args sys.argv[:] = args
if (len(sys.argv) > 0): if len(args) > 0:
sys.path.insert(0, os.path.dirname(sys.argv[0])) progname = args[0]
fp = open(sys.argv[0]) sys.path.insert(0, os.path.dirname(progname))
try: with open(progname, 'rb') as fp:
script = fp.read() code = compile(fp.read(), progname, 'exec')
finally: globs = {
fp.close() '__file__': progname,
run('exec(%r)' % script, options.outfile, options.sort) '__name__': '__main__',
'__package__': None,
'__cached__': None,
}
runctx(code, globs, None, options.outfile, options.sort)
else: else:
parser.print_usage() parser.print_usage()
return parser return parser
......
...@@ -75,7 +75,7 @@ def run(statement, filename=None, sort=-1): ...@@ -75,7 +75,7 @@ def run(statement, filename=None, sort=-1):
else: else:
return prof.print_stats(sort) 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, """Run statement under profiler, supplying your own globals and locals,
optionally saving results in filename. optionally saving results in filename.
...@@ -90,7 +90,7 @@ def runctx(statement, globals, locals, filename=None): ...@@ -90,7 +90,7 @@ def runctx(statement, globals, locals, filename=None):
if filename is not None: if filename is not None:
prof.dump_stats(filename) prof.dump_stats(filename)
else: else:
return prof.print_stats() return prof.print_stats(sort)
if hasattr(os, "times"): if hasattr(os, "times"):
def _get_time_times(timer=os.times): def _get_time_times(timer=os.times):
...@@ -582,20 +582,28 @@ def main(): ...@@ -582,20 +582,28 @@ def main():
parser.add_option('-o', '--outfile', dest="outfile", parser.add_option('-o', '--outfile', dest="outfile",
help="Save stats to <outfile>", default=None) help="Save stats to <outfile>", default=None)
parser.add_option('-s', '--sort', dest="sort", 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:]: if not sys.argv[1:]:
parser.print_usage() parser.print_usage()
sys.exit(2) sys.exit(2)
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
sys.argv[:] = args
if (len(args) > 0):
sys.argv[:] = args if len(args) > 0:
sys.path.insert(0, os.path.dirname(sys.argv[0])) progname = args[0]
with open(sys.argv[0], 'rb') as fp: sys.path.insert(0, os.path.dirname(progname))
script = fp.read() with open(progname, 'rb') as fp:
run('exec(%r)' % script, options.outfile, options.sort) code = compile(fp.read(), progname, 'exec')
globs = {
'__file__': progname,
'__name__': '__main__',
'__package__': None,
'__cached__': None,
}
runctx(code, globs, None, options.outfile, options.sort)
else: else:
parser.print_usage() parser.print_usage()
return parser return parser
......
...@@ -29,6 +29,9 @@ Extensions ...@@ -29,6 +29,9 @@ Extensions
Library Library
------- -------
- Issue #9428: Fix running scripts with the profile/cProfile modules from
the command line.
- Issue #7781: Fix restricting stats by entry counts in the pstats - Issue #7781: Fix restricting stats by entry counts in the pstats
interactive browser. interactive browser.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment