Skip to content

Crash when parsing long comment (E501) #333

Closed
@movermeyer

Description

@movermeyer

When a ran autopep8 over a large codebase, I noticed one file that was causing it to crash.

It seems to only happen in specific cases. This is what I had to have in my Python file in to get it to trigger:

  1. A function
  2. A single empty line
  3. A long comment (long enough to trigger E501, 80 characters in my case)
  4. A variable assignment

Example code:

def foo(bar):
    pass

#This is a long comment. It has more than 80 characters in it. It triggers an E501
baz = []

Which gives the following stack trace:

$> env/bin/autopep8 test.py
Traceback (most recent call last):
  File "env/bin/autopep8", line 11, in <module>
    sys.exit(main())
  File "env/lib/python2.7/site-packages/autopep8.py", line 3811, in main
    fix_multiple_files(args.files, args, sys.stdout)
  File "env/lib/python2.7/site-packages/autopep8.py", line 3723, in fix_multiple_files
    _fix_file((name, options, output))
  File "env/lib/python2.7/site-packages/autopep8.py", line 3704, in _fix_file
    fix_file(*parameters)
  File "env/lib/python2.7/site-packages/autopep8.py", line 3104, in fix_file
    fixed_source = fix_lines(fixed_source, options, filename=filename)
  File "env/lib/python2.7/site-packages/autopep8.py", line 3084, in fix_lines
    fixed_source = fix.fix()
  File "env/lib/python2.7/site-packages/autopep8.py", line 564, in fix
    aggressive=self.options.aggressive))
  File "env/lib/python2.7/site-packages/autopep8.py", line 507, in _fix_source
    modified_lines = fix(result)
  File "env/lib/python2.7/site-packages/autopep8.py", line 872, in fix_long_line_physically
    original=target)
  File "env/lib/python2.7/site-packages/autopep8.py", line 903, in fix_long_line
    verbose=self.options.verbose)
  File "env/lib/python2.7/site-packages/autopep8.py", line 1166, in get_fixed_long_line
    key=lambda x: line_shortening_rank(
  File "env/lib/python2.7/site-packages/autopep8.py", line 1170, in <lambda>
    experimental=experimental))
  File "env/lib/python2.7/site-packages/autopep8.py", line 3465, in line_shortening_rank
    lines[0].rstrip()[-1] not in '([{'
IndexError: string index out of range

Adding the --ignore=E501 flag makes it work again.

Tested with:

  • cPython 2.7.5 (on CentOS 7) and cPython 2.7.13 (on Windows 10)
  • autopep8 1.3.2 (pycodestyle: 2.3.1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions