The function sys._getframe() returns traceback information.
#!/usr/bin/env python
import sys
def func2():
for n in range( 0, 5):
try:
code = sys._getframe(n).f_code
print( "traceback: depth: %d function: %s file: %s line: %d" % \
(n, code.co_name, code.co_filename, code.co_firstlineno))
except ValueError:
print( "level ", n, "is too deep")
break
def func1():
return func2()
func1()
#
# output:
# traceback: depth: 0 function: func2 file: frame.py line: 4
# traceback: depth: 1 function: func1 file: frame.py line: 14
# traceback: depth: 2 function: <module> file: frame.py line: 2
# level 3 is too deep