Identifying a function call in a python script line in runtime
- by Dani
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?