python / python/cpython

Add copyreg.copy() and copyreg.deepcopy()

未关闭
#153,612 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-feature
主要语言
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 as function(obj), like __copy__.
  • copyreg.deepcopy(type, function) — registers the deep copy function; called as function(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_dispatch table
Linked PRs
  • gh-153613

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 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

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。