[英]how to print out stack trace of function call that has already been returned?
例如:
with magically_trace_stack() as trace:
func(1, 2)
print(trace.format_tb())
func
可以调用许多其他函数,我想知道是哪些。
(ps:不要向我推荐调试器)
def b():
print('b')
def a():
print('a')
b()
def func():
print('func')
a()
with magically_trace_stack() as trace:
func()
trace.print_stack()
和打印堆栈应该是这样的:
called func
called a
called b
exit b
exit a
exit func
函数a
和b
是隐藏的,我只能访问func
。
您可以创建一个上下文管理器类,该类使用sys.settrace
启用解释器的跟踪功能,并使用将跟踪事件和帧附加到列表的方法:
import sys
class magically_trace_stack:
def __init__(self):
self.events = []
def __enter__(self):
sys.settrace(self.trace)
return self
def __exit__(self, *args):
sys.settrace(None)
def trace(self, frame, event, arg):
# we don't want to record the call to the exit of the context manager
if frame.f_code is not self.__exit__.__func__.__code__:
self.events.append((frame, event, arg))
return self.trace
def print_stack(self):
for frame, event, arg in self.events:
if event == 'call':
print(f'called {frame.f_code.co_name}')
elif event == 'return':
print(f'exit {frame.f_code.co_name} with {arg}')
以便:
def b():
print('b')
return 1
def a():
print('a')
b()
return 2
def func():
print('func')
a()
with magically_trace_stack() as trace:
func()
trace.print_stack()
输出:
func
a
b
called func
called a
called b
exit b with 1
exit a with 2
exit func with None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.