I have a python script that I run with 'exec'. When a function is called by the script, I would like it to know the line number and offset in line for that call.
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?
Ok it seems that changing the original code is the simplest solution.
How would you solve things like
if foo1(7) or foo1(6):
or
foo2(foo1(), foo1())
There are some not very elegant solutions for this, for example, automatically turning the previous example to:
def curpos(pos, func):
record_curpos(pos)
return func
curpos(foo2,0)(curpos(foo1,5)(), curpos(foo1,13)())
Let me know if you have simpler ideas.