Skip to content

Commit 6e57382

Browse files
matrixisemiss-islington
authored andcommitted
[2.7] bpo-23420: Verify the value of '-s' when execute the CLI of cProfile (GH-9925) (GH-9928)
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 f82c9f1 commit 6e57382

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

Lib/cProfile.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def label(code):
161161
# ____________________________________________________________
162162

163163
def main():
164-
import os, sys
164+
import os, sys, pstats
165165
from optparse import OptionParser
166166
usage = "cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
167167
parser = OptionParser(usage=usage)
@@ -170,7 +170,8 @@ def main():
170170
help="Save stats to <outfile>", default=None)
171171
parser.add_option('-s', '--sort', dest="sort",
172172
help="Sort order when printing to stdout, based on pstats.Stats class",
173-
default=-1)
173+
default=-1,
174+
choices=sorted(pstats.Stats.sort_arg_dict_default))
174175

175176
if not sys.argv[1:]:
176177
parser.print_usage()

Lib/test/test_cprofile.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Test suite for the cProfile module."""
22

33
import sys
4+
import unittest
45
from test.test_support import run_unittest, TESTFN, unlink
6+
from test.support.script_helper import assert_python_failure
57

68
# rip off all interesting stuff from test_profile
79
import cProfile
@@ -26,8 +28,14 @@ def test_bad_counter_during_dealloc(self):
2628
unlink(TESTFN)
2729

2830

31+
class TestCommandLine(unittest.TestCase):
32+
def test_sort(self):
33+
rc, out, err = assert_python_failure('-m', 'cProfile', '-s', 'demo')
34+
self.assertGreater(rc, 0)
35+
self.assertIn(b"option -s: invalid choice: 'demo'", err)
36+
2937
def test_main():
30-
run_unittest(CProfileTest)
38+
run_unittest(CProfileTest, TestCommandLine)
3139

3240
def main():
3341
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)