8000 gh-108867: Add PyThreadState_GetUnchecked() function (#108870) · python/cpython@d735016 · GitHub
[go: up one dir, main page]

Skip to content

Commit d735016

Browse files
authored
gh-108867: Add PyThreadState_GetUnchecked() function (#108870)
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().
1 parent 6ab6040 commit d735016

File tree

10 files changed

+34
-8
lines changed

10 files changed

+34
-8
lines changed

Doc/c-api/init.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,19 @@ code, or when embedding the Python interpreter:
870870
When the current thread state is ``NULL``, this issues a fatal error (so that
871871
the caller needn't check for ``NULL``).
872872
873+
See also :c:func:`PyThreadState_GetUnchecked`.
874+
875+
876+
.. c:function:: PyThreadState* PyThreadState_GetUnchecked()
877+
878+
Similar to :c:func:`PyThreadState_Get`, but don't kill the process with a
879+
fatal error if it is NULL. The caller is responsible to check if the result
880+
is NULL.
881+
882+
.. versionadded:: 3.13
883+
In Python 3.5 to 3.12, the function was private and known as
884+
``_PyThreadState_UncheckedGet()``.
885+
873886
874887
.. c:function:: PyThreadState* PyThreadState_Swap(PyThreadState *tstate)
875888

Doc/whatsnew/3.13.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,13 @@ New Features
10031003
functions on Python 3.11 and 3.12.
10041004
(Contributed by Victor Stinner in :gh:`107073`.)
10051005

1006+
* Add :c:func:`PyThreadState_GetUnchecked()` function: similar to
1007+
:c:func:`PyThreadState_Get()`, but don't kill the process with a fatal error
1008+
if it is NULL. The caller is responsible to check if the result is NULL.
1009+
Previously, the function was private and known as
1010+
``_PyThreadState_UncheckedGet()``.
1011+
(Contributed by Victor Stinner in :gh:`108867`.)
1012+
10061013
Porting to Python 3.13
10071014
----------------------
10081015

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_GetUnchecked(); \
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
@@ -218,7 +218,7 @@ struct _ts {
218218

219219
/* Similar to PyThreadState_Get(), but don't issue a fatal error
220220
* if it is NULL. */
221-
PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
221+
PyAPI_FUNC(PyThreadState *) PyThreadState_GetUnchecked(void);
222222

223223

224224
// Disable tracing and profiling.

Include/internal/pycore_pystate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ PyAPI_FUNC(PyThreadState *) _PyThreadState_GetCurrent(void);
9393
9494
The caller must hold the GIL.
9595
96-
See also PyThreadState_Get() and _PyThreadState_UncheckedGet(). */
96+
See also PyThreadState_Get() and PyThreadState_GetUnchecked(). */
9797
static inline PyThreadState*
9898
_PyThreadState_GET(void)
9999
{

Include/pystate.h

Lines changed: 1 addition & 1 deletion
6D40
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_GetUnchecked() 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_GetUnchecked()` 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
@@ -2458,8 +2458,8 @@ test_tstate_capi(PyObject *self, PyObject *Py_UNUSED(args))
24582458
PyThreadState *tstate2 = PyThreadState_Get();
24592459
assert(tstate2 == tstate);
24602460

2461-
// private _PyThreadState_UncheckedGet()
2462-
PyThreadState *tstate3 = _PyThreadState_UncheckedGet();
2461+
// PyThreadState_GetUnchecked()
2462+
PyThreadState *tstate3 = PyThreadState_GetUnchecked();
24632463
assert(tstate3 == tstate);
24642464

24652465
// 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
@@ -1908,7 +1908,7 @@ PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
19081908
//---------------------------------
19091909

19101910
PyThreadState *
1911-
_PyThreadState_UncheckedGet(void)
1911+
PyThreadState_GetUnchecked(void)
19121912
{
19131913
return current_fast_get(&_PyRuntime);
19141914
}

0 commit comments

Comments
 (0)
0