diff --git a/Doc/data/stable_abi.dat b/Doc/data/stable_abi.dat index 2d4278c9d97c85..5936ce128bdf60 100644 --- a/Doc/data/stable_abi.dat +++ b/Doc/data/stable_abi.dat @@ -46,6 +46,7 @@ macro,PyBUF_STRIDES,3.11,, macro,PyBUF_WRITABLE,3.11,, macro,PyBUF_WRITE,3.11,, data,PyBaseObject_Type,3.2,, +func,PyBool_Check,3.16,, func,PyBool_FromLong,3.2,, data,PyBool_Type,3.2,, func,PyBuffer_FillContiguousStrides,3.11,, diff --git a/Include/boolobject.h b/Include/boolobject.h index b56e2baecaa36c..0e7db5d47bf49c 100644 --- a/Include/boolobject.h +++ b/Include/boolobject.h @@ -9,7 +9,14 @@ extern "C" { // PyBool_Type is declared by object.h -#define PyBool_Check(x) Py_IS_TYPE((x), &PyBool_Type) +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 16) +PyAPI_FUNC(int) PyBool_Check(PyObject *x); +#endif +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 16) +# define PyBool_Check(x) Py_IS_TYPE((x), &PyBool_Type) +#else +# define PyBool_Check(x) PyBool_Check(_PyObject_CAST(x)) +#endif /* Py_False and Py_True are the only two bools in existence. */ diff --git a/Lib/test/test_stable_abi_ctypes.py b/Lib/test/test_stable_abi_ctypes.py index ac5c4296c663d0..f560f5cd472a79 100644 --- a/Lib/test/test_stable_abi_ctypes.py +++ b/Lib/test/test_stable_abi_ctypes.py @@ -55,6 +55,7 @@ def test_windows_feature_macros(self): "PyArg_VaParseTupleAndKeywords", "PyArg_ValidateKeywordArguments", "PyBaseObject_Type", + "PyBool_Check", "PyBool_FromLong", "PyBool_Type", "PyBuffer_FillContiguousStrides", diff --git a/Misc/NEWS.d/next/C_API/2026-05-08-16-36-36.gh-issue-000000.2F1XTY.rst b/Misc/NEWS.d/next/C_API/2026-05-08-16-36-36.gh-issue-000000.2F1XTY.rst new file mode 100644 index 00000000000000..070ac30961d7bd --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-05-08-16-36-36.gh-issue-000000.2F1XTY.rst @@ -0,0 +1,2 @@ +In the Limited C API 3.16 and newer, :c:func:`PyBool_Check` is now available +as a function instead of a macro. diff --git a/Misc/stable_abi.toml b/Misc/stable_abi.toml index 8fd7aba09241e6..7f61fffae4bc3b 100644 --- a/Misc/stable_abi.toml +++ b/Misc/stable_abi.toml @@ -2831,3 +2831,6 @@ [function.PyObject_CallFinalizerFromDealloc] added = '3.15' + +[function.PyBool_Check] + added = '3.16' diff --git a/Objects/boolobject.c b/Objects/boolobject.c index b694691ae4d0d1..7b090ff6b47df8 100644 --- a/Objects/boolobject.c +++ b/Objects/boolobject.c @@ -225,3 +225,8 @@ struct _longobject _Py_TrueStruct = { { 1 } } }; + +// Implementations for the Stable ABI + +#undef PyBool_Check +int PyBool_Check(PyObject *x) { return Py_IS_TYPE(x, &PyBool_Type); } diff --git a/PC/python3dll.c b/PC/python3dll.c index e0be9d65a93cda..f01f445e46f357 100755 --- a/PC/python3dll.c +++ b/PC/python3dll.c @@ -105,6 +105,7 @@ EXPORT_FUNC(PyArg_UnpackTuple) EXPORT_FUNC(PyArg_ValidateKeywordArguments) EXPORT_FUNC(PyArg_VaParse) EXPORT_FUNC(PyArg_VaParseTupleAndKeywords) +EXPORT_FUNC(PyBool_Check) EXPORT_FUNC(PyBool_FromLong) EXPORT_FUNC(PyBuffer_FillContiguousStrides) EXPORT_FUNC(PyBuffer_FillInfo)