Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

PrinceNaroliya
Copy link
Contributor

@PrinceNaroliya PrinceNaroliya commented Aug 24, 2025

This PR updates the documentation of :c:func:PyDict_Next to clarify the
handling of references in the free-threaded build.

Currently, the docs mention that PyDict_Next is not safe in the free-threaded
build and suggest using a critical section. However, the returned pkey and
pvalue are borrowed references, which are only valid while the critical
section 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, and
suggests using :c:func:Py_NewRef to obtain strong references.

Rationale

  • Makes the documentation safer and clearer for users of the free-threaded
    build.
  • Resolves issue gh-138098.
  • Matches the behavior noted in gh-119438.

Example of new docs rendering

Screenshot 2025-08-24 125023

Thanks!


📚 Documentation preview 📚: https://cpython-previews--138106.org.readthedocs.build/

Copy link
Member

@ZeroIntensity ZeroIntensity left a 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
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
*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`).
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
create strong references (for example, with :c:func:`Py_NewRef`).
create :term:`strong references <strong reference>` (for example, with :c:func:`Py_NewRef`).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

2 participants