Skip to content

gh-74876: Support pickling weak references and weak containers#153818

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:weakref-native-pickle
Open

gh-74876: Support pickling weak references and weak containers#153818
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:weakref-native-pickle

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Jul 16, 2026

Copy link
Copy Markdown
Member

Weak references and weak containers can now be pickled.

  • 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. 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

* 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>
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33622637 | 📁 Comparing 5a7a1d8 against main (f62050d)

  🔍 Preview build  

4 files changed
± library/pickle.html
± library/weakref.html
± whatsnew/3.16.html
± whatsnew/changelog.html

@autoweave-bot

Copy link
Copy Markdown

Oh neat, this looks like it'll make handling weak references way less fussy! 😄
Explore here →

Subweave map of python/cpython#153818

Maintainer? Turn off weaves from non-maintainers →
Carefully crafted by Subweave · 🧶 used ~2.5m LLM tokens

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