Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 572f4a4

Browse files
committed
gh-108867: Add PyThreadState_GetUnsafe() function
Add PyThreadState_GetUnsafe() 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().
1 parent 6f8411c commit 572f4a4

File tree

10 files changed

+32
-8
lines changed

10 files changed

+32
-8
lines changed

Doc/c-api/init.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,17 @@ code, or when embedding the Python interpreter:
871871
the caller needn't check for ``NULL``).
872872
873873
874+
.. c:function:: PyThreadState* PyThreadState_GetUnsafe()
875+
876+
Similar to :c:func:`PyThreadState_Get`, but don't kill the process with a
877+
fatal error if it is NULL. The caller is responsible to check if the result
878+
is NULL.
879+
880+
.. versionadded:: 3.13
881+
In Python 3.5, the function was private and known as
882+
``_PyThreadState_UncheckedGet()``.
883+
884+
874885
.. c:function:: PyThreadState* PyThreadState_Swap(PyThreadState *tstate)
875886
876887
Swap the current thread state with the thread state given by the argument

Doc/whatsnew/3.13.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,13 @@ New Features
924924
references) now supports the :ref:`Limited API <limited-c-api>`.
925925
(Contributed by Victor Stinner in :gh:`108634`.)
926926

927+
* Add :c:func:`PyThreadState_GetUnsafe()` function: similar to
928+
:c:func:`PyThreadState_Get()`, but don't kill the process with a fatal error
929+
if it is NULL. The caller is responsible to check if the result is NULL.
930+
Previously, the function was private and known as
931+
``_PyThreadState_UncheckedGet()``.
932+
(Contributed by Victor Stinner in :gh:`108867`.)
933+
927934
Porting to Python 3.13
928935
----------------------
929936

Include/cpython/object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ PyAPI_FUNC(int) _PyTrash_cond(PyObject *op, destructor dealloc);
425425
/* If "cond" is false, then _tstate remains NULL and the deallocator \
426426
* is run normally without involving the trashcan */ \
427427
if (cond) { \
428-
_tstate = _PyThreadState_UncheckedGet(); \
428+
_tstate = PyThreadState_GetUnsafe(); \
429429
if (_PyTrash_begin(_tstate, _PyObject_CAST(op))) { \
430430
break; \
431431
} \

Include/cpython/pystate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ struct _ts {
210210

211211
/* Similar to PyThreadState_Get(), but don't issue a fatal error
212212
* if it is NULL. */
213-
PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
213+
PyAPI_FUNC(PyThreadState *) PyThreadState_GetUnsafe(void);
214214

215215
// Disable tracing and profiling.
216216
PyAPI_FUNC(void) PyThreadState_EnterTracing(PyThreadState *tstate);

Include/internal/pycore_pystate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ PyAPI_FUNC(PyThreadState *) _PyThreadState_GetCurrent(void);
7777
7878
The caller must hold the GIL.
7979
80-
See also PyThreadState_Get() and _PyThreadState_UncheckedGet(). */
80+
See also PyThreadState_Get() and PyThreadState_GetUnsafe(). */
8181
static inline PyThreadState*
8282
_PyThreadState_GET(void)
8383
{

Include/pystate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
5656
5757
The caller must hold the GIL.
5858
59-
See also _PyThreadState_UncheckedGet() and _PyThreadState_GET(). */
59+
See also PyThreadState_GetUnsafe() and _PyThreadState_GET(). */
6060
PyAPI_FUNC(PyThreadState *) PyThreadState_Get(void);
6161

6262
// Alias to PyThreadState_Get()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Add :c:func:`PyThreadState_GetUnsafe()` function: similar to
2+
:c:func:`PyThreadState_Get()`, but don't kill the process with a fatal error if
3+
it is NULL. The caller is responsible to check if the result is NULL.
4+
Previously, the function was private and known as
5+
``_PyThreadState_UncheckedGet()``. Patch by Victor Stinner.

Modules/_testcapimodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,8 +2445,8 @@ test_tstate_capi(PyObject *self, PyObject *Py_UNUSED(args))
24452445
PyThreadState *tstate2 = PyThreadState_Get();
24462446
assert(tstate2 == tstate);
24472447

2448-
// private _PyThreadState_UncheckedGet()
2449-
PyThreadState *tstate3 = _PyThreadState_UncheckedGet();
2448+
// PyThreadState_GetUnsafe()
2449+
PyThreadState *tstate3 = PyThreadState_GetUnsafe();
24502450
assert(tstate3 == tstate);
24512451

24522452
// PyThreadState_EnterTracing(), PyThreadState_LeaveTracing()

Modules/getpath.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "pycore_pathconfig.h" // _PyPathConfig_ReadGlobal()
77
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
88
#include "pycore_pymem.h" // _PyMem_RawWcsdup()
9+
#include "pycore_pystate.h" // _PyThreadState_GET()
910

1011
#include "marshal.h" // PyMarshal_ReadObjectFromString
1112
#include "osdefs.h" // DELIM
@@ -821,7 +822,7 @@ _PyConfig_InitPathConfig(PyConfig *config, int compute_path_config)
821822
return status;
822823
}
823824

824-
if (!_PyThreadState_UncheckedGet()) {
825+
if (!_PyThreadState_GET()) {
825826
return PyStatus_Error("cannot calculate path configuration without GIL");
826827
}
827828

Python/pystate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1828,7 +1828,7 @@ PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
18281828
//---------------------------------
18291829

18301830
PyThreadState *
1831-
_PyThreadState_UncheckedGet(void)
1831+
PyThreadState_GetUnsafe(void)
18321832
{
18331833
return current_fast_get(&_PyRuntime);
18341834
}

0 commit comments

Comments
 (0)