For debugging purposes I would like to get the source code of the function that has called my function. So in the following situation:
def calling_func():
x = 123
y = x + 1
myfunc()
def myfunc():
calling_source = f()
I would like a function f
, such that calling_source
becomes the string
"""
x = 123
y = x + 1
myfunc()
"""
Does such a function exist? I'm guessing that this might be possible with something like sys._current_frames
and the inspect
module.