Skip to content

feat: weakref -- ref, finalize, callbacks at gc.collect() - #272

Merged
jonruttan merged 1 commit into
mainfrom
feat/weakref
Nov 28, 2026
Merged

jonruttan merged 1 commit into
mainfrom
feat/weakref

Conversation

@jonruttan

Copy link
Copy Markdown
Owner

What

  • stdlib/weakref.py — ref / ReferenceType, finalize, getweakrefcount, getweakrefs.
  • lib/python/_weakref.x — the weak cell. It holds the object by address, which the collector does not follow, so the reference doesn't keep the object alive. A heap free hook runs after the mark and before the sweep. It flags each cell whose object is unmarked as dead. It flags a cell whose own ref is unmarked as gone: a ref that dies first calls no callback, as in CPython. The hook only sets an existing cell's state to a symbol and allocates nothing that outlives it.
  • gc.collect() now calls a python-gc-after hook (util.x) after collecting. weakref registers its callback runner there, and dead refs' callbacks run newest first.
  • parse.x — lambda **kw dropped its keyword collector (unexpected keyword argument). The lambda emitter now passes it to the prelude and to the signature.
  • D14-weakref spec: 3 cases, golden output from python3.15. README row added.

Divergences (in the module docstring)

  • With no reference counting, an object dies at a collection, and callbacks run when gc.collect() returns. When the runtime collects on its own, refs are cleared but their callbacks wait for the next gc.collect().
  • Finalizers don't run at exit.
  • ref(o) makes a new ref each time.
  • proxy, WeakMethod, WeakSet and the weak dicts aren't implemented yet.

Results

  • Full suite: 1291 tests, 0 failed.
  • All 6 micropython weakref_* cases match CPython: ref_basic, ref_collect, multiple_refs, callback_exception, finalize_basic, finalize_collect. Projected conformance: 635 → 641/646.
  • if-ladder and path-literal checks pass.

Found along the way (not fixed here)

  • A class nested directly in a class body fails to parse (class A:\n class B: ...). This is already broken on main. finalize uses a module-level _FinalizeInfo to avoid it, and the fix will be its own PR.

🤖 Generated with Claude Code

stdlib/weakref.py builds ref and finalize on lib/python/_weakref.x: a
weak cell holds its object by address, which the collector does not
follow, and a free hook flags the cells whose object (or whose ref) the
sweep is about to free.  gc.collect() then runs the callbacks of the
dead refs, newest first, through the new python-gc-after hook.

Also fixes lambda **kw, which the lambda emitter dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonruttan
jonruttan merged commit f5ce5a4 into main Nov 28, 2026
12 checks passed
@jonruttan
jonruttan deleted the feat/weakref branch November 28, 2026 20:02
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.

1 participant