Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash when parsing long comment (E501) #333

Closed
movermeyer opened this issue Jun 26, 2017 · 1 comment · Fixed by #334
Closed

Crash when parsing long comment (E501) #333

movermeyer opened this issue Jun 26, 2017 · 1 comment · Fixed by #334
Labels

Comments

@movermeyer
Copy link

movermeyer commented Jun 26, 2017

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)
@myint myint added the bug label Jun 27, 2017
myint added a commit that referenced this issue Jun 27, 2017
This fixes #333. The bug was introduced in 138f281.
@myint
Copy link
Collaborator

myint commented Jun 27, 2017

Thanks for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants