-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-128844: Make _Py_TryIncref
public as an unstable API.
#128926
Conversation
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe void?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
* Note that this is a stealth reference: wr_object's refcount is |
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I think this is better, especially with the example. I think we should also include the use of PyUnstable_EnableTryIncref
in the example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the branch.
f5507d8
to
405952c
Compare
@encukou - would you please take another look at this when you get a chance? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thank you!
One more nitpick.
Co-authored-by: Petr Viktorin <[email protected]>
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_TryIncref
asPyUnstable_TryIncref()
and the helper function_PyObject_SetMaybeWeakref
asPyUnstable_EnableTryIncRef
.These are helpers for dealing with unowned references in a safe way, particularly in the free threading build.
_Py_TryIncref
public 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