当我用sys.excepthook捕获意外错误时
import sys import traceback def handleException(excType,excValue,trace): print 'error' traceback.print_exception(excType,trace) sys.excepthook = handleException h = 1 k = 0 print h/k
这是我得到的输出
error Traceback (most recent call last): File "test.py",line 13,in <module> print h/k ZeroDivisionError: integer division or modulo by zero
如何在追溯到http://www.doughellmann.com/PyMOTW/cgitb/的追溯中包含变量值(h,k,…)?当我包含cgitb结果是一样的.
编辑:
def handleException(excType,trace): cgitb.Hook(logdir=os.path.dirname(__file__),display=False,format='text')(excType,trace)
解决方法
通过查看cgitb.py的来源,您应该可以使用以下内容:
import sys import traceback import cgitb def handleException(excType,trace): print 'error' cgitb.Hook(format="text")(excType,trace) sys.excepthook = handleException h = 1 k = 0 print h/k