Add bindings for new PyUnstable APIs in 3.14
There are a few new C API additions in 3.14 that I think will help supporting the free-threaded build:
-
PyUnstable_Object_IsUniquelyReferencedReplaces uses of
Py_REFCNT(obj) == 1. This is needed for certain optimizations and it'd be nice to be able to do those optimizations safely on the free-threaded build. -
PyUnstable_Object_IsUniquelyReferencedTemporaryMore niche, used to detect uniquely referenced temporaries created by certain Python bytecode operations. NumPy uses this along with some code to scan the call stack to elide temporaries that are created by NumPy internals or by Python bytecode. It's pretty cursed. There are probably less cursed ways to use this.
-
PyUnstable_EnableTryIncrefandPyUnstable_TryIncrefSee the docs for
PyUnstable_TryIncRef. This is needed in some advanced cases to properly handle weak references without lots of overhead. I think there are other cases as well.
I created a C backport for IsUniquelyReferenced, see: https://github.com/python/pythoncapi-compat/pull/149
That might be useful for inspiration if anyone wants to write a safe wrapper that works on older Python versions.