gh-124622: Add PyThreadState_Ensure() function by vstinner · Pull Request #130012 · python/cpython (original) (raw)

I'm a lot happier with this 😄! When this was first discussed a few months ago, Petr suggested using PyStatus as the return value to differentiate between failure reasons. So, something like this:

static int my_thread(PyInterpreterState interp) { int state; PyStatus status = PyThreadState_Ensure(interp, &state); if (PyStatus_Exception(status)) { // Log the error somehow / ... */ } PyThreadState_Release(state); }

I think that could be handy, especially for applications that want to continue running instead of just bailing out of the thread.