summaryrefslogtreecommitdiffstats
path: root/Lib/profile.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/profile.py')
-rwxr-xr-xLib/profile.py32
1 files changed, 21 insertions, 11 deletions
diff --git a/Lib/profile.py b/Lib/profile.py
index 0340a79..5df4360 100755
--- a/Lib/profile.py
+++ b/Lib/profile.py
@@ -553,11 +553,13 @@ def main():
import os
from optparse import OptionParser
- usage = "profile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
+ usage = "profile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..."
parser = OptionParser(usage=usage)
parser.allow_interspersed_args = False
parser.add_option('-o', '--outfile', dest="outfile",
help="Save stats to <outfile>", default=None)
+ parser.add_option('-m', dest="module", action="store_true",
+ help="Profile a library module.", default=False)
parser.add_option('-s', '--sort', dest="sort",
help="Sort order when printing to stdout, based on pstats.Stats class",
default=-1)
@@ -570,16 +572,24 @@ def main():
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,
- }
+ if options.module:
+ import runpy
+ code = "run_module(modname, run_name='__main__')"
+ globs = {
+ 'run_module': runpy.run_module,
+ 'modname': args[0]
+ }
+ else:
+ 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()