我发现此函数可以格式化引发的异常
def format_trace_except(self, sysexecinfo, smessage= ''):
""" Trace exceptions """
exc_type, exc_value, exc_traceback = sysexecinfo
i, j = (traceback.extract_tb(exc_traceback, 1))[0][0:2]
k = (traceback.format_exception_only(exc_type, exc_value))[0]
trace('Err : ' + smessage + k + i + ', line ' + str(j))
return k
当我使用此功能时,出现以下错误:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 121, in get_response
response = middleware_method(request, e)
File "/home/xxxxxxxxxxxxx", line 60, in process_exception
return self.handle_500(request, exception)
File "/home/xxxxxxxxxxxxxxx", line 64, in handle_500
response = self.format_trace_except(sys.exc_info())
File "/home/xxxxxxxxxxxxxxxxx", line 74, in format_trace_except
x = trace('Err : ' + smessage + k + i + ', line ' + str(j))
File "/usr/lib/python2.7/inspect.py", line 1058, in trace
return getinnerframes(sys.exc_info()[2], context)
File "/usr/lib/python2.7/inspect.py", line 1043, in getinnerframes
framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
File "/usr/lib/python2.7/inspect.py", line 1005, in getframeinfo
start = lineno - 1 - context//2
TypeError: unsupported operand type(s) for //: 'str' and 'int'
谁能告诉我如何解决?
最佳答案
停止从*
导入inspect
。
关于python -//不支持的操作数类型//:'str','int',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20717672/