how to find a function definition using AST

S

stephene

I have a question about the compiler.ast module. Please bear with me if I
am missing something obvious, I am not too sure how to use this module.

I am using the compiler.visitor module to examine a piece of code and
extract certain useful information. However, I have run into trouble that I
dont know how to solve. When I override visitCallFunc I can get the name of
the function being called, but thats about it. What I would really like is
to know exactly where that function is defined (line of code, what class if
belongs to, everything).

I had a thought that when one prints the node argument from visitCallFunc
then you can see a node which looks something like Getattr(Name('catfish'),
'double'. Perhaps if this statement could be evaluated then the function in
question could be found.

Absolutely any thoughts would be appreciated!
Thanks
Stephen Emslie

my current code looks like this:

class C1:
def F1(self):
"""
pretty much does bugger all
"""
catfish = C2
x = catfish.double(50)
return x

class C2:
def double(x):
"Return twice the argument"
return x * 2

class MyVisitor(visitor.ASTVisitor):
def visitCallFunc(self,node):
print node
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top