diff options
author | Sanyam Khurana <8039608+CuriousLearner@users.noreply.github.com> | 2017-11-08 10:50:56 (GMT) |
---|---|---|
committer | Antoine Pitrou <pitrou@free.fr> | 2017-11-08 10:50:56 (GMT) |
commit | 7973e279a21999f134aff92dd6d344ec4591fae9 (patch) | |
tree | f8007dd3dac2f7cbf905f45ccd148cf4b5a1fd9a /Lib/cProfile.py | |
parent | 4fc4defd1c9bd667635ba4080404e7aa3fcd49ea (diff) | |
download | cpython-7973e279a21999f134aff92dd6d344ec4591fae9.zip cpython-7973e279a21999f134aff92dd6d344ec4591fae9.tar.gz cpython-7973e279a21999f134aff92dd6d344ec4591fae9.tar.bz2 |
bpo-21862: Add -m option to cProfile for profiling modules (#4297)
* bpo-21862: Add -m option to cProfile for profiling modules
Diffstat (limited to 'Lib/cProfile.py')
-rwxr-xr-x | Lib/cProfile.py | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/Lib/cProfile.py b/Lib/cProfile.py index 1184385..f166a1c 100755 --- a/Lib/cProfile.py +++ b/Lib/cProfile.py @@ -121,9 +121,11 @@ def label(code): # ____________________________________________________________ def main(): - import os, sys + import os + import sys + import runpy from optparse import OptionParser - usage = "cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ..." + usage = "cProfile.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", @@ -131,6 +133,8 @@ def main(): parser.add_option('-s', '--sort', dest="sort", help="Sort order when printing to stdout, based on pstats.Stats class", default=-1) + parser.add_option('-m', dest="module", action="store_true", + help="Profile a library module", default=False) if not sys.argv[1:]: parser.print_usage() @@ -140,16 +144,23 @@ 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: + 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() |