Python Glossary Русский

all Python 3doctestexception

doctest.UnexpectedException

exceptions

Raised by DebugRunner when an example raises an exception its expected output did not allow for. The original error is in exc_info.

Syntax

except doctest.UnexpectedException as e: e.test, e.example, e.exc_info

exc_info is a (type, value, traceback) triple like sys.exc_info(); the original exception is e.exc_info[1]. Pass e.exc_info[2] to pdb.post_mortem() to land in the frame where it failed. If the expected output starts with Traceback, the exception is not unexpected but an ordinary check.

Examples

import doctest
test = doctest.DocTestParser().get_doctest(">>> int('x')\n0\n", {}, "t", None, 0)
try:
    doctest.DebugRunner().run(test)
except doctest.UnexpectedException as e:
    print(type(e.exc_info[1]).__name__, e.example.source.strip())  # → ValueError int('x')

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy