Commit e1bb3d96 by Peter Ljunglöf

Fixes issue #229. Not the nicest solution, but i think it works now.

parent c09ae1a1
......@@ -813,6 +813,21 @@ class AbstractParentedTree(Tree):
- ``_delparent()`` is called whenever a child is removed.
"""
def __init__(self, node_or_str, children=None):
super(AbstractParentedTree, self).__init__(node_or_str, children)
# If children is None, the tree is parsed from node_or_str, and
# all parents will be set during parsing.
if children is not None:
# Otherwise we have to set the parent of the children.
# Iterate over self, and *not* children, because children
# might be an iterator.
for i, child in enumerate(self):
if isinstance(child, Tree):
self._setparent(child, i, dry_run=True)
for i, child in enumerate(self):
if isinstance(child, Tree):
self._setparent(child, i)
#////////////////////////////////////////////////////////////
# Parent management
#////////////////////////////////////////////////////////////
......@@ -1023,6 +1038,15 @@ class ParentedTree(AbstractParentedTree):
self._parent = None
"""The parent of this Tree, or None if it has no parent."""
super(ParentedTree, self).__init__(node_or_str, children)
if children is None:
# If children is None, the tree is parsed from node_or_str.
# After parsing, the parent of the immediate children
# will point to an intermediate tree, not self.
# We fix this by brute force:
for i, child in enumerate(self):
if isinstance(child, Tree):
child._parent = None
self._setparent(child, i)
def _frozen_class(self): return ImmutableParentedTree
......@@ -1133,6 +1157,15 @@ class MultiParentedTree(AbstractParentedTree):
contain duplicates, even if a parent contains this tree
multiple times."""
super(MultiParentedTree, self).__init__(node_or_str, children)
if children is None:
# If children is None, the tree is parsed from node_or_str.
# After parsing, the parent(s) of the immediate children
# will point to an intermediate tree, not self.
# We fix this by brute force:
for i, child in enumerate(self):
if isinstance(child, Tree):
child._parents = []
self._setparent(child, i)
def _frozen_class(self): return ImmutableMultiParentedTree
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment