Repository navigation
feat: weakref -- ref, finalize, callbacks at gc.collect() - #272
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 apython-gc-afterhook (util.x) after collecting. weakref registers its callback runner there, and dead refs' callbacks run newest first.lambda **kwdropped its keyword collector (unexpected keyword argument). The lambda emitter now passes it to the prelude and to the signature.Divergences (in the module docstring)
gc.collect()returns. When the runtime collects on its own, refs are cleared but their callbacks wait for the nextgc.collect().ref(o)makes a new ref each time.proxy,WeakMethod,WeakSetand the weak dicts aren't implemented yet.Results
weakref_*cases match CPython: ref_basic, ref_collect, multiple_refs, callback_exception, finalize_basic, finalize_collect. Projected conformance: 635 → 641/646.Found along the way (not fixed here)
class A:\n class B: ...). This is already broken on main.finalizeuses a module-level_FinalizeInfoto avoid it, and the fix will be its own PR.🤖 Generated with Claude Code