diff options
author | Mario Corchero <mariocj89@gmail.com> | 2018-11-05 12:03:46 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2018-11-05 12:03:46 (GMT) |
commit | ad1a25f499362eaf9cbfcafa0b8e2454eb43dcf1 (patch) | |
tree | 2d5abe6b5b1f3bf01254dce0d1504930072a3d9b /Lib | |
parent | 2810dd7be9876236f74ac80716d113572c9098dd (diff) | |
download | cpython-ad1a25f499362eaf9cbfcafa0b8e2454eb43dcf1.zip cpython-ad1a25f499362eaf9cbfcafa0b8e2454eb43dcf1.tar.gz cpython-ad1a25f499362eaf9cbfcafa0b8e2454eb43dcf1.tar.bz2 |
bpo-32512: Add -m option to profile for profiling modules (#5132)
The new option in the CLI of the profile module allow to profile
executable modules. This change follows the same implementation as the
one already present in `cProfile`.
As the argument is now present on both modules, move the tests to the
common test case to be run with profile as well.
Diffstat (limited to 'Lib')
-rwxr-xr-x | Lib/profile.py | 32 | ||||
-rw-r--r-- | Lib/test/test_cprofile.py | 13 | ||||
-rw-r--r-- | Lib/test/test_profile.py | 13 |
3 files changed, 34 insertions, 24 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() diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py index 406d703..efcf6bc 100644 --- a/Lib/test/test_cprofile.py +++ b/Lib/test/test_cprofile.py @@ -37,19 +37,6 @@ class CProfileTest(ProfileTest): finally: unlink(TESTFN) - # Issue 21862 - def test_module_path_option(self): - # Test -m switch with modules - - # Test that -m switch needs an argument - assert_python_failure('-m', 'cProfile', '-m') - - # Test failure for not-existent module - assert_python_failure('-m', 'cProfile', '-m', 'random_module_xyz') - - # Test successful run - assert_python_ok('-m', 'cProfile', '-m', 'timeit', '-n', '1') - def test_profile_enable_disable(self): prof = self.profilerclass() # Make sure we clean ourselves up if the test fails for some reason. diff --git a/Lib/test/test_profile.py b/Lib/test/test_profile.py index a998266..01a8a6e 100644 --- a/Lib/test/test_profile.py +++ b/Lib/test/test_profile.py @@ -11,6 +11,7 @@ from contextlib import contextmanager import profile from test.profilee import testfunc, timer +from test.support.script_helper import assert_python_failure, assert_python_ok class ProfileTest(unittest.TestCase): @@ -98,6 +99,18 @@ class ProfileTest(unittest.TestCase): filename=TESTFN) self.assertTrue(os.path.exists(TESTFN)) + def test_run_profile_as_module(self): + # Test that -m switch needs an argument + assert_python_failure('-m', self.profilermodule.__name__, '-m') + + # Test failure for not-existent module + assert_python_failure('-m', self.profilermodule.__name__, + '-m', 'random_module_xyz') + + # Test successful run + assert_python_ok('-m', self.profilermodule.__name__, + '-m', 'timeit', '-n', '1') + def regenerate_expected_output(filename, cls): filename = filename.rstrip('co') |