]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[2.7] 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 11:51:28 +0000 (13:51 +0200)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 17 Oct 2018 11:51:28 +0000 (04:51 -0700)
Verify the value for the parameter '-s' of the cProfile CLI. Patch by Robert
Kuska.

Co-authored-by: Robert Kuska <rkuska@gmail.com>
(cherry picked from commit fcd5e84a515e19409840c570730f0728e9fcfc83)

https://bugs.python.org/issue23420

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 d3770946db11cddd5d92a3d4c91dacb762bf276a..cd53cc0abd53c58fe5be352f2abd20b5b5d8aaa1 100755 (executable)
@@ -161,7 +161,7 @@ def label(code):
 # ____________________________________________________________
 
 def main():
-    import os, sys
+    import os, sys, pstats
     from optparse import OptionParser
     usage = "cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
     parser = OptionParser(usage=usage)
@@ -170,7 +170,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))
 
     if not sys.argv[1:]:
         parser.print_usage()
index af3fe62a9a6b0e40c9df499d443c90a3f495ce01..83d52296e218ac9073bb814883e899b9faaeeb87 100644 (file)
@@ -1,7 +1,9 @@
 """Test suite for the cProfile module."""
 
 import sys
+import unittest
 from test.test_support import run_unittest, TESTFN, unlink
+from test.support.script_helper import assert_python_failure
 
 # rip off all interesting stuff from test_profile
 import cProfile
@@ -26,8 +28,14 @@ class CProfileTest(ProfileTest):
             unlink(TESTFN)
 
 
+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