Open andreparames opened 8 years ago
I find similar behavior but it depends on how I initialize the nodelist object. Copied nodelists have the problem but not ones built by parsing code. Here's a trace to illustrate that:
>>> from redbaron import RedBaron
>>> test = RedBaron('#!python')
>>> test[-1].insert_after('# comment')
>>> test[-1].insert_after('# comment')
>>> test
0 #!python
1 # comment
2 # comment
>>> copy = RedBaron('#!python').copy()
>>> copy
0 #!python
>>> copy[-1].insert_after('# comment')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/redbaron/base_nodes.py", line 1095, in insert_after
self.parent.insert(self.index_on_parent + 1 + offset, value)
AttributeError: 'NoneType' object has no attribute 'insert'
So I can insert multiple nodes in sequence, but it seems to depend on how the nodelist was constructed in the first place.
I tried using the patch @andreparames suggested on Dec 10, 2015, but that didn't fix my copied nodelist example.
Test case:
Result:
Debugging the code, it seems the problem is that when
d
is inserted, the parent gets set to thenode_list
, instead of theClassNode
that the other attributes have as their parent. That prevents theindex_on_parent
from being calculated for that node.The following patch seems to fix it, but I'm not sure it's safe: