[New-bugs-announce] [issue6635] Profiler doesn't print usage (indexError instead)

Francesco Del Degan report at bugs.python.org
Mon Aug 3 22:32:40 CEST 2009


New submission from Francesco Del Degan <f.deldegan at ngi.it>:

$ 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).

----------
components: Library (Lib)
files: python-profile-sysargv.patch
keywords: patch
messages: 91240
nosy: pr0gg3d
severity: normal
status: open
title: Profiler doesn't print usage (indexError instead)
type: behavior
versions: Python 2.5, Python 2.6, Python 3.1
Added file: http://bugs.python.org/file14639/python-profile-sysargv.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6635>
_______________________________________


More information about the New-bugs-announce mailing list