Skip to content

gh-102960: Make frames weak-referenceable#152838

Open
ambv wants to merge 3 commits into
python:mainfrom
ambv:weakref-frames
Open

gh-102960: Make frames weak-referenceable#152838
ambv wants to merge 3 commits into
python:mainfrom
ambv:weakref-frames

Conversation

@ambv

@ambv ambv commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Frame objects now support weak references, stored in an explicit f_weakreflist field, following the same pattern as generators and coroutines. Weakref clearing in frame_dealloc() uses FT_CLEAR_WEAKREFS(), which handles the free-threaded build correctly (unconditional PyObject_ClearWeakRefs() there, NULL-check fast path on the default build).

Py_TPFLAGS_MANAGED_WEAKREF was considered instead, but static builtin types must not carry it (see init_static_type()), and the managed pre-header would cost two extra words per frame object instead of one.

This is a redo of GH-102962 against current main. Compared to that PR it adds free-threading-safe weakref clearing, a more comprehensive test suite, and docs.

Tested on the default, free-threaded (--disable-gil), and JIT (--enable-experimental-jit) builds.

🤖 Generated with Claude Code

Add an explicit f_weakreflist field to the frame object, following the
same pattern as generators and coroutines, including free-threading-safe
weakref clearing via FT_CLEAR_WEAKREFS() in frame_dealloc().

Py_TPFLAGS_MANAGED_WEAKREF is not used because static builtin types
must not carry it (see init_static_type()) and the pre-header would
cost two extra words per frame instead of one.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@read-the-docs-community

read-the-docs-community Bot commented Jul 2, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33405080 | 📁 Comparing fd5ea22 against main (669299b)

  🔍 Preview build  

3 files changed
± library/weakref.html
± whatsnew/3.16.html
± whatsnew/changelog.html

ambv and others added 2 commits July 2, 2026 03:40
Exceptions raised in worker threads don't propagate to the unittest
result, so report weakref mismatches via a shared list instead.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Under --parallel-threads a concurrent collection can turn a test's own
gc.collect() into a no-op, leaving its cycles uncollected.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Comment thread Lib/test/test_frame.py
Frames support weak references (gh-102960).
"""

def make_frame(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we really need so many tests for this? A single test which verifies that frame objects are weakrefable and that when a frame dies the weakrefs gets cleared should be enough for this.

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.

2 participants