不要忘记事后调试!引发异常后,所有本地变量的堆栈框架都包含在中
sys.last_traceback。您可以
pdb.pm()转到引发异常的堆栈帧,然后p(retty)p(rint)the
locals()。
这是一个使用此信息从堆栈中提取局部变量的函数。
def findlocals(search, startframe=None, trace=False): from pprint import pprint import inspect, pdb startframe = startframe or sys.last_traceback frames = inspect.getinnerframes(startframe) frame = [tb for (tb, _, lineno, fname, _, _) in frames if search in (lineno, fname)][0] if trace: pprint(frame.f_locals) pdb.set_trace(frame) return frame.f_locals
用法:
>>> def screwyFunc(): a = 0 return 2/a>>> screwyFunc()Traceback (most recent call last): File "<pyshell#62>", line 1, in <module> screwyFunc() File "<pyshell#55>", line 3, in screwyFunc return 2/aZeroDivisionError: integer division or modulo by zero>>> findlocals('screwyFunc'){'a': 0}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)