8000 bpo-40826: Add _Py_EnsureTstateNotNULL() macro (GH-20571) · python/cpython@859e131 · GitHub
[go: up one dir, main page]

Skip to content

Commit 859e131

Browse files
committed
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. (cherry picked from commit 3026cad)
1 parent 5d2396c commit 859e131

File tree

6 files changed

+40
-30
lines changed

6 files changed

+40
-30
lines changed

Include/internal/pycore_pystate.h

Lines changed: 18 additions & 1 deletion
91
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ _PyThreadState_GET(void)
7474
#undef PyThreadState_GET
7575
#define PyThreadState_GET() _PyThreadState_GET()
7676

77+
PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalError_TstateNULL(const char *func);
78+
79+
static inline void
80+
_Py_EnsureFuncTstateNotNULL(const char *func, PyThreadState *tstate)
81+
{
82+
if (tstate == NULL) {
83+
_Py_FatalError_TstateNULL(func);
84+
}
85+
}
86+
87+
// Call Py_FatalError() if tstate is NULL
88+
#define _Py_EnsureTstateNotNULL(tstate) \
89+
_Py_EnsureFuncTstateNotNULL(__func__, tstate)
90+
+
7792
/* Get the current interpreter state.
7893
7994
The macro is unsafe: it does not check for error and it can return NULL.
@@ -84,7 +99,9 @@ _PyThreadState_GET(void)
8499
and _PyGILState_GetInterpreterStateUnsafe(). */
85100
static inline PyInterpreterState* _PyInterpreterState_GET(void) {
86101
PyThreadState *tstate = _PyThreadState_GET();
87-
assert(tstate != NULL);
102+
#ifdef Py_DEBUG
103+
_Py_EnsureTstateNotNULL(tstate);
104+
#endif
88105
return tstate->interp;
89106
}
90107

Lib/test/test_capi.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ def test_no_FatalError_infinite_loop(self):
6767
self.assertTrue(err.rstrip().startswith(
6868
b'Fatal Python error: '
6969
b'PyThreadState_Get: '
70-
b'current thread state is NULL (released GIL?)'))
70+
b'the function must be called with the GIL held, '
71+
b'but the GIL is released '
72+
b'(the current Python thread state is NULL)'),
73+
err)
7174

7275
def test_memoryview_from_NULL_pointer(self):
7376
self.assertRaises(ValueError, _testcapi.make_memoryview_from_NULL_pointer)

Python/ceval.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,13 @@ UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
240240
#endif
241241
#include "ceval_gil.h"
242242

243-
static void
244-
ensure_tstate_not_null(const char *func, PyThreadState *tstate)
243+
void _Py_NO_RETURN
244+
_Py_FatalError_TstateNULL(const char *func)
245245
{
246-
if (tstate == NULL) {
247-
_Py_FatalErrorFunc(func,
248-
"current thread state is NULL (released GIL?)");
249-
}
246+
_Py_FatalErrorFunc(func,
247+
"the function must be called with the GIL held, "
248+
"but the GIL is released "
249+
"(the current Python thread state is NULL)");
250250
}
251251

252252

