diff -r 69285bf7e865 Lib/cProfile.py --- a/Lib/cProfile.py Thu Jan 15 09:44:24 2015 +0100 +++ b/Lib/cProfile.py Sun Jan 18 01:08:02 2015 +0530 @@ -122,8 +122,10 @@ def main(): import os, 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 @@ 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", action="store_true", + help="Profile a library module", default=False) if not sys.argv[1:]: parser.print_usage() @@ -140,7 +144,10 @@ sys.argv[:] = args if len(args) > 0: - progname = args[0] + if options.module: + progname = runpy.run_module(args[0])['__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')