gh-128844: Make _Py_TryIncref public as an unstable API. (#128926)

This exposes `_Py_TryIncref` as `PyUnstable_TryIncref()` and the helper
function `_PyObject_SetMaybeWeakref` as `PyUnstable_EnableTryIncRef`.

These are helpers for dealing with unowned references in a safe way,
particularly in the free threading build.

Co-authored-by: Petr Viktorin <encukou@gmail.com>
This commit is contained in:
Sam Gross 2025-01-28 14:32:27 -05:00 committed by GitHub
parent 7dd0a7e52e
commit d23f5701ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 160 additions and 0 deletions

View file

@ -2588,6 +2588,20 @@ PyUnstable_Object_EnableDeferredRefcount(PyObject *op)
#endif
}
int
PyUnstable_TryIncRef(PyObject *op)
{
return _Py_TryIncref(op);
}
void
PyUnstable_EnableTryIncRef(PyObject *op)
{
#ifdef Py_GIL_DISABLED
_PyObject_SetMaybeWeakref(op);
#endif
}
void
_Py_ResurrectReference(PyObject *op)
{