The goal is to detect if a builtin function such as eval()
is used in some code.
def foo(a):
eval('a = 2')
I have tried the following approach:
ex_ast = ast.parse(inspect.getsource(foo))
for node in ast.walk(ex_ast):
if isinstance(node, ast.FunctionDef):
print(node.name)
The function name foo
is printed as the output.
I know that Builtin functions don't have constructors. They are in the type
Module. So 1 approach would be using types.FunctionType
in an isinstance
call.
But since I'm using AST nodes. They cannot be transformed back into code. I have to check for each node if they are types.FunctionType
:
for node in ast.walk(ex_ast):
if isinstance(node, ast.FunctionType):
print(node.name)
I got these errors:
AttributeError: module 'ast' has no attribute 'FunctionType'
How should I correctly identify if a specific Buildin Function is used in code? Thanks!