Python Glossary Русский

Python 3.3+faulthandlerfunction

faulthandler.enable()

fault handler state

Enables printing the Python traceback when the process crashes hard: segfault, SIGABRT, SIGFPE and so on.

Syntax

faulthandler.enable(file=sys.stderr, all_threads=True, c_stack=True)

It catches what try/except cannot see: a crash inside a C extension, where there is no normal traceback at all. file needs a real file descriptor — io.StringIO() gives UnsupportedOperation — and must stay open while the handler is enabled. To enable it without editing code use -X faulthandler or PYTHONFAULTHANDLER; -X dev mode turns it on by itself.

Examples

import faulthandler, io
try:
    faulthandler.enable(file=io.StringIO())
except io.UnsupportedOperation as e:
    print(e)  # → fileno
with open('crash.log', 'w') as log:
    faulthandler.enable(file=log)
    print(faulthandler.is_enabled())  # → True
    faulthandler.disable()

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy