Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 657e3f9

Browse files
matrixisemiss-islington
authored andcommitted
[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 <[email protected]> (cherry picked from commit fcd5e84) https://bugs.python.org/issue23420
1 parent 514bbfc commit 657e3f9

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

Lib/cProfile.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def main():
124124
import os
125125
import sys
126126
import runpy
127+
import pstats
127128
from optparse import OptionParser
128129
usage = "cProfile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..."
129130
parser = OptionParser(usage=usage)
@@ -132,7 +133,8 @@ def main():
132133
help="Save stats to <outfile>", default=None)
133134
parser.add_option('-s', '--sort', dest="sort",
134135
help="Sort order when printing to stdout, based on pstats.Stats class",
135-
default=-1)
136+
default=-1,
137+
choices=sorted(pstats.Stats.sort_arg_dict_default))
136138
parser.add_option('-m', dest="module", action="store_true",
137139
help="Profile a library module", default=False)
138140

Lib/test/test_cprofile.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import sys
44
from test.support import run_unittest, TESTFN, unlink
5+
import unittest
56

67
# rip off all interesting stuff from test_profile
78
import cProfile
@@ -50,8 +51,14 @@ def test_module_path_option(self):
5051
assert_python_ok('-m', 'cProfile', '-m', 'timeit', '-n', '1')
5152

5253

54+
class TestCommandLine(unittest.TestCase):
55+
def test_sort(self):
56+
rc, out, err = assert_python_failure('-m', 'cProfile', '-s', 'demo')
57+
self.assertGreater(rc, 0)
58+
self.assertIn(b"option -s: invalid choice: 'demo'", err)
59+
5360
def test_main():
54-
run_unittest(CProfileTest)
61+
run_unittest(CProfileTest, TestCommandLine)
5562

5663
def main():
5764
if '-r' not in sys.argv:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Verify the value for the parameter '-s' of the cProfile CLI. Patch by Robert
2+
Kuska

0 commit comments

Comments
 (0)