From 82638117156529b838bcfe0b6975f713b699fed1 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 14 Nov 2026 20:11:58 +0100 Subject: [PATCH] Test Py_SET_TYPE() on limited C API --- docs/api.rst | 2 ++ tests/test_pythoncapi_compat.py | 6 ++++-- tests/test_pythoncapi_compat_cext.c | 11 ++++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index cf86c17..7f5ac3a 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -870,6 +870,8 @@ PyObject See `Py_SET_TYPE() documentation `__. + Not available on Free Threading with limited C API 3.15 or newer (abi3t). + .. c:function:: void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) See `Py_SET_SIZE() documentation `__. diff --git a/tests/test_pythoncapi_compat.py b/tests/test_pythoncapi_compat.py index 8ef3f24..d6eff38 100644 --- a/tests/test_pythoncapi_compat.py +++ b/tests/test_pythoncapi_compat.py @@ -166,13 +166,15 @@ def run_tests(build_dir, module_name, std): if VERBOSE: empty_line = False for attr in ('__cplusplus', 'PY_VERSION', 'PY_VERSION_HEX', - 'PYPY_VERSION', 'PYPY_VERSION_NUM', 'Py_LIMITED_API'): + 'PYPY_VERSION', 'PYPY_VERSION_NUM', 'Py_LIMITED_API', + 'Py_TARGET_ABI3T'): try: value = getattr(testmod, attr) except AttributeError: pass else: - if attr in ('PY_VERSION_HEX', 'Py_LIMITED_API', 'PYPY_VERSION_NUM'): + if attr in ('PY_VERSION_HEX', 'Py_LIMITED_API', + 'PYPY_VERSION_NUM', 'Py_TARGET_ABI3T'): value = "0x%x" % value print("%s: %s" % (attr, value)) empty_line = True diff --git a/tests/test_pythoncapi_compat_cext.c b/tests/test_pythoncapi_compat_cext.c index 45a3d6e..aee2f35 100644 --- a/tests/test_pythoncapi_compat_cext.c +++ b/tests/test_pythoncapi_compat_cext.c @@ -102,8 +102,8 @@ test_object(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) // Py_SET_REFCNT Py_SET_REFCNT(obj, Py_REFCNT(obj)); -#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 10) - // Py_SET_TYPE + // Py_SET_TYPE. Not implemented in the abi3t. +#ifndef Py_TARGET_ABI3T Py_SET_TYPE(obj, Py_TYPE(obj)); #endif // Py_SET_SIZE @@ -315,7 +315,7 @@ test_thread_state(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) static PyObject * test_interpreter(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored)) { - // On Windows,PyInterpreterState_Get() is not available in stable ABI 3.9, + // On Windows, PyInterpreterState_Get() is not available in stable ABI 3.9, // only in stable ABI 3.10. Don't test it on limited C API 3.9 on Unix to // make the test simpler. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 10) @@ -2802,6 +2802,11 @@ module_exec(PyObject *module) return -1; } #endif +#ifdef Py_TARGET_ABI3T + if (PyModule_AddIntMacro(module, Py_TARGET_ABI3T)) { + return -1; + } +#endif #if !defined(PYPY_VERSION) && !defined(Py_LIMITED_API) TryIncrefType.tp_name = "TryIncrefType"; TryIncrefType.tp_basicsize = sizeof(PyObject);