Skip to content

Commit 1c43468

Browse files
committed
Fix infinite loop visitor
When traversing nodes, prevent infinite loops (cycles).
1 parent 85464f6 commit 1c43468

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

ast_/ast.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ def generic_visit(node: Ast):
5757
def filter_inorder(self, node, filter_func: Callable[[Any], bool]):
5858
""" Visit the tree inorder, but only those that return true for filter
5959
"""
60+
visited = set()
6061
stack = [node]
6162
while stack:
6263
node = stack.pop()
64+
if node in visited:
65+
continue
66+
visited.add(node)
6367
if filter_func(node):
6468
yield self.visit(node)
6569
elif isinstance(node, Ast):

0 commit comments

Comments
 (0)