Use new 3.14 C APIs when available (#5854)

* Use new 3.14 C APIs when available

Use the new "unstable" C APIs for the functions added in #5494.

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Sam Gross
2025-10-05 12:58:13 -04:00
committed by GitHub
parent 4dc33d6524
commit 9ea197627d
2 changed files with 7 additions and 5 deletions

View File

@@ -314,8 +314,9 @@ inline void traverse_offset_bases(void *valueptr,
#ifdef Py_GIL_DISABLED
inline void enable_try_inc_ref(PyObject *obj) {
// TODO: Replace with PyUnstable_Object_EnableTryIncRef when available.
// See https://github.com/python/cpython/issues/128844
# if PY_VERSION_HEX >= 0x030E00A4
PyUnstable_EnableTryIncRef(obj);
# else
if (_Py_IsImmortal(obj)) {
return;
}
@@ -330,6 +331,7 @@ inline void enable_try_inc_ref(PyObject *obj) {
return;
}
}
# endif
}
#endif

View File

@@ -253,9 +253,9 @@ PYBIND11_NOINLINE handle get_type_handle(const std::type_info &tp, bool throw_if
inline bool try_incref(PyObject *obj) {
// Tries to increment the reference count of an object if it's not zero.
// TODO: Use PyUnstable_TryIncref when available.
// See https://github.com/python/cpython/issues/128844
#ifdef Py_GIL_DISABLED
#if defined(Py_GIL_DISABLED) && PY_VERSION_HEX >= 0x030E00A4
return PyUnstable_TryIncRef(obj);
#elif defined(Py_GIL_DISABLED)
// See
// https://github.com/python/cpython/blob/d05140f9f77d7dfc753dd1e5ac3a5962aaa03eff/Include/internal/pycore_object.h#L761
uint32_t local = _Py_atomic_load_uint32_relaxed(&obj->ob_ref_local);