vstinner / rpms / python2

Forked from rpms/python2 5 years ago
Clone
Blob Blame History Raw
commit f6be4ebeb3315ac4d9dc6fcbbfc15b80bad5a4cd
Author: Victor Stinner <vstinner@redhat.com>
Date:   Tue Nov 6 23:46:55 2018 +0100

    bpo-23420: Verify the value of '-s' when execute the CLI of cProfile
    
    Verify the value for the parameter '-s' of the cProfile CLI. Patch by Robert
    Kuska.
    
    Resolves: rhbz#1565101

diff --git a/Lib/cProfile.py b/Lib/cProfile.py
index b2efd04..44abf43 100755
--- a/Lib/cProfile.py
+++ b/Lib/cProfile.py
@@ -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()
diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py
index af3fe62..83d5229 100644
--- a/Lib/test/test_cprofile.py
+++ b/Lib/test/test_cprofile.py
@@ -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: