Add copyreg.copy() and copyreg.deepcopy()
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Feature or enhancement
Proposal:
Add two registration functions to copyreg, following the copyreg.pickle() precedent:
copyreg.copy(type, function)— registers the shallow copy function for objects of type type; called asfunction(obj), like__copy__.copyreg.deepcopy(type, function)— registers the deep copy function; called asfunction(obj, memo), like__deepcopy__.
copy.copy() and copy.deepcopy() use the registered functions in preference to the special methods and the pickle interfaces. Registration is by exact type and does not affect pickling. This allows customizing copying of instances of types that cannot be modified — for example, treating an external library's immutable objects as atomic:
copyreg.deepcopy(thirdparty.Handle, lambda obj, memo: obj)
Today the only way to customize copying of a foreign type is copyreg.pickle(), which forces copy and pickle semantics to be the same. The private _copy_dispatch and _deepcopy_dispatch tables have been monkey-patched in the wild for lack of a public API — even by our own test_descr. The new registries replace this non-public way; _deepcopy_dispatch is now removed, as _copy_dispatch already was in gh-128118.
This was requested in gh-141737. Implementation is ready; PR to follow.
Has this already been discussed elsewhere?
Companion of PEP 837, whose Rationale notes that registries for other protocols will be proposed separately: https://discuss.python.org/t/pep-837-extensible-json-serialization/108124
Links to previous discussion of this feature:
- gh-141737 — the direct feature request
- gh-128118 — removal of the historical
_copy_dispatchtable
Linked PRs
- gh-153613
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先阅读 copyreg.copy() 和 copyreg.deepcopy() 提案,以及 copy.copy() 和 copy.deepcopy() 入口点。查看关联的 PR gh-153613,并验证确切类型注册的优先级高于特殊方法和 pickle 接口,同时复制操作仍与 pickling 分离。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- tooling
- Issue 类型
- 功能
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100