Add copyreg.copy() and copyreg.deepcopy()
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách đọc đề xuất về copyreg.copy() và copyreg.deepcopy() cùng với các entry point copy.copy() và copy.deepcopy(). Xem xét PR được liên kết gh-153613 và xác minh rằng các đăng ký kiểu chính xác được ưu tiên hơn các special method và giao diện pickle, trong khi việc sao chép vẫn tách biệt với pickling.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- tooling
- Loại issue
- Tính năng
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 25/100