Repository navigation
Open
Labels
bugUnexpected or incorrect user-visible behaviorUnexpected or incorrect user-visible behavior
Description
In order to reproduce:
x.py:
from .test import system
system('hello world')
test/__init__.py
from os import system
note that test/__init__.py must not have a newline at the end of the file. Try to find_occurrences on system in the call in x.py and you will get this IndexError:
File "/home/ubuntu/anaconda/lib/python2.7/site-packages/rope/contrib/findit.py", line 35, in find_occurrences
return _find_locations(finder, resources, job_set)
File "/home/ubuntu/anaconda/lib/python2.7/site-packages/rope/contrib/findit.py", line 111, in _find_locations
for occurrence in finder.find_occurrences(resource):
File "/home/ubuntu/anaconda/lib/python2.7/site-packages/rope/refactor/occurrences.py", line 75, in find_occurrences
result = filter(occurrence)
File "/home/ubuntu/anaconda/lib/python2.7/site-packages/rope/refactor/occurrences.py", line 205, in __call__
if same_pyname(self.pyname, occurrence.get_pyname()):
File "/home/ubuntu/anaconda/lib/python2.7/site-packages/rope/base/utils.py", line 11, in _wrapper
setattr(self, name, func(self, *args, **kwds))
File "/home/ubuntu/anaconda/lib/python2.7/site-packages/rope/refactor/occurrences.py", line 131, in get_pyname
return self.tools.name_finder.get_pyname_at(self.offset)
File "/home/ubuntu/anaconda/lib/python2.7/site-packages/rope/base/evaluate.py", line 76, in get_pyname_at
return self.get_primary_and_pyname_at(offset)[1]
File "/home/ubuntu/anaconda/lib/python2.7/site-packages/rope/base/evaluate.py", line 108, in get_primary_and_pyname_at
if self.worder.is_from_aliased(offset):
File "/home/ubuntu/anaconda/lib/python2.7/site-packages/rope/base/worder.py", line 66, in is_from_aliased
return self.code_finder.is_from_aliased(offset)
File "/home/ubuntu/anaconda/lib/python2.7/site-packages/rope/base/worder.py", line 370, in is_from_aliased
as_start = self._find_word_start(as_end)
File "/home/ubuntu/anaconda/lib/python2.7/site-packages/rope/base/worder.py", line 131, in _find_word_start
while current_offset >= 0 and self._is_id_char(current_offset):
File "/home/ubuntu/anaconda/lib/python2.7/site-packages/rope/base/worder.py", line 163, in _is_id_char
return self.code[offset].isalnum() or self.code[offset] == '_'
IndexError: string index out of range
I tried to go through and find the right place to fix it, but wasn't sure where to make the change. We don't want to change the FIle.read to enforce newline at end of file. I'm having trouble getting a test case that has this error.
Making this change seems to help:
rope/base/worder.py:162
def _is_id_char(self, offset):
+ if offset >= len(self.code):
+ return False
return self.code[offset].isalnum() or self.code[offset] == '_'
I'm not sure if this is the right way to go about fixing it though, it seems like it might need to be fixed somewhere else.
Metadata
Metadata
Assignees
Labels
bugUnexpected or incorrect user-visible behaviorUnexpected or incorrect user-visible behavior
Activity
mcepl commentedon Jul 27, 2015
@aligrudi ... can I get your wisdom here, please?
aligrudi commentedon Jul 27, 2015
Zach Dwiel notifications@github.com wrote:
You may add a test case to WordRangeFinderTest in codeanalyzetest.py.
The problem is that in
is_from_aliased(),(end + 1)is passed to_find_word_end()to find the end of the next word (end islen(self.code) - 1andend + 1goes pastself.code). I wonder ifis_from_aliased()function should return False if an IndexError is raised (it already does so for ValueError). Does doing so fix the problem?mcepl commentedon Nov 2, 2026
This still reproduces on master (46ebf37) with Python 3.13, and it
breaks rename as well as
find_occurrences():Both end in
Worder._is_id_char()viais_from_aliased()and_find_word_start(), whereself.code[offset]is read past the end oft.py, the module whose last token is the name. That matches@aligrudi's 2015 diagnosis above.
is_from_aliased()now clampsas_endwithmin(..., len(self.code)), but that is still one pastthe last character, and it catches
ValueErrorbut notIndexError.Fix IndexError for a from-import at end of file