Skip to content

gh-128008: Add PyWeakref_IsDead() #128009

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

Merged
merged 2 commits into from
Dec 19, 2024
Merged

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Dec 16, 2024

The PyWeakref_IsDead() function tests if a weak reference is dead without any side effects. Although you can also detect if a weak reference is dead using PyWeakref_GetRef(), that function returns a strong reference that must be Py_DECREF()'d, which can introduce side effects if the last reference is concurrently dropped.


📚 Documentation preview 📚: https://cpython-previews--128009.org.readthedocs.build/en/128009/c-api/weakref.html#c.PyWeakref_IsDead

The `PyWeakref_IsDead()` function tests if a weak reference is dead
without any side effects. Although you can also detect if a weak
reference is dead using `PyWeakref_GetRef()`, that function returns a
strong reference that must be `Py_DECREF()`'d, which can introduce side
effects if the last reference is concurrently dropped (at least in the
free threading build).
@colesbury colesbury marked this pull request as ready for review December 17, 2024 15:11
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.

You just need to add the function to What's New in Python 3.14 > C API > New features: https://docs.python.org/dev/whatsnew/3.14.html#id6

@encukou encukou merged commit 7b811d0 into python:main Dec 19, 2024
47 checks passed
@colesbury colesbury deleted the gh-128008-weakref-isdead branch December 19, 2024 15:20
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Dec 23, 2024
The `PyWeakref_IsDead()` function tests if a weak reference is dead
without any side effects. Although you can also detect if a weak
reference is dead using `PyWeakref_GetRef()`, that function returns a
strong reference that must be `Py_DECREF()`'d, which can introduce side
effects if the last reference is concurrently dropped (at least in the
free threading build).
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Jan 8, 2025
The `PyWeakref_IsDead()` function tests if a weak reference is dead
without any side effects. Although you can also detect if a weak
reference is dead using `PyWeakref_GetRef()`, that function returns a
strong reference that must be `Py_DECREF()`'d, which can introduce side
effects if the last reference is concurrently dropped (at least in the
free threading build).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants