gh-128844: Make _Py_TryIncref public as an unstable API.#128926
gh-128844: Make _Py_TryIncref public as an unstable API.#128926colesbury merged 14 commits intopython:mainfrom
_Py_TryIncref public as an unstable API.#128926Conversation
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.
Doc/c-api/object.rst
Outdated
|
|
||
| .. versionadded:: 3.14 | ||
|
|
||
| .. c:function:: int PyUnstable_EnableTryIncRef(PyObject *obj) |
vstinner
left a comment
There was a problem hiding this comment.
LGTM. I just have a suggestion for the test.
Modules/_testcapi/object.c
Outdated
|
|
||
| PyUnstable_EnableTryIncRef(op); | ||
| #ifdef Py_GIL_DISABLED | ||
| // PyUnstable_EnableTryIncRef sets the shared flags to *at least* |
There was a problem hiding this comment.
This comment seems incomplete, can you elaborate it a bit more?
Doc/c-api/object.rst
Outdated
| This is intended as a building block for safely dealing with unowned | ||
| references without the overhead of creating a :c:type:`!PyWeakReference`. |
There was a problem hiding this comment.
In C API docs the term borrowed reference is more common than unowned reference, can you use that?
There's a glossary entry for it so it can be linked to that.
There was a problem hiding this comment.
I'll update the term, but I don't love the use of "borrowed reference" here. This seems different to me than the typical use of "borrowed reference" -- there's not a particular reference that's borrowed here. It's more like a building block for weak reference like objects without using PyWeakReference.
cpython/Include/cpython/weakrefobject.h
Line 12 in d95ba9f
|
Please don't merge yet, I'd like to try tweaking the docs wording, early next week
…On January 18, 2025 6:57:08 AM GMT+01:00, Donghee Na ***@***.***> wrote:
@corona10 approved this pull request.
--
Reply to this email directly or view it on GitHub:
#128926 (review)
You are receiving this because your review was requested.
Message ID: ***@***.***>
|
Doc/c-api/object.rst
Outdated
| This is intended as a building block for safely dealing with | ||
| :term:`borrowed references <borrowed reference>` without the overhead of | ||
| creating a :c:type:`!PyWeakReference`. |
There was a problem hiding this comment.
I think this should use weak reference, as a generic term. The “without the overhead” note implies that we don't mean the concrete PyWeakReference.
“Borrowed reference” has a specific meaning in CPython (in particular, obj of PyUnstable_TryIncRef is a borrowed reference).
“Unowned reference” would, IMO, need a definition.
I'd also warn that you typically need to control the deallocator (both TryIncRef and tp_dealloc need the same lock). Is that correct?
| This is intended as a building block for safely dealing with | |
| :term:`borrowed references <borrowed reference>` without the overhead of | |
| creating a :c:type:`!PyWeakReference`. | |
| This is intended as a building block for managing weak references without the overhead of | |
| creating a :c:type:`!PyWeakReference`. | |
| Typically, correct use of this function requires support from *obj*'s | |
| deallocator (:c:member:`~PyTypeObject.tp_dealloc`). |
Here is a larger change, with an example adapted from the issue (itself based on pybind11). I personally didn't “get” the function before seeing the example, but I don't know if I'm the target audience. I'm also not sure if there are other use cases this doesn't cover.
There was a problem hiding this comment.
Thanks, I think this is better, especially with the example. I think we should also include the use of PyUnstable_EnableTryIncref in the example.
f5507d8 to
405952c
Compare
|
@encukou - would you please take another look at this when you get a chance? |
encukou
left a comment
There was a problem hiding this comment.
Looks good, thank you!
One more nitpick.
Co-authored-by: Petr Viktorin <encukou@gmail.com>
78d29a3 to
4a87acb
Compare
|
Congrats Sam! Honestly, this API is weird/surprising (trying to INCREF, hu?), but it looks useful for Free Threading. The doc is nice, thanks @encukou for the example. |
This exposes
_Py_TryIncrefasPyUnstable_TryIncref()and the helper function_PyObject_SetMaybeWeakrefasPyUnstable_EnableTryIncRef.These are helpers for dealing with unowned references in a safe way, particularly in the free threading build.
_Py_TryIncrefpublic as an unstable API asPyUnstable_TryIncref()#128844📚 Documentation preview 📚: https://cpython-previews--128926.org.readthedocs.build/en/128926/c-api/object.html#c.PyUnstable_TryIncRef