Identifying a function call in a python script line in runtime

Posted by Dani on Stack Overflow See other posts from Stack Overflow or by Dani
Published on 2010-05-13T19:49:39Z Indexed on 2010/05/13 19:54 UTC
Read the original article Hit count: 209

Filed under:
|
|

I have a python script that I run with 'exec'. The script's string has calls to functions. When a function is called, I would like it to know the line number and offset in line for that call in the script (in the string I fed exec with).

Here is an example. If my script is:

foo1(); foo2(); foo1()
foo3()

And if I have code that prints (line,offset) in every function, I should get

(0,0), (0,8), (0,16), (1,0)

In most cases this can be easily done by getting the stack frame, because it contains the line number and the function name. The only problem is when there are two functions with the same name in a certain line. Unfortunately this is a common case for me. Any ideas?

© Stack Overflow or respective owner

Related posts about python

Related posts about stacktrace