8000 bpo-40826: Add _Py_EnsureTstateNotNULL() macro by vstinner · Pull Request #20571 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-40826: Add _Py_EnsureTstateNotNULL() macro #20571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
< 8000 div class="hide-sm hide-md">
Diff view
Diff view
19 changes: 18 additions & 1 deletion Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

8000
/* Get the current interpreter state.

The macro is unsafe: it does not check for error and it can return NULL.
Expand All @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ def test_no_FatalError_infinite_loop(self):
self.assertTrue(err.rstrip().startswith(
b'Fatal Python error: '
b'PyThreadState_Get: '
b'current thread state is NULL (released GIL?)'))
b'the function must be called with the GIL held, '
b'but the GIL is released '
b'(the current Python thread state is NULL)'),
err)

def test_memoryview_from_NULL_pointer(self):
self.assertRaises(ValueError, _testcapi.make_memoryview_from_NULL_pointer)
Expand Down
25 changes: 12 additions & 13 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,15 @@ UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
#endif
#include "ceval_gil.h"

static void
ensure_tstate_not_null(const char *func, PyThreadState *tstate)
void _Py_NO_RETURN
_Py_FatalError_TstateNULL(const char *func)
{
if (tstate == NULL) {
_Py_FatalErrorFunc(func,
"current thread state is NULL (released GIL?)");
}
_Py_FatalErrorFunc(func,
"the function must be called with the GIL held, "
"but the GIL is released "
"(the current Python thread state is NULL)");
}


#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
int
_PyEval_ThreadsInitialized(PyInterpreterState *interp)
Expand Down Expand Up @@ -374,7 +373,7 @@ PyEval_AcquireLock(void)
{
_PyRuntimeState *runtime = &_PyRuntime;
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
ensure_tstate_not_null(__func__, tstate);
_Py_EnsureTstateNotNULL(tstate);

take_gil(tstate);
}
Expand Down Expand Up @@ -403,7 +402,7 @@ _PyEval_ReleaseLock(PyThreadState *tstate)
void
PyEval_AcquireThread(PyThreadState *tstate)
{
ensure_tstate_not_null(__func__, tstate);
_Py_EnsureTstateNotNULL(tstate);

take_gil(tstate);

Expand Down Expand Up @@ -442,7 +441,7 @@ void
_PyEval_ReInitThreads(_PyRuntimeState *runtime)
{
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
ensure_tstate_not_null(__func__, tstate);
_Py_EnsureTstateNotNULL(tstate);

#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
Expand Down Expand Up @@ -486,7 +485,7 @@ PyEval_SaveThread(void)
#else
PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
#endif
ensure_tstate_not_null(__func__, tstate);
_Py_EnsureTstateNotNULL(tstate);

struct _ceval_runtime_state *ceval = &runtime->ceval;
struct _ceval_state *ceval2 = &tstate->interp->ceval;
Expand All @@ -502,7 +501,7 @@ PyEval_SaveThread(void)
void
PyEval_RestoreThread(PyThreadState *tstate)
{
ensure_tstate_not_null(__func__, tstate);
_Py_EnsureTstateNotNULL(tstate);

take_gil(tstate);

Expand Down Expand Up @@ -944,7 +943,7 @@ eval_frame_handle_pending(PyThreadState *tstate)
PyObject* _Py_HOT_FUNCTION
_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
{
ensure_tstate_not_null(__func__, tstate);
_Py_EnsureTstateNotNULL(tstate);

#ifdef DXPAIRS
int lastopcode = 0;
Expand Down
2 changes: 1 addition & 1 deletion Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ void
_PyErr_WriteUnraisableMsg(const char *err_msg_str, PyObject *obj)
{
PyThreadState *tstate = _PyThreadState_GET();
assert(tstate != NULL);
_Py_EnsureTstateNotNULL(tstate);

PyObject *err_msg = NULL;
PyObject *exc_type, *exc_value, *exc_tb;
Expand Down
18 changes: 4 additions & 14 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@ extern "C" {
_Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
(uintptr_t)(value))

static void
ensure_tstate_not_null(const char *func, PyThreadState *tstate)
{
if (tstate == NULL) {
_Py_FatalErrorFunc(func,
"current thread state is NULL (released GIL?)");
}
}


/* Forward declarations */
static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
static void _PyThreadState_Delete(PyThreadState *tstate, int check_current);
Expand Down Expand Up @@ -431,7 +421,7 @@ PyInterpreterState *
PyInterpreterState_Get(void)
{
PyThreadState *tstate = _PyThreadState_GET();
ensure_tstate_not_null(__func__, tstate);
_Py_EnsureTstateNotNULL(tstate);
PyInterpreterState *interp = tstate->interp;
if (interp == NULL) {
Py_FatalError("no current interpreter");
Expand Down Expand Up @@ -846,7 +836,7 @@ static void
tstate_delete_common(PyThreadState *tstate,
struct _gilstate_runtime_state *gilstate)
{
ensure_tstate_not_null(__func__, tstate);
_Py_EnsureTstateNotNULL(tstate);
PyInterpreterState *interp = tstate->interp;
if (interp == NULL) {
Py_FatalError("NULL interpreter");
Expand Down Expand Up @@ -897,7 +887,7 @@ PyThreadState_Delete(PyThreadState *tstate)
void
_PyThreadState_DeleteCurrent(PyThreadState *tstate)
{
ensure_tstate_not_null(__func__, tstate);
_Py_EnsureTstateNotNULL(tstate);
struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
tstate_delete_common(tstate, gilstate);
_PyRuntimeGILState_SetThreadState(gilstate, NULL);
Expand Down Expand Up @@ -975,7 +965,7 @@ PyThreadState *
PyThreadState_Get(void)
{
PyThreadState *tstate = _PyThreadState_GET();
ensure_tstate_not_null(__func__, tstate);
_Py_EnsureTstateNotNULL(tstate);
return tstate;
}

Expand Down
2 changes: 1 addition & 1 deletion Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ static PyObject *
sys_audit(PyObject *self, PyObject *const *args, Py_ssize_t argc)
{
PyThreadState *tstate = _PyThreadState_GET();
assert(tstate != NULL);
_Py_EnsureTstateNotNULL(tstate);

if (argc == 0) {
_PyErr_SetString(tstate, PyExc_TypeError,
Expand Down
0