classification
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
process
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 2018-10-17 13:09 by matrixise. This issue is now closed.

Files
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: https://bugzilla.redhat.com/show_bug.cgi?id=1160640


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 hello.py just prints a "Hello World":


$ python -m cProfile -s hello.py
Traceback (most recent call last):
  File "/usr/lib64/python2.6/runpy.py", line 122, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib64/python2.6/runpy.py", line 34, in _run_code
    exec code in run_globals
  File "/usr/lib64/python2.6/cProfile.py", line 190, in <module>
    main()
  File "/usr/lib64/python2.6/cProfile.py", line 185, in main
    parser.print_usage()
  File "/usr/lib64/python2.6/optparse.py", line 1597, in print_usage
    print >>file, self.get_usage()
  File "/usr/lib64/python2.6/optparse.py", line 1583, in get_usage
    self.expand_prog_name(self.usage))
  File "/usr/lib64/python2.6/optparse.py", line 1560, in expand_prog_name
    return s.replace("%prog", self.get_prog_name())
  File "/usr/lib64/python2.6/optparse.py", 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 sdds.py                                                                                                     
Usage: cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ...

cProfile.py: error: option -s: invalid choice: 'sdds.py' (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 triager) Date: 2018-10-17 09:04
Hi,

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)
https://github.com/python/cpython/commit/fcd5e84a515e19409840c570730f0728e9fcfc83
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)
https://github.com/python/cpython/commit/657e3f9a2c0d620807dd81882d566ad8f1ae423e
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)
https://github.com/python/cpython/commit/669fa8b6376ee8703ae4383536dfcc0e96e51b78
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)
https://github.com/python/cpython/commit/6e57382464101d2669a425622e19fff57586b2ff
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 triager) Date: 2018-10-17 13:09
welcome
History
Date User Action Args
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