简体   繁体   English

Python追溯:“在程序控制下模仿解释器” +

[英]Python Tracebacks: 'Mimicking the interpreter'+'under program control'

The following is from the P3 documentation : 以下是来自P3文档的信息

"The [traceback] module provides a standard interface to extract, format and print stack traces of Python programs. It exactly mimics the behavior of the Python interpreter when it prints a stack trace. This is useful when you want to print stack traces under program control , such as in a “wrapper” around the interpreter." “ [traceback]模块提供了提取,格式化和打印Python程序的堆栈跟踪的标准接口。它精确地模拟了Python 解释器在打印堆栈跟踪时的行为。当您要在程序下打印堆栈跟踪时,这很有用。 控制 ,例如在解释器周围的“包装器”中。”

1) Why does the traceback module "mimic" the interpreter? 1)为什么回溯模块“模仿”解释器?

2) Why is this useful "under program control" (what does this phrase mean)? 2)为什么这个有用的“在程序控制下”(这句话是什么意思)?

From what I understand, by mimic the interpreter, it is meant that the formatting and wording on exception reporting is exactly similar to that performed by the interpreter. 据我了解,通过模仿解释器,这意味着异常报告的格式和措辞与解释器所执行的完全相似。 That is, this: 也就是说,这是:

import traceback
try:
    raise AttributeError("Foo")
except:
    traceback.print_exc()

Displays the same message as this would: 显示与以下内容相同的消息:

raise AttributeError("Foo")

which is: 这是:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
AttributeError: Foo

As for your second question, you can see an example of that in the examples section of the module documentation. 至于第二个问题,您可以在模块文档的“ 示例”部分中看到该示例。 The first example illustrates simple "wrapping" of the interpreter (with help from input and exec ) and reporting by using print_exc (which mimics the interpreter). 第一个示例说明了解释器的简单“包装”(在inputexec帮助下)和使用print_exc (模仿解释器)进行报告。

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

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