-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
gh-138098: Clarify strong references in PyDict_Next docs for free-threaded build #138106
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
base: main
Are you sure you want to change the base?
gh-138098: Clarify strong references in PyDict_Next docs for free-threaded build #138106
Conversation
…ee-threaded build
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 mostly fine, we just need to add links to the terms.
@@ -258,6 +258,14 @@ Dictionary Objects | |||
value represents offsets within the internal dictionary structure, and | |||
since the structure is sparse, the offsets are not consecutive. | |||
.. note:: | |||
In the free-threaded build, this function can be used safely inside |
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 the free-threaded build, this function can be used safely inside | |
On the :term:`free threaded <free threading>` build, this function can be used safely inside |
In the free-threaded build, this function can be used safely inside | ||
a critical section. However, the references returned for *pkey* and | ||
*pvalue* are borrowed and only valid while the critical section is |
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.
*pvalue* are borrowed and only valid while the critical section is | |
*pvalue* are :term:`borrowed <borrowed reference>` and only valid while the critical section is |
a critical section. However, the references returned for *pkey* and | ||
*pvalue* are borrowed and only valid while the critical section is | ||
held. If you need to use these objects outside the critical section, | ||
create strong references (for example, with :c:func:`Py_NewRef`). |
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.
create strong references (for example, with :c:func:`Py_NewRef`). | |
create :term:`strong references <strong reference>` (for example, with :c:func:`Py_NewRef`). |
This PR updates the documentation of :c:func:
PyDict_Next
to clarify thehandling of references in the free-threaded build.
Currently, the docs mention that
PyDict_Next
is not safe in the free-threadedbuild and suggest using a critical section. However, the returned
pkey
andpvalue
are borrowed references, which are only valid while the criticalsection is held. If they need to be accessed outside of that critical section,
strong references must be created.
This PR adds a
.. note::
block explaining this behavior explicitly, andsuggests using :c:func:
Py_NewRef
to obtain strong references.Rationale
build.
Example of new docs rendering
Thanks!
📚 Documentation preview 📚: https://cpython-previews--138106.org.readthedocs.build/