【发布时间】:2016-10-06 19:03:37
【问题描述】:
我的文件 decorators.py 中有以下 python 装饰器
def catch_exceptions(function): #Line #1
@wraps(function) #Line #2
def decorator(*args, **kwargs): #Line #3
try: #Line #4
return function(*args, **kwargs) #Line #5
except Exception as e: #Line #6
exc_type, exc_obj, exc_tb = sys.exc_info() #Line #7
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] #Line #8
print "E=%s, F=%s, L=%s" % (str(e), fname, exc_tb.tb_lineno) #Line #9
return decorator #Line #10
在另一个文件my_file.py 中,我使用catch_exceptions 装饰器,如下所示:
from decorators import catch_exceptions #Line #1
@catch_exceptions #Line #2
def my_method() #Line #3
print (10/0 - 5/0) #Line #4
当我运行它时,我得到以下输出:
E=integer division or modulo by zero, F=decorators.py, L=5
而不是将异常位置报告为decorators.py,第 5 行,如何让它报告最初发生异常的实际文件和行号?那将是my_file.py 中的第 4 行。
【问题讨论】:
标签: python django exception-handling