classification
Title: Profiler doesn't print usage (indexError instead)
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.6, Python 2.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: doko, ggenellina, pr0gg3d
Priority: normal Keywords: patch

Created on 2009-08-03 20:32 by pr0gg3d, last changed 2009-09-13 15:32 by doko. This issue is now closed.

Files
File name Uploaded Description Edit
python-profile-sysargv.patch pr0gg3d, 2009-08-03 20:32 patch for preserving of original sys.argv if print_usage() is needed (
Messages (3)
msg91240 - (view) Author: Francesco Del Degan (pr0gg3d) Date: 2009-08-03 20:32
$ python -m profile
Usage: profile.py [-o output_file_path] [-s sort] scriptfile [arg] ...

$ python -m profile -s calls
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/runpy.py", line 122, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/runpy.py", line 34, in _run_code
    exec code in run_globals
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/profile.py", line 619, in <module>
    main()
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/profile.py", line 614, in main
    parser.print_usage()
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/optparse.py", line 1584, in print_usage
    print >>file, self.get_usage()
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/optparse.py", line 1570, in get_usage
    self.expand_prog_name(self.usage))
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/optparse.py", line 1547, in expand_prog_name
    return s.replace("%prog", self.get_prog_name())
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/optparse.py", line 1542, in get_prog_name
    return os.path.basename(sys.argv[0])
IndexError: list index out of range


This is triggered by an early override of sys.argv when usage() is called (Lib/profile.py:603):

    if not sys.argv[1:]:
        parser.print_usage()
        sys.exit(2)

    (options, args) = parser.parse_args()
    sys.argv[:] = args

    if (len(sys.argv) > 0):
        sys.path.insert(0, os.path.dirname(sys.argv[0]))
        run('execfile(%r)' % (sys.argv[0],), options.outfile, options.sort)
    else:
        parser.print_usage()
    return parser




In the "else" branch it tries to print usage but sys.argv[] were already overwritten.

Attached is the proposed patch (tested with 2.5, 2.6, 3.1).
msg91302 - (view) Author: Gabriel Genellina (ggenellina) Date: 2009-08-05 04:56
The patch looks fine to me.
msg92569 - (view) Author: Matthias Klose (doko) * (Python committer) Date: 2009-09-13 15:32
fixed in 2.6, 2.7, 3.1, 3.2
History
Date User Action Args
2009-09-13 15:32:19dokosetstatus: open -> closed
versions: + Python 3.2
nosy: + doko

messages: + msg92569

resolution: fixed
2009-08-05 04:56:02ggenellinasetnosy: + ggenellina
messages: + msg91302
2009-08-03 20:32:39pr0gg3dcreate