When C extensions crash: easier debugging for your Python test suite

When C extensions crash: easier debugging for your Python test suite

This is extra fun when you get a silent crash half-way through a test run on your CI system:

In this article I’ll cover some ways you can prepare for crashes in advance, so when they do occur you can quickly figure out which part of the codebase caused them:

The Python standard library has a handy module called faulthandler that can print a traceback when a segfault occurs—that is, when a C extension crashes (the documentation has a nice example). The only caveat is that if the problem involved sufficiently bad memory corruption you won’t be able to get any useful output. Many test runners don’t print which tests are being run by default: you just get a list of dots:

The problem is that if you crash, and the only thing you have access to is that output, you won’t know exactly where the crash happened: you’ll know the test module, but what if your module has 100 tests, or these are integration tests that can call lots of different codepaths?

Source: pythonspeed.com