@@ -346,7 +346,7 @@ PyEval_AcquireLock(void)
346346
{
347347
_PyRuntimeState *runtime = &_PyRuntime;
348348
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
349-
ensure_tstate_not_null(__func__, tstate);
349+
_Py_EnsureTstateNotNULL(tstate);
350350

351351
take_gil(tstate);
352352
}
@@ -375,7 +375,7 @@ _PyEval_ReleaseLock(PyThreadState *tstate)
375375
void
376376
PyEval_AcquireThread(PyThreadState *tstate)
377377
{
378-
ensure_tstate_not_null(__func__, tstate);
378+
_Py_EnsureTstateNotNULL(tstate);
379379

380380
take_gil(tstate);
381381

@@ -410,7 +410,7 @@ void
410410
_PyEval_ReInitThreads(_PyRuntimeState *runtime)
411411
{
412412
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
413-
ensure_tstate_not_null(__func__, tstate);
413+
_Py_EnsureTstateNotNULL(tstate);
414414

415415
struct _gil_runtime_state *gil = &runtime->ceval.gil;
416416
if (!gil_created(gil)) {
@@ -445,7 +445,7 @@ PyEval_SaveThread(void)
445445
{
446446
_PyRuntimeState *runtime = &_PyRuntime;
447447
PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
448-
ensure_tstate_not_null(__func__, tstate);
448+
_Py_EnsureTstateNotNULL(tstate);
449449

450450
struct _ceval_runtime_state *ceval = &runtime->ceval;
451451
struct _ceval_state *ceval2 = &tstate->interp->ceval;
@@ -457,7 +457,7 @@ PyEval_SaveThread(void)
457457
void
458458
PyEval_RestoreThread(PyThreadState *tstate)
459459
{
460-
ensure_tstate_not_null(__func__, tstate);
460+
_Py_EnsureTstateNotNULL(tstate);
461461

462462
take_gil(tstate);
463463

@@ -889,7 +889,7 @@ eval_frame_handle_pending(PyThreadState *tstate)
889889
PyObject* _Py_HOT_FUNCTION
890890
_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
891891
{
892-
ensure_tstate_not_null(__func__, tstate);
892+
_Py_EnsureTstateNotNULL(tstate);
893893

894894
#ifdef DXPAIRS
895895
int lastopcode = 0;

Python/errors.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,7 @@ void
14261426
_PyErr_WriteUnraisableMsg(const char *err_msg_str, PyObject *obj)
14271427
{
14281428
PyThreadState *tstate = _PyThreadState_GET();
1429-
assert(tstate != NULL);
1429+
_Py_EnsureTstateNotNULL(tstate);
14301430

14311431
PyObject *err_msg = NULL;
14321432
PyObject *exc_type, *exc_value, *exc_tb;

Python/pystate.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,6 @@ extern "C" {
3939
_Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
4040
(uintptr_t)(value))
4141

42-
static void
43-
ensure_tstate_not_null(const char *func, PyThreadState *tstate)
44-
{
45-
if (tstate == NULL) {
46-
_Py_FatalErrorFunc(func,
47-
"current thread state is NULL (released GIL?)");
48-
}
49-
}
50-
51-
5242
/* Forward declarations */
5343
static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
5444
static void _PyThreadState_Delete(PyThreadState *tstate, int check_current);
@@ -431,7 +421,7 @@ PyInterpreterState *
431421
PyInterpreterState_Get(void)
432422
{
433423
PyThreadState *tstate = _PyThreadState_GET();
434-
ensure_tstate_not_null(__func__, tstate);
424+
_Py_EnsureTstateNotNULL(tstate);
435425
PyInterpreterState *interp = tstate->interp;
436426
if (interp == NULL) {
437427
Py_FatalError("no current interpreter");
@@ -846,7 +836,7 @@ static void
846836
tstate_delete_common(PyThreadState *tstate,
847837
struct _gilstate_runtime_state *gilstate)
848838
{
849-
ensure_tstate_not_null(__func__, tstate);
839+
_Py_EnsureTstateNotNULL(tstate);
850840
PyInterpreterState *interp = tstate->interp;
851841
if (interp == NULL) {
852842
Py_FatalError("NULL interpreter");
@@ -897,7 +887,7 @@ PyThreadState_Delete(PyThreadState *tstate)
897887
void
898888
_PyThreadState_DeleteCurrent(PyThreadState *tstate)
899889
{
900-
ensure_tstate_not_null(__func__, tstate);
890+
_Py_EnsureTstateNotNULL(tstate);
901891
struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
902892
tstate_delete_common(tstate, gilstate);
903893
_PyRuntimeGILState_SetThreadState(gilstate, NULL);
@@ -967,7 +957,7 @@ PyThreadState *
967957
PyThreadState_Get(void)
968958
{
969959
PyThreadState *tstate = _PyThreadState_GET();
970-
ensure_tstate_not_null(__func__, tstate);
960+
_Py_EnsureTstateNotNULL(tstate);
971961
return tstate;
972962
}
973963

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ static PyObject *
457457
sys_audit(PyObject *self, PyObject *const *args, Py_ssize_t argc)
458458
{
459459
PyThreadState *tstate = _PyThreadState_GET();
460-
assert(tstate != NULL);
460+
_Py_EnsureTstateNotNULL(tstate);
461461

462462
if (argc == 0) {
463463
_PyErr_SetString(tstate, PyExc_TypeError,

0 commit comments

Comments
 (0)
0