From d561b1083ebc0fd6c514212ebec3405cc73afffd Mon Sep 17 00:00:00 2001 From: Pankaj Thorat Date: Sun, 27 Oct 2024 18:01:23 +0530 Subject: [PATCH] Increase recursion limit and add error handling for deep recursion of ASTs This commit raises the recursion limit to 10000 to accommodate deeper recursive operations in the AST parsing process. Additionally, it introduces try-except blocks to catch and handle RecursionErrors gracefully, ensuring the program can recover and continue processing when the recursion depth exceeds the newly set limit. This approach enhances the robustness of our AST traversal by preventing abrupt terminations due to recursion depth issues. Signed-off-by: Pankaj Thorat --- .../code/code_profiler/python/src/UAST_parser.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/transforms/code/code_profiler/python/src/UAST_parser.py b/transforms/code/code_profiler/python/src/UAST_parser.py index 3538d8884..fc5bc9819 100644 --- a/transforms/code/code_profiler/python/src/UAST_parser.py +++ b/transforms/code/code_profiler/python/src/UAST_parser.py @@ -14,6 +14,9 @@ import json from tree_sitter import Tree import os +import sys +sys.setrecursionlimit(10000) + """ Initialize the parser with a path for rules and grammar. """ @@ -251,7 +254,10 @@ def _dfs(self, AST_node, parent) : parent = node for child in AST_node.children: - self._dfs(AST_node= child, parent = parent) + try: + self._dfs(AST_node= child, parent = parent) + except RecursionError as e: + print(f"RecursionError caught: {str(e)}") def _extract(self, ast_snippet, node_type, exec_string): code_snippet = ast_snippet @@ -262,4 +268,4 @@ def _extract(self, ast_snippet, node_type, exec_string): try: return self.grammar[node_type]["keyword"] + " " + self.extracted except Exception as e: - print(e) \ No newline at end of file + print(e)