Skip to content

Commit

Permalink
[3.6] bpo-23420: Verify the value of '-s' when execute the CLI of cPr…
Browse files Browse the repository at this point in the history
…ofile (GH-9925) (GH-9927)

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 fcd5e84)
  • Loading branch information
matrixise authored and vstinner committed Oct 17, 2018
1 parent ae011e0 commit 669fa8b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Lib/cProfile.py
Expand Up @@ -121,7 +121,7 @@ def label(code):
# ____________________________________________________________

def main():
import os, sys
import os, sys, pstats
from optparse import OptionParser
usage = "cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
parser = OptionParser(usage=usage)
Expand All @@ -130,7 +130,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))

if not sys.argv[1:]:
parser.print_usage()
Expand Down
11 changes: 10 additions & 1 deletion Lib/test/test_cprofile.py
@@ -1,7 +1,9 @@
"""Test suite for the cProfile module."""

import sys
import unittest
from test.support import run_unittest, TESTFN, unlink
from test.support.script_helper import assert_python_failure

# rip off all interesting stuff from test_profile
import cProfile
Expand Down Expand Up @@ -36,8 +38,15 @@ def test_bad_counter_during_dealloc(self):
unlink(TESTFN)


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:
Expand Down
@@ -0,0 +1,2 @@
Verify the value for the parameter '-s' of the cProfile CLI. Patch by Robert
Kuska

0 comments on commit 669fa8b

Please sign in to comment.