Skip to content

insert_before() fails with comment #80

@connellybarnes

Description

@connellybarnes

The insert_before() method does not succeed if its string contains a single comment. Same deal for insert_after().

For example:

>>> s = """
... def f():
...   a = 10
...   for i in range(10):
...     j += i
... """
>>> 
>>> r=redbaron.RedBaron(s)
>>> fornode=r.find_all('ForNode')[0]
>>> fornode.insert_before('#comment')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/redbaron.py", line 1013, in insert_before
    self.parent.insert(self.index_on_parent - offset, value)
  File "/usr/local/lib/python2.7/site-packages/redbaron.py", line 1242, in insert
    value = self._convert_input_to_node_object(value, parent=self.node_list, on_attribute=self.on_attribute)
  File "/usr/local/lib/python2.7/site-packages/redbaron.py", line 1184, in _convert_input_to_node_object
    return self.node_list.parent._convert_input_to_node_object_list(value, parent, on_attribute).filtered()[0]
  File "/usr/local/lib/python2.7/site-packages/redbaron.py", line 165, in _convert_input_to_node_object_list
    return self._string_to_node_list(value, parent=parent, on_attribute=on_attribute)
  File "/usr/local/lib/python2.7/site-packages/redbaron.py", line 2172, in _string_to_node_list
    return super(DefNode, self)._string_to_node_list(string, parent, on_attribute)
  File "/usr/local/lib/python2.7/site-packages/redbaron.py", line 1022, in _string_to_node_list
    return self.parse_code_block(string, parent=parent, on_attribute=on_attribute)
  File "/usr/local/lib/python2.7/site-packages/redbaron.py", line 1038, in parse_code_block
    fst = baron.parse("def a():\n%s\n" % clean_string)[0]["value"]
  File "/usr/local/lib/python2.7/site-packages/baron/baron.py", line 51, in parse
    tokens = tokenize(source_code, False)
  File "/usr/local/lib/python2.7/site-packages/baron/baron.py", line 72, in tokenize
    return mark_indentation(inner_group(space_group(_tokenize(group(split(pouet)), print_function))))
  File "/usr/local/lib/python2.7/site-packages/baron/indentation_marker.py", line 24, in mark_indentation
    return list(mark_indentation_generator(sequence))
  File "/usr/local/lib/python2.7/site-packages/baron/indentation_marker.py", line 67, in mark_indentation_generator
    indentations.append(get_space(i))
  File "/usr/local/lib/python2.7/site-packages/baron/indentation_marker.py", line 31, in get_space
    if len(node[3]) == 0:
IndexError: tuple index out of range

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions