bpo-23420: 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>
This commit is contained in:
Stéphane Wirtel 2018-10-17 12:03:40 +02:00 committed by Victor Stinner
parent 8e73ad38ab
commit fcd5e84a51
3 changed files with 12 additions and 2 deletions

View File

@ -131,6 +131,7 @@ def main():
import os import os
import sys import sys
import runpy import runpy
import pstats
from optparse import OptionParser from optparse import OptionParser
usage = "cProfile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..." usage = "cProfile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..."
parser = OptionParser(usage=usage) parser = OptionParser(usage=usage)
@ -139,7 +140,8 @@ def main():
help="Save stats to <outfile>", default=None) help="Save stats to <outfile>", default=None)
parser.add_option('-s', '--sort', dest="sort", parser.add_option('-s', '--sort', dest="sort",
help="Sort order when printing to stdout, based on pstats.Stats class", 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", parser.add_option('-m', dest="module", action="store_true",
help="Profile a library module", default=False) help="Profile a library module", default=False)

View File

@ -2,6 +2,7 @@
import sys import sys
from test.support import run_unittest, TESTFN, unlink from test.support import run_unittest, TESTFN, unlink
import unittest
# rip off all interesting stuff from test_profile # rip off all interesting stuff from test_profile
import cProfile import cProfile
@ -76,9 +77,14 @@ class CProfileTest(ProfileTest):
# profile shouldn't be set once we leave the with-block. # profile shouldn't be set once we leave the with-block.
self.assertIs(sys.getprofile(), None) self.assertIs(sys.getprofile(), None)
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(): def test_main():
run_unittest(CProfileTest) run_unittest(CProfileTest, TestCommandLine)
def main(): def main():
if '-r' not in sys.argv: if '-r' not in sys.argv:

View File

@ -0,0 +1,2 @@
Verify the value for the parameter '-s' of the cProfile CLI. Patch by Robert
Kuska