traceback
— 打印或检索堆栈回溯
¶
源代码: Lib/traceback.py
This module provides a standard interface to extract, format and print stack traces of Python programs. It is more flexible than the interpreter’s default traceback display, and therefore makes it possible to configure certain aspects of the output. Finally, it contains a utility for capturing enough information about an exception to print it later, without the need to save a reference to the actual exception. Since exceptions can be the roots of large objects graph, this utility can significantly improve memory management.
The module uses
traceback objects
— these are objects of type
types.TracebackType
, which are assigned to the
__traceback__
字段对于
BaseException
实例。
另请参阅
-
模块
faulthandler -
Used to dump Python tracebacks explicitly, on a fault, after a timeout, or on a user signal.
-
模块
pdb -
Interactive source code debugger for Python programs.
The module’s API can be divided into two parts:
-
Module-level functions offering basic functionality, which are useful for interactive inspection of exceptions and tracebacks.
-
TracebackExceptionclass and its helper classesStackSummaryandFrameSummary. These offer both more flexibility in the output generated and the ability to store the information necessary for later formatting without holding references to actual exception and traceback objects.
模块级函数 ¶
- traceback. print_tb ( tb , limit = None , file = None ) ¶
-
打印直到 limit stack trace entries from 回溯对象 tb (从调用者帧开始) 若 limit 为正值。否则,打印最后
abs(limit)条目。若 limit 被省略或None,打印所有条目。若 file 被省略或None,输出转到sys.stderr; otherwise it should be an open file or 像文件对象 to receive the output.注意
The meaning of the limit parameter is different than the meaning of
sys.tracebacklimit. A negative limit value corresponds to a positive value ofsys.tracebacklimit, whereas the behaviour of a positive limit value cannot be achieved withsys.tracebacklimit.3.5 版改变: 添加负值 limit 支持。
- traceback. print_exception ( exc , / , [ 值 , tb , ] limit=None , file=None , chain=True ) ¶
-
Print exception information and stack trace entries from 回溯对象 tb to file 。这不同于
print_tb()在下列方式:-
if tb 不是
None,它打印头Traceback (most recent call last): -
it prints the exception type and value 在堆栈跟踪之后
-
if type(value) is
SyntaxErrorand value 拥有适当格式,它打印发生句法错误的行,采用插入符号指示错误的大致位置。
Since Python 3.10, instead of passing value and tb , an exception object can be passed as the first argument. If value and tb are provided, the first argument is ignored in order to provide backwards compatibility.
可选 limit 自变量有相同含义如
print_tb()。若 chain 为 True (默认),则连锁异常 (__cause__or__context__属性对于异常) 也将被打印,就像解释器本身在打印未处理异常时所做的。3.5 版改变: The etype 自变量被忽略并推断从类型对于 value .
3.10 版改变: The etype parameter has been renamed to exc and is now positional-only.
-