python stack在哪个模块_Python stacktrace模块回溯lin

我有以下Python程序:import traceback

import sys

try:

3/0

except OverflowError as e:

exc_type, exc_value, exc_traceback = sys.exc_info()

formatted_lines = traceback.format_exc().splitlines()

print(" It looks like in the arithmetic operation :" , formatted_lines[2], " ) #gets the offending line

print (at line number " , exc_traceback.tb_lineno ) #gets the line number

except ZeroDivisionError as e:

exc_type, exc_value, exc_traceback = sys.exc_info()

formatted_lines = traceback.format_exc().splitlines()

print(" It looks like in the arithmetic operation :" , formatted_lines[2], " ) #gets the offending line

print (at line number " , exc_traceback.tb_lineno ) #gets t

对于上面这样的简单程序,stacktrace返回正确的行号,但是对于更复杂的方法,比如下面Python抛出更多stacktrace(最新的调用是最后一个),有没有办法计算stacktrace ex:formatted_lines[2]的索引来获得最新的调用。在

^{pr2}$

任何帮助都将不胜感激。在

也尝试过:import traceback

import sys

import linecache

try:

2/0

except ZeroDivisionError as e:

filename = exc_traceback.tb_frame.f_code.co_filename

lineno = exc_traceback.tb_lineno

line = linecache.getline(filename, lineno)

print "exception occurred at %s:%d: %s" % (filename, lineno, line)

最后一行“无效语法”有个错误

当我试着:print (filename, lineno, line)

我得到一个错误:Traceback (most recent call last):

File "C:\Users\Anu\Desktop\test.py", line 39, in

filename = exc_traceback.tb_frame.f_code.co_filename

NameError: name 'exc_traceback' is not defined

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值