Fix a bug that might have caused an endless while loop a while ago. Fixes #878.

This commit is contained in:
Dave Halter
2017-03-09 21:46:53 +01:00
parent ccef008376
commit 818fb4f60c
2 changed files with 27 additions and 13 deletions

View File

@@ -148,6 +148,7 @@ class DiffParser(object):
# everything else we keep working with lines_new here. # everything else we keep working with lines_new here.
self._parser_lines_new = list(lines_new) self._parser_lines_new = list(lines_new)
self._parser_lines_new[-1] += '\n' self._parser_lines_new[-1] += '\n'
self._parser_lines_new.append('')
self._added_newline = True self._added_newline = True
self._reset() self._reset()
@@ -428,20 +429,18 @@ class _NodesStackNode(object):
self.children_groups.append(group) self.children_groups.append(group)
def get_last_line(self, suffix): def get_last_line(self, suffix):
if not self.children_groups: line = 0
assert not self.parent if self.children_groups:
return 0 last_leaf = self.children_groups[-1].children[-1].get_last_leaf()
line = last_leaf.end_pos[0]
last_leaf = self.children_groups[-1].children[-1].get_last_leaf() # Calculate the line offsets
line = last_leaf.end_pos[0] line += self.children_groups[-1].line_offset
# Calculate the line offsets # Newlines end on the next line, which means that they would cover
line += self.children_groups[-1].line_offset # the next line. That line is not fully parsed at this point.
if _ends_with_newline(last_leaf, suffix):
# Newlines end on the next line, which means that they would cover line -= 1
# the next line. That line is not fully parsed at this point.
if _ends_with_newline(last_leaf, suffix):
line -= 1
line += suffix.count('\n') line += suffix.count('\n')
return line return line
@@ -460,7 +459,7 @@ class _NodesStack(object):
return not self._base_node.children return not self._base_node.children
@property @property
def parsed_until_line(self, ): def parsed_until_line(self):
return self._tos.get_last_line(self.prefix) return self._tos.get_last_line(self.prefix)
def _get_insertion_node(self, indentation_node): def _get_insertion_node(self, indentation_node):

View File

@@ -423,6 +423,21 @@ def test_whitespace_at_end(differ):
differ.parse(code + '\n', parsers=1, copies=1) differ.parse(code + '\n', parsers=1, copies=1)
def test_endless_while_loop(differ):
"""
This was a bug in Jedi #878.
"""
code = '#dead'
differ.initialize(code)
module = differ.parse(code, parsers=1)
assert module.end_pos == (1, 5)
code = '#dead\n'
differ.initialize(code)
module = differ.parse(code + '\n', parsers=1)
assert module.end_pos == (3, 0)
def test_in_class_movements(differ): def test_in_class_movements(differ):
code1 = dedent("""\ code1 = dedent("""\
class PlaybookExecutor: class PlaybookExecutor: