python / python/cpython

`copy.copy` ignores methods of the object which are not part of the class

Đang mở
#132,272 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib type-bug
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ả

Bug report

Bug description:
from types import MethodType
from copy import copy, deepcopy

class MyClass:
    def __copy__(self):
        print("Call __copy__ of class")
        return self

    def __deepcopy__(self, memo):
        print("Call __deepcopy__ of class")
        return self

obj1 = MyClass()
obj2 = MyClass()

def copy_func(self):
    print("New copy function")
    return self

def deepcopy_func(self, memo):
    print("New deepcopy function")
    return self

obj11 = copy(obj1)
# > Call __copy__ of class
obj12 = deepcopy(obj1)
# > Call __deepcopy__ of class

obj21 = copy(obj2)
# > Call __copy__ of class
obj22 = deepcopy(obj2)
# > Call __deepcopy__ of class

obj1.__copy__ = copy_func.__get__(obj1)
obj1.__deepcopy__ = deepcopy_func.__get__(obj1)

obj2.__copy__ = MethodType(copy_func, obj2)
obj2.__deepcopy__ = MethodType(deepcopy_func, obj2)

obj13 = copy(obj1)
# > Call __copy__ of class
obj14 = deepcopy(obj1)
# > New deepcopy function

obj23 = copy(obj2)
# > Call __copy__ of class
obj24 = deepcopy(obj2)
# > New deepcopy function

Knowing, that binding additional methods to an object after instantiation might not be recommended, the behavior shown above confuses me. My expectation is that copy.copy and copy.deepcopy both use the __copy__/__deepcopy__ method of the actual object passed to it (code). However, copy.copy does not, but rather uses the __copy__ method of the class which could be different from that of the object (code).

Historically, the behavior of copy.copy and copy.deepcopy was first identical (c06e3acc735a6e9cf28d0f511493bcfd8829117d, using the method of the class in both cases), but lateron changed (e690883ccf8081e5baab0e9d71f596f26245b569).

Apart from the differences between the behavior of copy.copy and copy.deepcopy w.r.t. the __copy__/__deepcopy__ methods, the copy.copy method itself takes two different approaches when looking for methods to create the copy: __copy__ is searched inside the class, and in case it is not found, the __reduce_ex__/__reduce__ are looked up in the object (code).

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với Lib/copy.py tại các đường dẫn tra cứu copy và reduction được liên kết trong báo cáo, sau đó chạy reproducer được cung cấp trên phiên bản Python được hỗ trợ. Xem lại các commit lịch sử được đề cập để hiểu vì sao copy.copy và copy.deepcopy lại khác biệt. Công việc được xem là hoàn tất khi hành vi của các phương thức copydeepcopy gắn với instance được giải quyết nhất quán và được bao phủ bởi một regression test phù hợp.

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
backend
Loại issue
Lỗi
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.