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

insert_before() fails with comment #80

Open
connellybarnes opened this issue Sep 26, 2015 · 1 comment
Open

insert_before() fails with comment #80

connellybarnes opened this issue Sep 26, 2015 · 1 comment

Comments

@connellybarnes
Copy link

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
@ibizaman
Copy link
Collaborator

This looks like it is related to #54.

Can you try:

>>> fornode.insert_before(RedBaron('#comment'))

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

2 participants