Using pythons inspect
module I have isolated a method object, I now need to step through the source code in the method to find calls to certain other methods and get their arguments.
For example, suppose in the following class:
def my_method():
print('hello')
foobar('apple', 'pear', 6)
print('world')
foobar(1, 2, 3)
return foobar('a', 'b')
I need to extract a list of arguments passed to foobar()
:
[('apple', 'pear', 6), (1, 2, 3), ('a', 'b', None)]
It can be assumed all arguments are hard coded and not dynamic.
Given a method object from the inspect
package, how can I inspect the method calls in said method?
Attempts
- I've tried using regexes with
inspect.getsourcelines(method)
but this breaks if the argument syntax changes.
- I've looked into abstract syntax trees with pythons
ast
module but havn't come to any solution.
- There must be a way to complete this using
inspect
but again I havn't come to any solution.
This is not perfect but should be a start, I will add a better implementation in a bit:
from ast import parse, Call, walk
import importlib
import inspect
mod = "test"
mod = importlib.import_module(mod)
p = parse(inspect.getsource(mod))
from ast import literal_eval
vals = []
for node in p.body:
if isinstance(node, FunctionDef) and node.name == "my_method":
for node in walk(node):
if isinstance(node,Call) and node.func.id == "foobar":
vals.append([literal_eval(val) for val in node.args])
print(vals)
[['apple', 'pear', 6], [1, 2, 3], ['a', 'b']]
test.py looks like:
def foobar(a=0, b=0, c=None):
return a, b, c
def other_method(x,y,z):
return x,y,z
def my_method():
print('hello')
foobar('apple', 'pear', 6)
print('world')
foobar(1, 2, 3)
for i in range(10):
if i > 9:
s = foobar(4, 5, 6)
print(s)
return foobar('a', 'b')
def my_method2():
foobar('orange', 'tomatoe', 6)
foobar(10, 20, 30)
for i in range(10):
if i > 9:
foobar(40, 50, 60)
other_method("foo","bar","foobar")
return foobar('c', 'd')
If you had a mixture of both you would need to combine somehow changing the call after print('world')
to foobar(a=1, b=2, c=3)
vals = []
for node in p.body:
if isinstance(node, FunctionDef) and node.name == "my_method":
for node in walk(node):
if isinstance(node, Call) and node.func.id == "foobar":
kws = node.keywords
if kws:
print("Found keywords",[(kw.arg, literal_eval(kw.value)) for kw in kws])
else:
print([literal_eval(val) for val in node.args])
Output:
[['apple', 'pear', 6], [], ['a', 'b'], [4, 5, 6]]
['apple', 'pear', 6]
('Found keywords', [('a', 1), ('b', 2), ('c', 3)])
['a', 'b']
[4, 5, 6]
Using the ast.Nodevisitor to find all Call objects will return all calls to "foobar" in all functions:
from ast import parse, NodeVisitor, literal_eval
import importlib
import inspect
mod = "test"
mod = importlib.import_module(mod)
p = parse(inspect.getsource(mod))
class FindCall(NodeVisitor):
def __init__(self, *args):
if len(args) < 1:
raise ValueError("Must supply at least ine target function")
self.result = {arg: []for arg in args}
def visit_Call(self, node):
if node.func.id in self.result:
self.result[node.func.id].append(map(literal_eval, node.args))
# visit the children
self.generic_visit(node)
fc = FindCall("foobar")
fc.visit(p)
print(fc.result)
Output:
from pprint import pprint as pp
pp(fc.result)
{'foobar': [['apple', 'pear', 6],
[1, 2, 3],
[4, 5, 6],
['a', 'b'],
['orange', 'tomatoe', 6],
[10, 20, 30],
[40, 50, 60],
['c', 'd']],
'other_method': [['foo', 'bar', 'foobar']]}
You can again add a search for kwargs and only search the specific functions.