我有以下代码:
# exc is a local variable of type Exception
# This is not inside an except block
if isinstance(exc, ClientError):
logging.debug("ClientError raised while loading %s:\n%s", package.id, traceback.format_exc())
continue
运行此代码并且exc
的类型为ClientError
时,format_exc()
只打印出NoneType: None
,因为当前没有处理任何异常(代码不在except
阻止)。幸运的是,format_exception
上的traceback
方法似乎没有与正在处理的当前异常相关联,但为了调用它,我需要提取类型,值和来自我的异常变量的tb。我该怎么做?
答案 0 :(得分:2)
exc
如何制作?如果从没有相应堆栈的某个函数返回它,那么无论如何都不可能生成正确的帧。最重要的是,如果没有going deep into ctypes
,则无法生成Traceback
个对象,因此这可能不是您想要的。
如果你所追求的实际上是记录异常的堆栈,那么使用inspect.currentframe
和traceback.format_stack
可能会产生你可能会追求的东西。但是,如上所述,您需要将帧设置为接近错误发生的位置。考虑这个例子:
import traceback
import inspect
import logging
class Client:
pass
class ClientError(Exception):
pass
def get_client(name):
if name is None:
return ClientError('client must have a name')
return Client()
def connect(target, name=None):
exc = get_client(name)
if isinstance(exc, ClientError):
frames = inspect.currentframe()
logging.debug("ClientError raised while loading %s:\n%s",
target, ''.join(traceback.format_stack(frames)))
def main():
connect('somewhere')
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
main()
执行此操作将产生以下输出:
DEBUG:root:ClientError raised while loading somewhere:
File "foo.py", line 34, in <module>
main()
File "foo.py", line 30, in main
connect('somewhere')
File "foo.py", line 26, in connect
target, ''.join(traceback.format_stack(frames)))
请注意,堆栈正好在调用完成的位置结束,因为current_frame
的返回值限制为frames
。这就是为什么应该在生成它的位置生成和格式化堆栈,并退回一个级别。考虑这些更新的功能:
def get_client(name):
if name is None:
return (
ClientError('client must have a name'),
traceback.format_stack(inspect.currentframe().f_back),
)
return Client(), None
def connect(target, name=None):
exc, frames = get_client(name)
if isinstance(exc, ClientError):
stack = ''.join(frames)
logging.debug("ClientError raised while loading %s:\n%s",
target, stack)
执行
$ python foo.py
DEBUG:root:ClientError raised while loading somewhere:
File "foo.py", line 37, in <module>
main()
File "foo.py", line 33, in main
connect('somewhere')
File "foo.py", line 25, in connect
exc, frames = get_client(name)
注意跟踪如何在产生异常的函数处结束。