]> granicus.if.org Git - python/commitdiff
bpo-23420: Verify the value of '-s' when execute the CLI of cProfile (GH-9925)
authorStéphane Wirtel <stephane@wirtel.be>
Wed, 17 Oct 2018 10:03:40 +0000 (12:03 +0200)
committerVictor Stinner <vstinner@redhat.com>
Wed, 17 Oct 2018 10:03:40 +0000 (12:03 +0200)
Verify the value for the parameter '-s' of the cProfile CLI. Patch by Robert
Kuska.

Co-authored-by: Robert Kuska <rkuska@gmail.com>
Lib/cProfile.py
Lib/test/test_cprofile.py
Misc/NEWS.d/next/Library/2018-10-17-11-00-00.bpo-23420.Lq74Uu.rst [new file with mode: 0644]

index 3c9be3cbd224417fefc17d0dd745c0ec110c132f..305e79e28049866808d9998ccf43085bbcd10b89 100755 (executable)
@@ -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 <outfile>", 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=sorted(pstats.Stats.sort_arg_dict_default))
     parser.add_option('-m', dest="module", action="store_true",
         help="Profile a library module", default=False)
 
index 2fd67ee75688fe7a61d233334db255ba1bca8501..406d70305f9e9c8d64a42eccb1617d3ffd968094 100644 (file)
@@ -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 @@ class CProfileTest(ProfileTest):
         # 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:
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 (file)
index 0000000..034e7e5
--- /dev/null
@@ -0,0 +1,2 @@
+Verify the value for the parameter '-s' of the cProfile CLI. Patch by Robert
+Kuska