]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-108867: Add PyThreadState_GetUnchecked() function (#108870)
authorVictor Stinner <vstinner@python.org>
Tue, 3 Oct 2023 16:53:51 +0000 (18:53 +0200)
committerGitHub <noreply@github.com>
Tue, 3 Oct 2023 16:53:51 +0000 (16:53 +0000)
Add PyThreadState_GetUnchecked() function: similar to
PyThreadState_Get(), but don't issue a fatal error if it is NULL. The
caller is responsible to check if the result is NULL. Previously,
this function was private and known as _PyThreadState_UncheckedGet().

Doc/c-api/init.rst
Doc/whatsnew/3.13.rst
Include/cpython/object.h
Include/cpython/pystate.h
Include/internal/pycore_pystate.h
Include/pystate.h
Misc/NEWS.d/next/C API/2023-09-04-11-47-12.gh-issue-108867.Cr_LKd.rst [new file with mode: 0644]
Modules/_testcapimodule.c
Modules/getpath.c
Python/pystate.c

index 60f5c81cff572c71f4ddb2a2b3b4c5a9ea946b7f..d164d1a752e2951f53b1814ebf607ba268ffc15e 100644 (file)
@@ -870,6 +870,19 @@ code, or when embedding the Python interpreter:
    When the current thread state is ``NULL``, this issues a fatal error (so that
    the caller needn't check for ``NULL``).
 
+   See also :c:func:`PyThreadState_GetUnchecked`.
+
+
+.. c:function:: PyThreadState* PyThreadState_GetUnchecked()
+
+   Similar to :c:func:`PyThreadState_Get`, but don't kill the process with a
+   fatal error if it is NULL. The caller is responsible to check if the result
+   is NULL.
+
+   .. versionadded:: 3.13
+      In Python 3.5 to 3.12, the function was private and known as
+      ``_PyThreadState_UncheckedGet()``.
+
 
 .. c:function:: PyThreadState* PyThreadState_Swap(PyThreadState *tstate)
 
index a99d1141f53d77bcc7aadef3741fb45e3b921248..785deea1c1e48fd61c18215bd09d2fbba866e48e 100644 (file)
@@ -1003,6 +1003,13 @@ New Features
   functions on Python 3.11 and 3.12.
   (Contributed by Victor Stinner in :gh:`107073`.)
 
+* Add :c:func:`PyThreadState_GetUnchecked()` function: similar to
+  :c:func:`PyThreadState_Get()`, but don't kill the process with a fatal error
+  if it is NULL. The caller is responsible to check if the result is NULL.
+  Previously, the function was private and known as
+  ``_PyThreadState_UncheckedGet()``.
+  (Contributed by Victor Stinner in :gh:`108867`.)
+
 Porting to Python 3.13
 ----------------------
 
index 3838f19c75a230f86029a127e53ca20db86d6ede..ede394d9673d7e1d3da2125496ca12464722957a 100644 (file)
@@ -425,7 +425,7 @@ PyAPI_FUNC(int) _PyTrash_cond(PyObject *op, destructor dealloc);
         /* If "cond" is false, then _tstate remains NULL and the deallocator \
          * is run normally without involving the trashcan */ \
         if (cond) { \
-            _tstate = _PyThreadState_UncheckedGet(); \
+            _tstate = PyThreadState_GetUnchecked(); \
             if (_PyTrash_begin(_tstate, _PyObject_CAST(op))) { \
                 break; \
             } \
index af5cc4a2f3bf63e7631fd53c6228094b4990318f..8fd06242bc82e03b4afdd666276ed780dc4c2da1 100644 (file)
@@ -218,7 +218,7 @@ struct _ts {
 
 /* Similar to PyThreadState_Get(), but don't issue a fatal error
  * if it is NULL. */
-PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
+PyAPI_FUNC(PyThreadState *) PyThreadState_GetUnchecked(void);
 
 
 // Disable tracing and profiling.
index 5f8b576e4a69abdee30a0284f83b64dbae0dfb8f..a60d949bff1ebab6dfa3f56e307f09aba73221d2 100644 (file)
@@ -93,7 +93,7 @@ PyAPI_FUNC(PyThreadState *) _PyThreadState_GetCurrent(void);
 
    The caller must hold the GIL.
 
-   See also PyThreadState_Get() and _PyThreadState_UncheckedGet(). */
+   See also PyThreadState_Get() and PyThreadState_GetUnchecked(). */
 static inline PyThreadState*
 _PyThreadState_GET(void)
 {
index e6b4de979c87b885cb6f1f33186d03e726a518b4..727b8fbfffe0e674c48e3b0594dc5c54f2221f97 100644 (file)
@@ -56,7 +56,7 @@ PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
 
    The caller must hold the GIL.
 
-   See also _PyThreadState_UncheckedGet() and _PyThreadState_GET(). */
+   See also PyThreadState_GetUnchecked() and _PyThreadState_GET(). */
 PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);
 
 // Alias to PyThreadState_Get()
diff --git a/Misc/NEWS.d/next/C API/2023-09-04-11-47-12.gh-issue-108867.Cr_LKd.rst b/Misc/NEWS.d/next/C API/2023-09-04-11-47-12.gh-issue-108867.Cr_LKd.rst
new file mode 100644 (file)
index 0000000..2f56466
--- /dev/null
@@ -0,0 +1,5 @@
+Add :c:func:`PyThreadState_GetUnchecked()` function: similar to
+:c:func:`PyThreadState_Get()`, but don't kill the process with a fatal error if
+it is NULL. The caller is responsible to check if the result is NULL.
+Previously, the function was private and known as
+``_PyThreadState_UncheckedGet()``. Patch by Victor Stinner.
index 64bcb49d3657748c13bce9fa51f171d8737e4a57..a46d986c18ecd47e55ef74326fdebb0ffd9d7ca4 100644 (file)
@@ -2458,8 +2458,8 @@ test_tstate_capi(PyObject *self, PyObject *Py_UNUSED(args))
     PyThreadState *tstate2 = PyThreadState_Get();
     assert(tstate2 == tstate);
 
-    // private _PyThreadState_UncheckedGet()
-    PyThreadState *tstate3 = _PyThreadState_UncheckedGet();
+    // PyThreadState_GetUnchecked()
+    PyThreadState *tstate3 = PyThreadState_GetUnchecked();
     assert(tstate3 == tstate);
 
     // PyThreadState_EnterTracing(), PyThreadState_LeaveTracing()
index 3b926cac0d3f24ec2ebf30006ececad6da098500..6f76a84e78bf62570246d5fe6608af4959a39646 100644 (file)
@@ -6,6 +6,7 @@
 #include "pycore_pathconfig.h"    // _PyPathConfig_ReadGlobal()
 #include "pycore_pyerrors.h"      // _PyErr_WriteUnraisableMsg()
 #include "pycore_pymem.h"         // _PyMem_RawWcsdup()
+#include "pycore_pystate.h"       // _PyThreadState_GET()
 
 #include "marshal.h"              // PyMarshal_ReadObjectFromString
 #include "osdefs.h"               // DELIM
@@ -821,7 +822,7 @@ _PyConfig_InitPathConfig(PyConfig *config, int compute_path_config)
         return status;
     }
 
-    if (!_PyThreadState_UncheckedGet()) {
+    if (!_PyThreadState_GET()) {
         return PyStatus_Error("cannot calculate path configuration without GIL");
     }
 
index 25a957c31f013471f7660cd628618e1962b7abcb..ae33259f8df2f630092b5e44c48e157dfae9e290 100644 (file)
@@ -1908,7 +1908,7 @@ PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
 //---------------------------------
 
 PyThreadState *
-_PyThreadState_UncheckedGet(void)
+PyThreadState_GetUnchecked(void)
 {
     return current_fast_get(&_PyRuntime);
 }