I'm trying to use Python's inspect
module (in Python 2) to show information about the function that called the current function, including its arguments.
Here's a simple test program:
import inspect
def caller_args():
frame = inspect.currentframe()
outer_frames = inspect.getouterframes(frame)
caller_frame = outer_frames[1]
return inspect.getargvalues(caller_frame)
def fun_a(arg1):
print caller_args()
def fun_b():
fun_a('foo')
if __name__ == '__main__':
fun_b()
And this happens when I run it:
$ python getargvalues_test.py
Traceback (most recent call last):
File "getargvalues_test.py", line 16, in <module>
fun_b()
File "getargvalues_test.py", line 13, in fun_b
fun_a('foo')
File "getargvalues_test.py", line 10, in fun_a
print caller_args()
File "getargvalues_test.py", line 7, in caller_args
return inspect.getargvalues(caller_frame)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 829, in getargvalues
args, varargs, varkw = getargs(frame.f_code)
AttributeError: 'tuple' object has no attribute 'f_code'
I've googled that AttributeError exception, but with no luck. What am I doing wrong?
(I've since found the problem, so I'm asking-and-answering this here so anyone who has this problem in future will find the answer here.)