churchyard / rpms / python3

Forked from rpms/python3 4 years ago
Clone

Blame 00313-cprofile-sort-option.patch

a4e3949
commit c36e8721f276e7cc09cecdb9c04783630f0ba82a
a4e3949
Author: Victor Stinner <vstinner@redhat.com>
a4e3949
Date:   Wed Nov 7 00:34:22 2018 +0100
a4e3949
a4e3949
    bpo-23420: Verify the value of '-s' when execute the CLI of cProfile
a4e3949
    
a4e3949
    Verify the value for the parameter '-s' of the cProfile CLI. Patch by Robert
a4e3949
    Kuska.
a4e3949
    
a4e3949
    Resolves: rhbz#1160640
a4e3949
a4e3949
diff --git a/Lib/cProfile.py b/Lib/cProfile.py
a4e3949
index c044be8..f6e423b 100755
a4e3949
--- a/Lib/cProfile.py
a4e3949
+++ b/Lib/cProfile.py
a4e3949
@@ -124,6 +124,7 @@ def main():
a4e3949
     import os
a4e3949
     import sys
a4e3949
     import runpy
a4e3949
+    import pstats
a4e3949
     from optparse import OptionParser
a4e3949
     usage = "cProfile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..."
a4e3949
     parser = OptionParser(usage=usage)
a4e3949
@@ -132,7 +133,8 @@ def main():
a4e3949
         help="Save stats to <outfile>", default=None)
a4e3949
     parser.add_option('-s', '--sort', dest="sort",
a4e3949
         help="Sort order when printing to stdout, based on pstats.Stats class",
a4e3949
-        default=-1)
a4e3949
+        default=-1,
a4e3949
+        choices=sorted(pstats.Stats.sort_arg_dict_default))
a4e3949
     parser.add_option('-m', dest="module", action="store_true",
a4e3949
         help="Profile a library module", default=False)
a4e3949
 
a4e3949
diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py
a4e3949
index 1430d22..5c4ec5b 100644
a4e3949
--- a/Lib/test/test_cprofile.py
a4e3949
+++ b/Lib/test/test_cprofile.py
a4e3949
@@ -2,6 +2,7 @@
a4e3949
 
a4e3949
 import sys
a4e3949
 from test.support import run_unittest, TESTFN, unlink
a4e3949
+import unittest
a4e3949
 
a4e3949
 # rip off all interesting stuff from test_profile
a4e3949
 import cProfile
a4e3949
@@ -50,8 +51,14 @@ class CProfileTest(ProfileTest):
a4e3949
         assert_python_ok('-m', 'cProfile', '-m', 'timeit', '-n', '1')
a4e3949
 
a4e3949
 
a4e3949
+class TestCommandLine(unittest.TestCase):
a4e3949
+    def test_sort(self):
a4e3949
+        rc, out, err = assert_python_failure('-m', 'cProfile', '-s', 'demo')
a4e3949
+        self.assertGreater(rc, 0)
a4e3949
+        self.assertIn(b"option -s: invalid choice: 'demo'", err)
a4e3949
+
a4e3949
 def test_main():
a4e3949
-    run_unittest(CProfileTest)
a4e3949
+    run_unittest(CProfileTest, TestCommandLine)
a4e3949
 
a4e3949
 def main():
a4e3949
     if '-r' not in sys.argv: