473,320 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

trace function calls to get signatures

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 'foo.py':

def bar(x,y,z=None) : pass
bar(1,"a",bar)
bar(2,int)

the output of my script should be:
foo.bar(int,str,function)
foo.bar(int,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]
sys.argv.pop(0)
sys.settrace(tracefunc)
__import__(prog)
the output of the above example is:
bar(int,str,function)
bar(int,type,NoneType)

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

1. I would like to have not only the name of the functions and type
arguments
but their full package/module/class-path, e.g.
xml.dom.pulldom.PullDOM.clear
However, I cannot find a way from the frame object to the function
object
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 "tracetest.py", line 28, in ?
__import__(prog)
File "/Volumes/space/Users/harold/uni/pace/dpd/setup.py", line 1,
in ?
from distutils.core import setup, Extension
File "tracetest.py", line 5, in tracefunc
if event is 'call' : return trace_call(frame,event,arg)
File "tracetest.py", 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
or
did anyone of you come up with an implementation?

thanks,

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

Jul 21 '05 #1
0 1042

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Joerg Schoen | last post by:
Hi folks! "JSCPP" is a ANSI-C preprocessor and language parser. It can analyze or instrument C-code. The most prominent application currently is to add function trace calls to existing C...
3
by: Stephen Sprunk | last post by:
On a project I'm working on, I ran across the following macros: /* assume s is struct stream *, s->p is char, v is unit16_t or uint32_t */ #define in_uint16_le(s,v) { v = *((s)->p++); v +=...
0
by: Mike Schilling | last post by:
I have some code that calls methods reflectively (the method called and its parameters are determined by text received in a SOAP message, and I construct a map from strings to MethodInfos). The...
1
by: H Branyan | last post by:
In the Application_OnStart event in global.asax, my code calls an AlertService which creates a System.Timers.Timer() object. So this service doesn't actually run in conjunction with a specific...
0
by: martin | last post by:
Hi, I have created an asp.net application as a project, and also another project that is just a class library. My asp.net application calls a function in the class library. I am attempting to...
3
by: Mo | last post by:
Hi, I have a webform which has vb.net code behind it and I would like it to submit the entries in the fields into a sql server db using a stored procedure. I have a central .vb file in my...
3
by: Jeremy | last post by:
While working with ASP.NET I've sometimes encountered errors in my applications, but I've always known exactly why I'm getting the error and have been able to fix it. So I've never needed to...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
2
by: tshad | last post by:
I have a page with Trace.Warns statements in my Page_Load and functions that it calls, but it doesn't seem to work in a class that is defined on the same page. I am writing this in C# I am...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.