Skip to content

Commit

Permalink
fix #221, avoid children attribute access on maxlevel
Browse files Browse the repository at this point in the history
  • Loading branch information
c0fec0de committed Oct 11, 2023
1 parent 6fe3851 commit 2e803f6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
15 changes: 9 additions & 6 deletions anytree/iterators/levelorderiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ def _iter(children, filter_, stop, maxlevel):
level = 1
while children:
next_children = []
for child in children:
if filter_(child):
yield child
next_children += AbstractIter._get_children(child.children, stop)
children = next_children
level += 1
if AbstractIter._abort_at_level(level, maxlevel):
break
for child in children:
if filter_(child):
yield child
else:
for child in children:
if filter_(child):
yield child
next_children += AbstractIter._get_children(child.children, stop)
children = next_children
13 changes: 7 additions & 6 deletions anytree/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,14 @@ def __iter__(self):

def __next(self, node, continues, level=0):
yield RenderTree.__item(node, continues, self.style)
children = node.children
level += 1
if children and (self.maxlevel is None or level < self.maxlevel):
children = self.childiter(children)
for child, is_last in _is_last(children):
for grandchild in self.__next(child, continues + (not is_last,), level=level):
yield grandchild
if self.maxlevel is None or level < self.maxlevel:
children = node.children
if children:
children = self.childiter(children)
for child, is_last in _is_last(children):
for grandchild in self.__next(child, continues + (not is_last,), level=level):
yield grandchild

@staticmethod
def __item(node, continues, style):
Expand Down

0 comments on commit 2e803f6

Please sign in to comment.