By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

trace function calls to get signatures

P: n/a
Hi all,

I am trying to write a script that prints out the signatures
of each function call that occurs during the execution of
a second script which is invoked by my program. i.e. if the
inspected program is '':

def bar(x,y,z=None) : pass

the output of my script should be:,str,function),type,NoneType)
I thought I go for sys.settrace() and achieved the following:

import sys
import types

def tracefunc(frame,event,arg) :
if event is 'call' : return trace_call(frame,event,arg)
else : return None

def trace_call(frame,event,arg) :
code = frame.f_code
scope = frame.f_locals
try :
print code.co_name+"("+",".join(
[ str(type(scope[var])).split("'")[1]
for var in code.co_varnames
except KeyError : pass
return None

if __name__ == "__main__" :
prog = sys.argv[1]
the output of the above example is:

which is pretty close, but I need / would like to improve several
but have no idea how to do it:

1. I would like to have not only the name of the functions and type
but their full package/module/class-path, e.g.
However, I cannot find a way from the frame object to the function
where I could find the information.

2. The KeyError in my code is raised by the "from XXX import" statement:
"from distutils import setup" results in

File "", line 28, in ?
File "/Volumes/space/Users/harold/uni/pace/dpd/", line 1,
in ?
from distutils.core import setup, Extension
File "", line 5, in tracefunc
if event is 'call' : return trace_call(frame,event,arg)
File "", line 12, in trace_call
print code.co_name+"("+",".join(
KeyError: 'setup'

does anyone know how I can circumvent this?

3. Is there any way to set the __name__ attribute for the inspected
script to
"__main__", so that tracing is really transparent?

4. finally, does a functionality like this already exist in the library
did anyone of you come up with an implementation?


- harold -
Military intelligence is a contradiction in terms.
-- Groucho Marx

Jul 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.