This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: python -m cProfile -s fails with non informative message
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6, Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, cstratak, louielu, matrixise, miss-islington, rkuska, sYnfo, vstinner
Priority: normal Keywords: patch

Created on 2015-02-09 12:05 by rkuska, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
sort-choices.patch rkuska, 2015-02-09 12:05 Add choices for sort option of cProfile review
sort-choices.patch rkuska, 2015-02-09 12:12 missing space before imported module review
sort-choices.patch rkuska, 2015-02-09 12:51 convert to list review
Pull Requests
URL Status Linked Edit
PR 9925 merged matrixise, 2018-10-17 09:03
PR 9926 merged matrixise, 2018-10-17 10:33
PR 9927 merged matrixise, 2018-10-17 11:18
PR 9928 merged matrixise, 2018-10-17 11:24
Messages (11)
msg235602 - (view) Author: Robert Kuska (rkuska) * Date: 2015-02-09 12:05
Originaly reported here:

I've forgotten to add the sort value to the -s option of cProfile which results in a traceback instead of user friendly error message. In the example below just prints a "Hello World":

$ python -m cProfile -s
Traceback (most recent call last):
  File "/usr/lib64/python2.6/", line 122, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib64/python2.6/", line 34, in _run_code
    exec code in run_globals
  File "/usr/lib64/python2.6/", line 190, in <module>
  File "/usr/lib64/python2.6/", line 185, in main
  File "/usr/lib64/python2.6/", line 1597, in print_usage
    print >>file, self.get_usage()
  File "/usr/lib64/python2.6/", line 1583, in get_usage
  File "/usr/lib64/python2.6/", line 1560, in expand_prog_name
    return s.replace("%prog", self.get_prog_name())
  File "/usr/lib64/python2.6/", line 1555, in get_prog_name
    return os.path.basename(sys.argv[0])
IndexError: list index out of range

Tested with python2.7, python3.4 with the same result.

Attached patch adds `choices` for sort option of cProfile.

> $ python -m cProfile -s                                                                                                     
Usage: [-o output_file_path] [-s sort] scriptfile [arg] ... error: option -s: invalid choice: '' (choose from 'cumulative', 'module', 'ncalls', 'pcalls', 'file', 'line', 'name', 'calls', 'stdname', 'nfl', 'filename', 'cumtime', 'time', 'tottime')
msg265765 - (view) Author: Charalampos Stratakis (cstratak) * Date: 2016-05-17 11:03
Any info regarding that? Patch seems good and it actually works.
msg265813 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-05-18 06:35
Patch looks good to me, but we need a test case.
msg292147 - (view) Author: Louie Lu (louielu) * Date: 2017-04-23 03:07
If we can solve #30118 for argument unittest, and apply #18971 for optparse to argparse, this issue will then can be solve, too.
msg327876 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2018-10-17 09:04

Just used the content of the patch and apply it on master,
Add a unittest and the blurb entry.
msg327884 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-10-17 10:03
New changeset fcd5e84a515e19409840c570730f0728e9fcfc83 by Victor Stinner (Stéphane Wirtel) in branch 'master':
 bpo-23420: Verify the value of '-s' when execute the CLI of cProfile (GH-9925)
msg327891 - (view) Author: miss-islington (miss-islington) Date: 2018-10-17 11:48
New changeset 657e3f9a2c0d620807dd81882d566ad8f1ae423e by Miss Islington (bot) (Stéphane Wirtel) in branch '3.7':
[3.7] bpo-23420: Verify the value of '-s' when execute the CLI of cProfile (GH-9925) (GH-9926)
msg327892 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-10-17 11:48
New changeset 669fa8b6376ee8703ae4383536dfcc0e96e51b78 by Victor Stinner (Stéphane Wirtel) in branch '3.6':
[3.6] bpo-23420: Verify the value of '-s' when execute the CLI of cProfile (GH-9925) (GH-9927)
msg327893 - (view) Author: miss-islington (miss-islington) Date: 2018-10-17 11:51
New changeset 6e57382464101d2669a425622e19fff57586b2ff by Miss Islington (bot) (Stéphane Wirtel) in branch '2.7':
[2.7] bpo-23420: Verify the value of '-s' when execute the CLI of cProfile (GH-9925) (GH-9928)
msg327894 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-10-17 13:07
Thanks Robert Kuska for the bug report and the initial patch, thanks Stéphane Wirtel for the PR (with the NEWS entry and the new test ;-))
msg327895 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2018-10-17 13:09
Date User Action Args
2022-04-11 14:58:12adminsetgithub: 67608
2018-10-17 13:09:22matrixisesetmessages: + msg327895
2018-10-17 13:07:16vstinnersetstatus: open -> closed
versions: + Python 3.7, Python 3.8, - Python 3.5
messages: + msg327894

resolution: fixed
stage: patch review -> resolved
2018-10-17 11:51:31miss-islingtonsetmessages: + msg327893
2018-10-17 11:48:56vstinnersetmessages: + msg327892
2018-10-17 11:48:09miss-islingtonsetnosy: + miss-islington
messages: + msg327891
2018-10-17 11:24:12matrixisesetpull_requests: + pull_request9281
2018-10-17 11:18:56matrixisesetpull_requests: + pull_request9280
2018-10-17 10:33:48matrixisesetpull_requests: + pull_request9279
2018-10-17 10:03:50vstinnersetmessages: + msg327884
2018-10-17 09:04:28matrixisesetnosy: + matrixise
messages: + msg327876
2018-10-17 09:03:08matrixisesetpull_requests: + pull_request9278
2018-10-10 13:08:25vstinnersetnosy: + vstinner
2017-04-23 03:07:16louielusetnosy: + louielu
messages: + msg292147
2016-05-18 06:35:15berker.peksagsettype: enhancement -> behavior
messages: + msg265813
versions: - Python 3.4
2016-05-17 11:03:14cstrataksetnosy: + cstratak
messages: + msg265765
2015-07-31 03:46:38zach.waresetversions: + Python 3.6
2015-02-14 18:39:44sYnfosetnosy: + sYnfo
2015-02-14 18:27:56berker.peksagsetnosy: + berker.peksag
stage: patch review

components: + Library (Lib), - Extension Modules
versions: + Python 3.5
2015-02-09 12:51:05rkuskasetfiles: + sort-choices.patch
2015-02-09 12:12:52rkuskasetfiles: + sort-choices.patch
2015-02-09 12:05:42rkuskacreate