gh-74876: Support pickling weak references and weak containers#153818
Open
serhiy-storchaka wants to merge 1 commit into
Open
gh-74876: Support pickling weak references and weak containers#153818serhiy-storchaka wants to merge 1 commit into
serhiy-storchaka wants to merge 1 commit into
Conversation
* weakref.ref is pickled by the pickler itself, as weakref.ref(referent). The referent is pickled weakly: it must be strongly referenced elsewhere in the same pickle, otherwise pickling raises PicklingError. A reference to a dead object is pickled by name, as the new weakref._dead_ref singleton. The callback is not preserved. * WeakMethod can now be pickled and copied: it reduces to a weak reference to the object the method is bound to. * WeakSet, WeakValueDictionary and WeakKeyDictionary can now be pickled: they reduce their elements, values and keys to weak references. WeakSet can now also be copied. Only existing opcodes are used, so no new pickle protocol is needed. The detection of weak references without a strong reference in the pickle is conservative: valid but exotic reference graphs may be refused. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
serhiy-storchaka
requested review from
AA-Turner and
kumaraditya303
as code owners
July 16, 2026 19:07
Documentation build overview
4 files changed± library/pickle.html± library/weakref.html± whatsnew/3.16.html± whatsnew/changelog.html |
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.
Weak references and weak containers can now be pickled.
weakref.refis pickled by the pickler itself, asweakref.ref(referent). The referent is pickled weakly: it must be strongly referenced elsewhere in the same pickle, otherwise pickling raisesPicklingError. A reference to a dead object is pickled by name, as the newweakref._dead_refsingleton. The callback is not preserved.WeakMethodcan now be pickled and copied: it reduces to a weak reference to the object the method is bound to.WeakSet,WeakValueDictionaryandWeakKeyDictionarycan now be pickled: they reduce their elements, values and keys to weak references.WeakSetcan now also be copied.Only existing opcodes are used, so no new pickle protocol is needed. Both the C and the Python pickler emit identical streams.
The detection of weak references without a strong reference in the pickle is a single conservative rule (fail-closed): a referent is tagged when it is first reached only through a weak reference, and any completed non-weak save of it clears the tag.
Pickling a proxy is out of scope here: making it round-trip as a proxy also changes copying, so it will be proposed separately in gh-62268.
🤖 Generated with Claude Code