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

IndexError: string index out of range #346

Closed
fcastilloec opened this issue Sep 14, 2017 · 7 comments
Closed

IndexError: string index out of range #346

fcastilloec opened this issue Sep 14, 2017 · 7 comments

Comments

@fcastilloec
Copy link

I'm trying to run autopep8 on my file and this is the output I get:

[file:movieConverter_general.py]
--->  Applying global fix for E265
--->  Applying global fix for W602
--->  161 issue(s) to fix {u'E201': set([32, 113, 234, 123, 71]), u'E203': set([229]), u'E202': set([32, 113, 210, 123, 220]), 'E226': set([149, 150, 152, 156, 159, 160, 164, 171, 201, 202, 204, 208, 209, 210, 213, 214, 218, 219, 220, 95, 96, 97, 228, 229]), u'E211': set([12]), u'E127': set([67, 59, 63]), 'E501': set([129, 135, 140, 143, 19, 34, 171, 175, 189, 196, 76, 82, 84, 88, 89, 228, 229, 106, 238, 239, 116, 118, 249]), 'E265': set([129, 34, 19, 76, 118]), 'E261': set([224, 99, 231, 173, 167]), u'E231': set([135, 148, 149, 150, 151, 152, 153, 154, 155, 156, 158, 159, 160, 161, 162, 163, 164, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 212, 213, 214, 215, 216, 217, 218, 219, 106, 107, 113, 116]), 'E302': set([16, 35, 12, 21, 14]), 'E303': set([194]), 'E305': set([78]), u'E129': set([140])}
Traceback (most recent call last):
  File "/usr/local/bin/autopep8", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/autopep8.py", line 3811, in main
    fix_multiple_files(args.files, args, sys.stdout)
  File "/usr/local/lib/python2.7/dist-packages/autopep8.py", line 3723, in fix_multiple_files
    _fix_file((name, options, output))
  File "/usr/local/lib/python2.7/dist-packages/autopep8.py", line 3704, in _fix_file
    fix_file(*parameters)
  File "/usr/local/lib/python2.7/dist-packages/autopep8.py", line 3104, in fix_file
    fixed_source = fix_lines(fixed_source, options, filename=filename)
  File "/usr/local/lib/python2.7/dist-packages/autopep8.py", line 3084, in fix_lines
    fixed_source = fix.fix()
  File "/usr/local/lib/python2.7/dist-packages/autopep8.py", line 564, in fix
    aggressive=self.options.aggressive))
  File "/usr/local/lib/python2.7/dist-packages/autopep8.py", line 507, in _fix_source
    modified_lines = fix(result)
  File "/usr/local/lib/python2.7/dist-packages/autopep8.py", line 872, in fix_long_line_physically
    original=target)
  File "/usr/local/lib/python2.7/dist-packages/autopep8.py", line 903, in fix_long_line
    verbose=self.options.verbose)
  File "/usr/local/lib/python2.7/dist-packages/autopep8.py", line 1166, in get_fixed_long_line
    key=lambda x: line_shortening_rank(
  File "/usr/local/lib/python2.7/dist-packages/autopep8.py", line 1170, in <lambda>
    experimental=experimental))
  File "/usr/local/lib/python2.7/dist-packages/autopep8.py", line 3465, in line_shortening_rank
    lines[0].rstrip()[-1] not in '([{'
IndexError: string index out of range

I've tried to look everywhere what's going on, but I haven't found any answers.
Here's the version of my packages:
autopep8 1.3.2 (pycodestyle: 2.3.1)
pep8 1.7.0

I'm using Ubuntu 16.04, and as the versions show, I install the packages using pip

@christarazi
Copy link

christarazi commented Sep 18, 2017

I managed to fix this issue. Somewhat of a stopgap solution, because I did not look too deep into why this was occurring (also because I am unsure if rank should be returned in its current state). Maybe the maintainers will have a better idea on how to avoid this. The patch is below (from git format-patch):

From 7f25de9b5eae80d8ce7cb5df8af233e17bb60775 Mon Sep 17 00:00:00 2001
From: Chris Tarazi <tarazichris@gmail.com>
Date: Mon, 18 Sep 2017 11:55:23 -0700
Subject: [PATCH 1/1] Fix issue #346: avoid IndexError on empty line

---
 autopep8.py | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/autopep8.py b/autopep8.py
index 918af2d..f79ad71 100755
--- a/autopep8.py
+++ b/autopep8.py
@@ -3520,6 +3520,10 @@ def line_shortening_rank(candidate, indent_word, max_line_length,
     rank = 0
     lines = candidate.rstrip().split('\n')
 
+    # Avoid IndexError -- first line may be empty.
+    if not lines[0].rstrip():
+        return rank
+
     offset = 0
     if (
         not lines[0].lstrip().startswith('#') and
-- 
2.14.1

@myint
Copy link
Collaborator

myint commented Sep 19, 2017

A code example would be helpful us. Thanks!

@pikacic
Copy link

pikacic commented Sep 19, 2017

I have the same problem with this chunk of code:

def something:
    pass

# a quick brown fox jumps over the lazy dog, again, again, again, again, again..
something_else = 123

The comment is the line that triggers the problem.

@christarazi
Copy link

Can confirm @pikacic example triggers the problem. Seems like a very long comment that comes after a line that requires indentation is what causes it.

@myint
Copy link
Collaborator

myint commented Sep 20, 2017

I can't reproduce the IndexError with @pikacic's example. Though, there is indeed something wrong there that could be related. The comment is getting mangled.

$ ./autopep8.py foo.py  -aaavd
[file:foo.py]
--->  Applying global fix for E265
--->  Applying global fix for W602
--->  2 issue(s) to fix {'E501': {4}, 'E305': {5}}
--->  0 issue(s) to fix {}
--- original/foo.py
+++ fixed/foo.py
@@ -1,5 +1,8 @@
 def something():
     pass

-# a quick brown fox jumps over the lazy dog, again, again, again, again, again..
+
+#  # a quick brown fox jumps over the lazy dog, again, again, again,
+
+# again, again..
 something_else = 123

@hhatto
Copy link
Owner

hhatto commented Oct 13, 2017

@fcastilloec
autopep8 version 1.3.3 has been released.
Can you try it as it has been fixed in the latest version?

@fcastilloec
Copy link
Author

@hhatto I just updated to v1.3.3 and I don't see the error anymore! Thanks so much!

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

No branches or pull requests

5 participants