Repository navigation
gh-74876: Support pickling weak references and weak containers - #153818
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>
Documentation build overview
422 files changed ·
|
Co-authored-by: Muse Code (Meta Muse Spark) # Conflicts: # Doc/library/weakref.rst
|
|
@serhiy-storchaka, are you planning to fix the xpickle tests? |
|
aarch64 Ubuntu Oddballs 3.x started to fail since this change: https://buildbot.python.org/#/builders/1868/builds/2601 Well, see the "Buildbot failure" comment above with the big warnings :-) Example: |
|
Thank you. For now these tests should be skipped. #160082 |
|
See an alternative approach in #160219. |
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