0

I'm using ast to get information from Python source files (as per the suggestion here). For this, I have extended ast.NodeVisitor and it runs fine with most source files. However, I'm getting a

RuntimeError: maximum recursion depth exceeded while calling a Python object

exception when I use this on big classes (~2000 lines. Terrible practice. I know :) ). When I inspect the stack upon the crash, I see that there is indeed a very deep recursion going on, but it does not seem to be going in a loop. I.e., if the recursion would go on for some more, it'll complete successfully. Is there a way to fix this?

I'm not sure if this has anything to do with this, but this big class contains a large if ... elif ... elif ... elif ... else statement.

Community
  • 1
  • 1
asherbret
  • 5,439
  • 4
  • 38
  • 58

2 Answers2

0

I met the error recently, I think this will help you:

class MyNodeVisitor(ast.NodeVisitor):
    def visit(self, node):
        """Visit a node, no recursively."""
        for node in ast.walk(node):
            method = 'visit_' + node.__class__.__name__
            getattr(self, method, lambda x: x)(node)
damnever
  • 12,435
  • 1
  • 13
  • 18
-1

I figured it out: Since I only need to parse ClassDef, Import and FromImport statements, I gave an overriding method to generic_visit that does nothing:

def generic_visit(self, node):
    pass

instead of the super's generic_visit, which was causing the recursion. This also boosted performance greatly. The downside is that only statements in the global scope of the module are parsed (I think), but that's fine with what I need.

asherbret
  • 5,439
  • 4
  • 38
  • 58