diff -r 5d70ac83d104 Lib/cProfile.py --- a/Lib/cProfile.py Thu Jul 24 12:44:07 2014 +0200 +++ b/Lib/cProfile.py Sat Jul 26 02:06:40 2014 +0400 @@ -8,6 +8,7 @@ import _lsprof import profile as _pyprofile +import runpy # ____________________________________________________________ # Simple interface @@ -123,7 +124,7 @@ def main(): import os, sys 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,16 +132,23 @@ 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', '--module', dest='module', + help="Module name to profile. Can be used as alternative to script path", + default=None) - if not sys.argv[1:]: + (options, args) = parser.parse_args() + + if not sys.argv[1:] and not options.module or (args and options.module): parser.print_usage() sys.exit(2) - (options, args) = parser.parse_args() sys.argv[:] = args - if len(args) > 0: - progname = args[0] + if len(args) > 0 or options.module: + if options.module: + progname = runpy.run_module(options.module)['__file__'] + else: + progname = args[0] sys.path.insert(0, os.path.dirname(progname)) with open(progname, 'rb') as fp: code = compile(fp.read(), progname, 'exec')