Skip to content

Commit 29eb0a8

Browse files
ZeroIntensityvstinner
authored andcommitted
pythongh-127314: Don't mention the GIL when calling without a thread state on the free-threaded build (python#127315)
Co-authored-by: Victor Stinner <[email protected]>
1 parent 8b7e7eb commit 29eb0a8

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

Include/internal/pycore_pystate.h

+8
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,18 @@ static inline void
190190
_Py_EnsureFuncTstateNotNULL(const char *func, PyThreadState *tstate)
191191
{
192192
if (tstate == NULL) {
193+
#ifndef Py_GIL_DISABLED
193194
_Py_FatalErrorFunc(func,
194195
"the function must be called with the GIL held, "
195196
"after Python initialization and before Python finalization, "
196197
"but the GIL is released (the current Python thread state is NULL)");
198+
#else
199+
_Py_FatalErrorFunc(func,
200+
"the function must be called with an active thread state, "
201+
"after Python initialization and before Python finalization, "
202+
"but it was called without an active thread state. "
203+
"Are you trying to call the C API inside of a Py_BEGIN_ALLOW_THREADS block?");
204+
#endif
197205
}
198206
}
199207

Lib/test/test_capi/test_mem.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,13 @@ def test_api_misuse(self):
6868

6969
def check_malloc_without_gil(self, code):
7070
out = self.check(code)
71-
expected = ('Fatal Python error: _PyMem_DebugMalloc: '
72-
'Python memory allocator called without holding the GIL')
71+
if not support.Py_GIL_DISABLED:
72+
expected = ('Fatal Python error: _PyMem_DebugMalloc: '
73+
'Python memory allocator called without holding the GIL')
74+
else:
75+
expected = ('Fatal Python error: _PyMem_DebugMalloc: '
76+
'Python memory allocator called without an active thread state. '
77+
'Are you trying to call it inside of a Py_BEGIN_ALLOW_THREADS block?')
7378
self.assertIn(expected, out)
7479

7580
def test_pymem_malloc_without_gil(self):

Lib/test/test_capi/test_misc.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,18 @@ def test_no_FatalError_infinite_loop(self):
100100
_rc, out, err = run_result
101101
self.assertEqual(out, b'')
102102
# This used to cause an infinite loop.
103-
msg = ("Fatal Python error: PyThreadState_Get: "
104-
"the function must be called with the GIL held, "
105-
"after Python initialization and before Python finalization, "
106-
"but the GIL is released "
107-
"(the current Python thread state is NULL)").encode()
103+
if not support.Py_GIL_DISABLED:
104+
msg = ("Fatal Python error: PyThreadState_Get: "
105+
"the function must be called with the GIL held, "
106+
"after Python initialization and before Python finalization, "
107+
"but the GIL is released "
108+
"(the current Python thread state is NULL)").encode()
109+
else:
110+
msg = ("Fatal Python error: PyThreadState_Get: "
111+
"the function must be called with an active thread state, "
112+
"after Python initialization and before Python finalization, "
113+
"but it was called without an active thread state. "
114+
"Are you trying to call the C API inside of a Py_BEGIN_ALLOW_THREADS block?").encode()
108115
self.assertTrue(err.rstrip().startswith(msg),
109116
err)
110117

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve error message when calling the C API without an active thread state
2+
on the :term:`free-threaded <free threading>` build.

Objects/obmalloc.c

+7
Original file line numberDiff line numberDiff line change
@@ -2910,9 +2910,16 @@ static inline void
29102910
_PyMem_DebugCheckGIL(const char *func)
29112911
{
29122912
if (!PyGILState_Check()) {
2913+
#ifndef Py_GIL_DISABLED
29132914
_Py_FatalErrorFunc(func,
29142915
"Python memory allocator called "
29152916
"without holding the GIL");
2917+
#else
2918+
_Py_FatalErrorFunc(func,
2919+
"Python memory allocator called "
2920+
"without an active thread state. "
2921+
"Are you trying to call it inside of a Py_BEGIN_ALLOW_THREADS block?");
2922+
#endif
29162923
}
29172924
}
29182925

0 commit comments

Comments
 (0)