summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStéphane Wirtel <stephane@wirtel.be>2018-10-17 11:48:05 (GMT)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-10-17 11:48:05 (GMT)
commit657e3f9a2c0d620807dd81882d566ad8f1ae423e (patch)
treebeea4d4779aed4150a6cbd4dd8e68b7e3f76a311
parent514bbfc7fc4dcb868d4364632ad14c0533af154f (diff)
downloadcpython-657e3f9a2c0d620807dd81882d566ad8f1ae423e.zip
cpython-657e3f9a2c0d620807dd81882d566ad8f1ae423e.tar.gz
cpython-657e3f9a2c0d620807dd81882d566ad8f1ae423e.tar.bz2
[3.7] bpo-23420: Verify the value of '-s' when execute the CLI of cProfile (GH-9925) (GH-9926)
[3.7] [bpo-23420](https://bugs.python.org/issue23420): Verify the value of '-s' when execute the CLI of cProfile (GH-9925) 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
-rwxr-xr-xLib/cProfile.py4
-rw-r--r--Lib/test/test_cprofile.py9
-rw-r--r--Misc/NEWS.d/next/Library/2018-10-17-11-00-00.bpo-23420.Lq74Uu.rst2
3 files changed, 13 insertions, 2 deletions
diff --git a/Lib/cProfile.py b/Lib/cProfile.py
index c044be8..f6e423b 100755
--- a/Lib/cProfile.py
+++ b/Lib/cProfile.py
@@ -124,6 +124,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)
@@ -132,7 +133,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)
diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py
index 1430d22..5c4ec5b 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
@@ -50,8 +51,14 @@ class CProfileTest(ProfileTest):
assert_python_ok('-m', 'cProfile', '-m', 'timeit', '-n', '1')
+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
index 0000000..034e7e5
--- /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