Python Glossary Русский

all Python 3pdbfunction

pdb.post_mortem()

post-mortem debugging

Opens the debugger in the frame where an exception was raised, so you can inspect the variables of code that already failed.

Syntax

pdb.post_mortem(t=None)

Without an argument it takes the exception currently being handled, so it only works inside except; outside one it raises ValueError. Since 3.13 you may pass the exception object itself; on 3.11–3.12 only e.__traceback__. In 3.13+ the exceptions command walks chained exceptions (raise ... from).

Examples

import io, sys, pdb
def f():
    data = {'a': 1}
    return data['b']
sys.stdin = io.StringIO('p data\nq\n')
sys.stdout = log = io.StringIO()
try:
    f()
except KeyError as e:
    pdb.post_mortem(e.__traceback__)
sys.stdout = sys.__stdout__
print(log.getvalue().split('(Pdb) ')[1].strip())  # → {'a': 1}
import pdb
try:
    pdb.post_mortem()
except ValueError as e:
    print(type(e).__name__)  # → ValueError — вне except разбирать нечего

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy