diff options
author | Victor Stinner <vstinner@python.org> | 2020-06-01 14:02:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-01 14:02:40 (GMT) |
commit | 3026cad59b87751a9215111776cac8e819458fce (patch) | |
tree | b994018d331ca9f01f58ebd1a3d05c4f3aa1cdc0 /Include | |
parent | db64f12e4deda2abbafb6d2bd5c06762fca991ff (diff) | |
download | cpython-3026cad59b87751a9215111776cac8e819458fce.zip cpython-3026cad59b87751a9215111776cac8e819458fce.tar.gz cpython-3026cad59b87751a9215111776cac8e819458fce.tar.bz2 |
bpo-40826: Add _Py_EnsureTstateNotNULL() macro (GH-20571)
Add _Py_EnsureTstateNotNULL(tstate) macro: call Py_FatalError() if
tstate is NULL, the error message contains the current function name.
Diffstat (limited to 'Include')
-rw-r--r-- | Include/internal/pycore_pystate.h | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index d96ba31..7ac4ad5 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -86,6 +86,21 @@ _PyThreadState_GET(void) #undef PyThreadState_GET #define PyThreadState_GET() _PyThreadState_GET() +PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalError_TstateNULL(const char *func); + +static inline void +_Py_EnsureFuncTstateNotNULL(const char *func, PyThreadState *tstate) +{ + if (tstate == NULL) { + _Py_FatalError_TstateNULL(func); + } +} + +// Call Py_FatalError() if tstate is NULL +#define _Py_EnsureTstateNotNULL(tstate) \ + _Py_EnsureFuncTstateNotNULL(__func__, tstate) + + /* Get the current interpreter state. The macro is unsafe: it does not check for error and it can return NULL. @@ -96,7 +111,9 @@ _PyThreadState_GET(void) and _PyGILState_GetInterpreterStateUnsafe(). */ static inline PyInterpreterState* _PyInterpreterState_GET(void) { PyThreadState *tstate = _PyThreadState_GET(); - assert(tstate != NULL); +#ifdef Py_DEBUG + _Py_EnsureTstateNotNULL(tstate); +#endif return tstate->interp; } |