Skip to content
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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Jan 16, 2025

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.


📚 Documentation preview 📚: https://cpython-previews--128926.org.readthedocs.build/en/128926/c-api/object.html#c.PyUnstable_TryIncRef

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.
@corona10 corona10 requested review from vstinner and encukou January 17, 2025 13:55
return _Py_TryIncref(op);
}

void
Copy link
Member

@corona10 corona10 Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the documentation, it should return int type :)
c:function:: int PyUnstable_EnableTryIncRef(PyObject *obj)
or you should fix the docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the return type in the docs.


.. versionadded:: 3.14

.. c:function:: int PyUnstable_EnableTryIncRef(PyObject *obj)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe void?

Copy link
Member

@vstinner vstinner left a 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.

}
Py_DECREF(op); // undo try-incref
Py_DECREF(op); // dealloc
assert(MyObject_dealloc_called == 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you reset the counter to 0 at the start of the function, so the function can be called multiple times?

@colesbury colesbury marked this pull request as ready for review January 17, 2025 18:00

PyUnstable_EnableTryIncRef(op);
#ifdef Py_GIL_DISABLED
// PyUnstable_EnableTryIncRef sets the shared flags to *at least*
Copy link
Contributor

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?

Comment on lines 635 to 636
This is intended as a building block for safely dealing with unowned
references without the overhead of creating a :c:type:`!PyWeakReference`.
Copy link
Contributor

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.

Copy link
Contributor Author

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.

* Note that this is a stealth reference: wr_object's refcount is

@encukou
Copy link
Member

encukou commented Jan 18, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants