From fc718f87288b6f4c95e8ec4450571e79936be4aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Wed, 17 Oct 2018 11:00:13 +0200 Subject: [PATCH 1/4] bpo-23420: Verify the value of '-s' when execute the CLI of cProfile. Co-authored-by: Robert Kuska --- Lib/cProfile.py | 4 +++- .../next/Library/2018-10-17-11-00-00.bpo-23420.Lq74Uu.rst | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2018-10-17-11-00-00.bpo-23420.Lq74Uu.rst diff --git a/Lib/cProfile.py b/Lib/cProfile.py index 3c9be3cbd22441..320a53bae28d4d 100755 --- a/Lib/cProfile.py +++ b/Lib/cProfile.py @@ -131,6 +131,7 @@ def main(): import os import sys import runpy + import pstats from optparse import OptionParser usage = "cProfile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..." parser = OptionParser(usage=usage) @@ -139,7 +140,8 @@ def main(): help="Save stats to ", default=None) parser.add_option('-s', '--sort', dest="sort", help="Sort order when printing to stdout, based on pstats.Stats class", - default=-1) + default=-1, + choices=list(pstats.Stats.sort_arg_dict_default.keys())) parser.add_option('-m', dest="module", action="store_true", help="Profile a library module", default=False) diff --git a/Misc/NEWS.d/next/Library/2018-10-17-11-00-00.bpo-23420.Lq74Uu.rst b/Misc/NEWS.d/next/Library/2018-10-17-11-00-00.bpo-23420.Lq74Uu.rst new file mode 100644 index 00000000000000..034e7e53970adf --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-10-17-11-00-00.bpo-23420.Lq74Uu.rst @@ -0,0 +1,2 @@ +Verify the value for the parameter '-s' of the cProfile CLI. Patch by Robert +Kuska From e110b8da65f3c9b9ddb238544089dc6ed3b32912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Wed, 17 Oct 2018 11:02:24 +0200 Subject: [PATCH 2/4] Add a test for the parameter '-s' for the CLI of cPython --- Lib/test/test_cprofile.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py index 2fd67ee75688fe..406d70305f9e9c 100644 --- a/Lib/test/test_cprofile.py +++ b/Lib/test/test_cprofile.py @@ -2,6 +2,7 @@ import sys from test.support import run_unittest, TESTFN, unlink +import unittest # rip off all interesting stuff from test_profile import cProfile @@ -76,9 +77,14 @@ def test_profile_as_context_manager(self): # profile shouldn't be set once we leave the with-block. self.assertIs(sys.getprofile(), None) +class TestCommandLine(unittest.TestCase): + def test_sort(self): + rc, out, err = assert_python_failure('-m', 'cProfile', '-s', 'demo') + self.assertGreater(rc, 0) + self.assertIn(b"option -s: invalid choice: 'demo'", err) def test_main(): - run_unittest(CProfileTest) + run_unittest(CProfileTest, TestCommandLine) def main(): if '-r' not in sys.argv: From dbe15f9a93e078ff77e6fd90d62a31dff50e2ee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Wed, 17 Oct 2018 11:41:54 +0200 Subject: [PATCH 3/4] Remove the usage of .keys() --- Lib/cProfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/cProfile.py b/Lib/cProfile.py index 320a53bae28d4d..ff3d8505e3d28b 100755 --- a/Lib/cProfile.py +++ b/Lib/cProfile.py @@ -141,7 +141,7 @@ def main(): parser.add_option('-s', '--sort', dest="sort", help="Sort order when printing to stdout, based on pstats.Stats class", default=-1, - choices=list(pstats.Stats.sort_arg_dict_default.keys())) + choices=list(pstats.Stats.sort_arg_dict_default)) parser.add_option('-m', dest="module", action="store_true", help="Profile a library module", default=False) From fef9db20d34da61602fbf2b2de80f39ed9758dc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Wed, 17 Oct 2018 11:43:31 +0200 Subject: [PATCH 4/4] Use sort for the choices of '-s' --- Lib/cProfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/cProfile.py b/Lib/cProfile.py index ff3d8505e3d28b..305e79e2804986 100755 --- a/Lib/cProfile.py +++ b/Lib/cProfile.py @@ -141,7 +141,7 @@ def main(): parser.add_option('-s', '--sort', dest="sort", help="Sort order when printing to stdout, based on pstats.Stats class", default=-1, - choices=list(pstats.Stats.sort_arg_dict_default)) + choices=sorted(pstats.Stats.sort_arg_dict_default)) parser.add_option('-m', dest="module", action="store_true", help="Profile a library module", default=False)