-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_exceptions.py
More file actions
101 lines (87 loc) · 4.12 KB
/
parse_exceptions.py
File metadata and controls
101 lines (87 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import ast
import sys
from pathlib import Path
_ME = Path(sys.argv[0] if __name__ =="__main__" else __file__).resolve().name
# Manually curated list of functions and the exceptions they might raise
known_exceptions = {
'open': {'FileNotFoundError', 'PermissionError'},
# Add more functions and their exceptions here
# 'function_name': {'Exception1', 'Exception2', ...},
}
class ExceptionFinder(ast.NodeVisitor):
def __init__(self):
self.current_function = None
self.functions = {}
self.function_calls = {}
self.handled_exceptions = set() # Exceptions that are caught and handled
self.propagated_exceptions = set() # Exceptions that are caught but not handled (re-raised)
def visit_FunctionDef(self, node):
self.current_function = node.name
self.functions[self.current_function] = set()
self.function_calls[self.current_function] = set()
self.handled_exceptions = set() # Reset for each function
self.propagated_exceptions = set() # Reset for each function
self.generic_visit(node)
# Propagate exceptions from called functions
for called_func in self.function_calls[self.current_function]:
if called_func in self.functions:
self.functions[self.current_function].update(
self.functions[called_func] - self.handled_exceptions
)
self.functions[self.current_function].update(self.propagated_exceptions)
self.current_function = None
def visit_Raise(self, node):
if self.current_function:
if isinstance(node.exc, ast.Call) and isinstance(node.exc.func, ast.Name):
exception_name = node.exc.func.id
self.functions[self.current_function].add(exception_name)
if exception_name in known_exceptions:
self.functions[self.current_function].update(known_exceptions[exception_name])
elif isinstance(node.exc, ast.Name):
self.functions[self.current_function].add(node.exc.id)
# If there's no specific exception being raised, it might be a re-raise
elif node.exc is None:
self.functions[self.current_function].update(self.propagated_exceptions)
self.generic_visit(node)
def visit_Call(self, node):
if self.current_function and isinstance(node.func, ast.Name):
func_name = node.func.id
# Record the function call
self.function_calls[self.current_function].add(func_name)
if func_name in known_exceptions:
self.functions[self.current_function].update(known_exceptions[func_name])
self.generic_visit(node)
def visit_Try(self, node):
# Visit the body of the try block
for item in node.body:
self.visit(item)
for handler in node.handlers:
# If the handler is catching an exception but has no body, it's re-raising it
if not handler.body:
caught_exception = handler.name if handler.name else handler.type.id
self.propagated_exceptions.add(caught_exception)
else:
# If the handler has a body, the exception is considered handled
handled_exception = handler.name if handler.name else handler.type.id
self.handled_exceptions.add(handled_exception)
self.generic_visit(handler)
# Visit orelse part
for item in node.orelse:
self.visit(item)
# Visit finalbody part
for item in node.finalbody:
self.visit(item)
def report(self):
for function, exceptions in self.functions.items():
print(f"Function '{function}' may raise the following exceptions: {', '.join(exceptions) or 'None'}")
def analyze_exceptions(filename):
with open(filename, 'r') as file:
node = ast.parse(file.read())
finder = ExceptionFinder()
finder.visit(node)
finder.report()
if __name__ == "__main__":
if len(sys.argv) < 2:
print(f"Usage: python {_ME} <filename.py>")
else:
analyze_exceptions(sys.argv[1])