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

Skip to content

bpo-23420: Verify the value of '-s' when execute the CLI of cProfile #9925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/cProfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def main():
import os
import sys
import runpy
import pstats
from optparse import OptionParser
usage = "cProfile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..."
parser = OptionParser(usage=usage)
Expand All @@ -139,7 +140,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))
parser.add_option('-m', dest="module", action="store_true",
help="Profile a library module", default=False)

Expand Down
8 changes: 7 additions & 1 deletion Lib/test/test_cprofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

# rip off all interesting stuff from test_profile
import cProfile
Expand Down Expand Up @@ -76,9 +77,14 @@ def test_profile_as_context_manager(self):
# profile shouldn't be set once we leave the with-block.
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():
run_unittest(CProfileTest)
run_unittest(CProfileTest, TestCommandLine)

def main():
if '-r' not in sys.argv:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Verify the value for the parameter '-s' of the cProfile CLI. Patch by Robert
Kuska