Commit 6e573824 authored by Stéphane Wirtel's avatar Stéphane Wirtel Committed by Miss Islington (bot)

[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: default avatarRobert Kuska <rkuska@gmail.com>
(cherry picked from commit fcd5e84a)





https://bugs.python.org/issue23420
parent f82c9f1e
...@@ -161,7 +161,7 @@ def label(code): ...@@ -161,7 +161,7 @@ def label(code):
# ____________________________________________________________ # ____________________________________________________________
def main(): def main():
import os, sys import os, sys, pstats
from optparse import OptionParser from optparse import OptionParser
usage = "cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ..." usage = "cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
parser = OptionParser(usage=usage) parser = OptionParser(usage=usage)
...@@ -170,7 +170,8 @@ def main(): ...@@ -170,7 +170,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))
if not sys.argv[1:]: if not sys.argv[1:]:
parser.print_usage() parser.print_usage()
......
"""Test suite for the cProfile module.""" """Test suite for the cProfile module."""
import sys import sys
import unittest
from test.test_support import run_unittest, TESTFN, unlink from test.test_support import run_unittest, TESTFN, unlink
from test.support.script_helper import assert_python_failure
# rip off all interesting stuff from test_profile # rip off all interesting stuff from test_profile
import cProfile import cProfile
...@@ -26,8 +28,14 @@ class CProfileTest(ProfileTest): ...@@ -26,8 +28,14 @@ class CProfileTest(ProfileTest):
unlink(TESTFN) 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(): 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:
......
Verify the value for the parameter '-s' of the cProfile CLI. Patch by Robert
Kuska
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment