Skip to content

gh-74876: Add copyreg.Proxy - #160219

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:copyreg-proxy
Open

serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:copyreg-proxy

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

copyreg.Proxy(obj) is a wrapper that is pickled as the wrapped object. Like the referent of a weak reference, the wrapped object must be strongly referenced elsewhere in the same pickle; otherwise pickling raises PicklingError. Reduction functions can use it to pickle objects to which the reconstructed object only holds weak references.

It replaces the native support of weak references added in #153818: the picklers now only know about Proxy, and weakref.ref, WeakMethod, WeakSet, WeakValueDictionary and WeakKeyDictionary are reduced using it. This has two advantages:

  • The pickle data is more compact: the weak containers and WeakMethod pickle the weakly referenced objects themselves instead of weak references to them.
  • The pickle data can be unpickled in older Python versions: it no longer refers to names that do not exist there (weakref._dead_ref and private reconstructors). A dead reference is pickled as a reference to an empty set.

The copy module supports Proxy too: copy.copy() returns the wrapped object and copy.deepcopy() returns its deep copy.

🤖 Generated with Claude Code

A copyreg.Proxy is pickled as the wrapped object, which must be strongly
referenced elsewhere in the same pickle.  It replaces the native support
of weak references in pickle: weakref.ref, WeakMethod, WeakSet,
WeakValueDictionary and WeakKeyDictionary are now reduced using Proxy.
The copy module supports Proxy too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35318956 | 📁 Comparing 764297b against main (226ca06)

  🔍 Preview build  

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

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.

1 participant