From 3fb9e479298e9a8cef74c0d48de11accaecf6aaa Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 3 Oct 2023 19:03:00 +0200 Subject: [PATCH] Add PyThreadState_GetUnchecked() --- docs/api.rst | 6 ++++++ docs/changelog.rst | 8 ++++++-- pythoncapi_compat.h | 10 ++++++++++ tests/test_pythoncapi_compat_cext.c | 5 +++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index c2564f5..9204d9a 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -107,6 +107,12 @@ Python 3.13 See `PyObject_ClearManagedDict() documentation `__. +.. c:function:: PyThreadState* PyThreadState_GetUnchecked(void) + + See `PyThreadState_GetUnchecked() documentation `__. + + Available on Python 3.5.2 and newer. + Python 3.12 ----------- diff --git a/docs/changelog.rst b/docs/changelog.rst index b5203b8..9fd3d06 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,8 +1,12 @@ Changelog ========= -* 2023-10-03: Add ``PyObject_VisitManagedDict()`` and - ``PyObject_ClearManagedDict()`` functions. +* 2023-10-03: Add functions: + + * ``PyObject_VisitManagedDict()`` + * ``PyObject_ClearManagedDict()`` + * ``PyThreadState_GetUnchecked()`` + * 2023-09-29: Add functions: * ``PyMapping_HasKeyWithError()`` diff --git a/pythoncapi_compat.h b/pythoncapi_compat.h index fa32887..dcd97ff 100644 --- a/pythoncapi_compat.h +++ b/pythoncapi_compat.h @@ -929,6 +929,16 @@ PyObject_ClearManagedDict(PyObject *obj) } #endif +// gh-108867 added PyThreadState_GetUnchecked() to Python 3.13.0a1 +// Python 3.5.2 added _PyThreadState_UncheckedGet(). +#if PY_VERSION_HEX >= 0x03050200 && PY_VERSION_HEX < 0x030D00A1 +static inline PyThreadState* +PyThreadState_GetUnchecked(void) +{ + return _PyThreadState_UncheckedGet(); +} +#endif + #ifdef __cplusplus } diff --git a/tests/test_pythoncapi_compat_cext.c b/tests/test_pythoncapi_compat_cext.c index b50333d..78ee1c8 100644 --- a/tests/test_pythoncapi_compat_cext.c +++ b/tests/test_pythoncapi_compat_cext.c @@ -318,6 +318,11 @@ test_thread_state(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) PyThreadState_LeaveTracing(tstate); #endif +#if PY_VERSION_HEX >= 0x03050200 + // PyThreadState_GetUnchecked() + assert(PyThreadState_GetUnchecked() == tstate); +#endif + Py_RETURN_NONE; }