python / python/cpython

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

オープン
#132,272 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず Lib/copy.py の、報告でリンクされている copy と reduction lookup のパスを確認し、その後、提供されている reproducer をサポート対象の Python バージョンで実行します。copy.copy と copy.deepcopy の挙動がなぜ分岐したのかを理解するため、言及されている過去のコミットを確認します。インスタンスに束縛された copy メソッドと deepcopy メソッドの挙動が一貫して解決され、適切な回帰テストでカバーされれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。