Sometimes Python not only throws exception but also segfaults. Through many years of my experience with Python I saw many segfaults, half of them where inside binary modules (C libraries, i.e. .so/.pyd files), half of them where inside CPython binary itself. When segfault is issued then whole Python program finishes with crashdump (or silently). My question is if segfault happens in some block of code or thread is there any chance to catch it as regular Exception through except, and thus preventing whole program from crashing? It is known that you can use faulthandler, for example through python -q -X faulthandler. Then it creates following dump when segfaults: >>> import ctypes >>> ctypes.string_at(0) Fatal Python error: Segmentation fault Current thread 0x00007fb899f39700 (most recent call first): File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at File "<stdin>", line 1 in <module> Segmentation fault But this dump above finishes program entirely. Instead I want to catch this traceback as some standard Exception. Another question is whether I can catch segfault of Python code inside C API of PyRun_SimpleString() function? Continue reading...