]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-127314: Don't mention the GIL when calling without a thread state on the free...
authorPeter Bierma <zintensitydev@gmail.com>
Fri, 6 Dec 2024 15:58:19 +0000 (10:58 -0500)
committerGitHub <noreply@github.com>
Fri, 6 Dec 2024 15:58:19 +0000 (16:58 +0100)
Co-authored-by: Victor Stinner <vstinner@python.org>
Include/internal/pycore_pystate.h
Lib/test/test_capi/test_mem.py
Lib/test/test_capi/test_misc.py
Misc/NEWS.d/next/C_API/2024-11-26-22-06-10.gh-issue-127314.SsRrIu.rst [new file with mode: 0644]
Objects/obmalloc.c

index 54d8803bc0bdb651480b5a108198d4aa00ce9237..1e73e541ef8de03efefbb91399ed12d0a14edcc8 100644 (file)
@@ -190,10 +190,18 @@ static inline void
 _Py_EnsureFuncTstateNotNULL(const char *func, PyThreadState *tstate)
 {
     if (tstate == NULL) {
+#ifndef Py_GIL_DISABLED
         _Py_FatalErrorFunc(func,
             "the function must be called with the GIL held, "
             "after Python initialization and before Python finalization, "
             "but the GIL is released (the current Python thread state is NULL)");
+#else
+        _Py_FatalErrorFunc(func,
+            "the function must be called with an active thread state, "
+            "after Python initialization and before Python finalization, "
+            "but it was called without an active thread state. "
+            "Are you trying to call the C API inside of a Py_BEGIN_ALLOW_THREADS block?");
+#endif
     }
 }
 
index 6ab7b685c2e18bd04e1f8e4251a25db9f0249245..5035b2b4829bf64765efd3d5dbb0bf416b45f4a9 100644 (file)
@@ -68,8 +68,13 @@ class PyMemDebugTests(unittest.TestCase):
 
     def check_malloc_without_gil(self, code):
         out = self.check(code)
-        expected = ('Fatal Python error: _PyMem_DebugMalloc: '
-                    'Python memory allocator called without holding the GIL')
+        if not support.Py_GIL_DISABLED:
+            expected = ('Fatal Python error: _PyMem_DebugMalloc: '
+                        'Python memory allocator called without holding the GIL')
+        else:
+            expected = ('Fatal Python error: _PyMem_DebugMalloc: '
+                        'Python memory allocator called without an active thread state. '
+                        'Are you trying to call it inside of a Py_BEGIN_ALLOW_THREADS block?')
         self.assertIn(expected, out)
 
     def test_pymem_malloc_without_gil(self):
index 8e0271919cc8a559925df8289f2f6b6c8d9d0533..61512e610f46f22b52d035d943e54b9c5f576baa 100644 (file)
@@ -100,11 +100,18 @@ class CAPITest(unittest.TestCase):
         _rc, out, err = run_result
         self.assertEqual(out, b'')
         # This used to cause an infinite loop.
-        msg = ("Fatal Python error: PyThreadState_Get: "
-               "the function must be called with the GIL held, "
-               "after Python initialization and before Python finalization, "
-               "but the GIL is released "
-               "(the current Python thread state is NULL)").encode()
+        if not support.Py_GIL_DISABLED:
+            msg = ("Fatal Python error: PyThreadState_Get: "
+                   "the function must be called with the GIL held, "
+                   "after Python initialization and before Python finalization, "
+                   "but the GIL is released "
+                   "(the current Python thread state is NULL)").encode()
+        else:
+            msg = ("Fatal Python error: PyThreadState_Get: "
+                   "the function must be called with an active thread state, "
+                   "after Python initialization and before Python finalization, "
+                   "but it was called without an active thread state. "
+                   "Are you trying to call the C API inside of a Py_BEGIN_ALLOW_THREADS block?").encode()
         self.assertTrue(err.rstrip().startswith(msg),
                         err)
 
diff --git a/Misc/NEWS.d/next/C_API/2024-11-26-22-06-10.gh-issue-127314.SsRrIu.rst b/Misc/NEWS.d/next/C_API/2024-11-26-22-06-10.gh-issue-127314.SsRrIu.rst
new file mode 100644 (file)
index 0000000..8ea3c4e
--- /dev/null
@@ -0,0 +1,2 @@
+Improve error message when calling the C API without an active thread state
+on the :term:`free-threaded <free threading>` build.
index 2cc0377f68f990cc00b9819932e04ab16267a99e..b103deb01ca7129c9b210c777f5411ada0add284 100644 (file)
@@ -2910,9 +2910,16 @@ static inline void
 _PyMem_DebugCheckGIL(const char *func)
 {
     if (!PyGILState_Check()) {
+#ifndef Py_GIL_DISABLED
         _Py_FatalErrorFunc(func,
                            "Python memory allocator called "
                            "without holding the GIL");
+#else
+        _Py_FatalErrorFunc(func,
+                           "Python memory allocator called "
+                           "without an active thread state. "
+                           "Are you trying to call it inside of a Py_BEGIN_ALLOW_THREADS block?");
+#endif
     }
 }