The following is from the P3 documentation:
"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."
1) Why does the traceback module "mimic" the interpreter?
2) Why is this useful "under program control" (what does this phrase mean)?
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).