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

Skip to content

Commit 669fa8b

Browse files
matrixiserkuska
authored andcommitted
[3.6] bpo-23420: Verify the value of '-s' when execute the CLI of cProfile (GH-9925) (GH-9927)
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)
1 parent ae011e0 commit 669fa8b

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

Lib/cProfile.py

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

123123
def main():
124-
import os, sys
124+
import os, sys, pstats
125125
from optparse import OptionParser
126126
usage = "cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
127127
parser = OptionParser(usage=usage)
@@ -130,7 +130,8 @@ def main():
130130
help="Save stats to <outfile>", default=None)
131131
parser.add_option('-s', '--sort', dest="sort",
132132
help="Sort order when printing to stdout, based on pstats.Stats class",
133-
default=-1)
133+
default=-1,
134+
choices=sorted(pstats.Stats.sort_arg_dict_default))
134135

135136
if not sys.argv[1:]:
136137
parser.print_usage()

Lib/test/test_cprofile.py

Lines changed: 10 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.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
@@ -36,8 +38,15 @@ def test_bad_counter_during_dealloc(self):
3638
unlink(TESTFN)
3739

3840

41+
class TestCommandLine(unittest.TestCase):
42+
def test_sort(self):
43+
rc, out, err = assert_python_failure('-m', 'cProfile', '-s', 'demo')
44+
self.assertGreater(rc, 0)
45+
self.assertIn(b"option -s: invalid choice: 'demo'", err)
46+
47+
3948
def test_main():
40-
run_unittest(CProfileTest)
49+
run_unittest(CProfileTest, TestCommandLine)
4150

4251
def main():
4352
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)