简体   繁体   English

如何在Python中格式化回溯对象

[英]How to format traceback objects in Python

I have a traceback object that I want to show in the nice format I get when calling traceback.format_exc() . 我有一个traceback对象,我希望以调用traceback.format_exc()时得到的漂亮格式显示。

Is there a builtin function for this? 这有内置函数吗? Or a few lines of code? 还是几行代码?

format_exc is really just format_exc真的很公正

    etype, value, tb = sys.exc_info()
    return ''.join(format_exception(etype, value, tb, limit))

So if you have the exception type, value, and traceback ready, it should be easy. 因此,如果您准备好异常类型,值和回溯,那么应该很容易。 If you have just the exception, notice that format_exception is essentially. 如果您只有异常,请注意format_exception本质上是。

    list = ['Traceback (most recent call last):\n']
    list = list + format_tb(tb, limit)

where limit defaults to None. 其中limit默认为None。

traceback docs提供了一些用于格式化回溯对象的示例整套函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM