-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Adding an unstable C API for unique references #133140
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
Comments
I'm not sure if it's tracked elsewhere, but I'd also like to see an API function so NumPy can replace this code: We had to add it to fix NumPy on 3.14. See numpy/numpy#28681. |
@ngoldbaum can you please open an issue for that? I'll mark it as a deferred blocker so it lands before 3.14. Sam told me it's temporary code so it's fine for now. Just pointing out that the |
Regarding the NumPy code, it can't use |
see #133164 |
Actually, is there any use case for exposing |
Yeah, cases like Lines 197 to 236 in c46635a
|
Does this mean that the
IMO, that's generally OK for unstable API. Don't forget docs & tests though. But if this was sent to the WG I'd ask to avoid documenting it in terms of reference counting, which is an implementation detail you shouldn't rely on (as recent developments like this and immortality show). |
Yes, but unless we want to revert PEP 703, I don't think there's any good way around that.
I think in-place updates would generally be checked by
(@colesbury Fact check, please!) But anyways, documenting this one in terms of reference counting would just make the API unclear. It acts as solely a replacement for |
Yeah, that's right. I think a static PyObject *
iter_next(PyObject *self)
{
MyIterObject *iter = (MyIterObject *)self;
if (PyUnstable_Object_IsUniquelyReferenced(iter->it_result) {
// optimization: reuse iter->it_result
// update iter->it_result
...
return Py_NewRef(iter->it_result);
}
else {
// create a new result value
return ...
}
} You need static PyObject *
my_nb_add(PyObject *a, PyObject *b)
{
if (PyUnstable_Object_IsUniqueReferencedTemporary(a)) {
// optimization: modify a in-place and return it
}
else {
// add `a` and `b` and return the result as a new object
}
}
|
Bikeshedding, but could the names be tightened up by making them about the reference rather than about the object being referenced? Something like:
|
That would be introducing a |
Will this be in 3.14? And is there a way to get the same behaviour in 3.13 where |
Ideally, this one will make it into 3.14 too. I don't know if it's right to mark this as a blocker. cc @hugovk as the RM--do you want this in 3.14? |
I don't think this new feature needs to go in 3.14. Having said that, I'd encourage the C API experts above to review the PR and we can include it if approved before tomorrow. |
I merged the PR to land the function in Python 3.14. It's an unstable API, it can still change until Python 3.14 final, but I don't expect changes in its API, more on its documentation. I close the issue since the function is added. |
Uh oh!
There was an error while loading. Please reload this page.
Feature or enhancement
Proposal:
In #132070, there was a race brought up in free-threading through use of
Py_REFCNT(op) == 1
. The fix is to use_PyObject_IsUniquelyReferenced
.There were a couple additional comments about how we should handle this for the public API (for example, @godlygeek suggested implementing
Py_REFCNT
as_PyObject_IsUniquelyReferenced(op) ? 1 : INT_MAX
). I think the best approach is to just expose an unstable API for this and point to it inPy_REFCNT
's documentation.I'm not imagining anything complex:
@encukou, do you mind if this skips the C API WG? I'm not sure there's much to discuss about the API, other than some light bikeshedding.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
PyUnstable_Object_IsUniquelyReferenced
for free-threading #133144The text was updated successfully, but these errors were encountered: