Skip to content

Commit 9812c92

Browse files
authored
bugfix for WeakMethod deepcopy (#4815)
1 parent 2b10f15 commit 9812c92

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

paddlex/inference/common/result/base_result.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import copy
1615
import inspect
1716
import random
1817
import time
@@ -26,6 +25,18 @@
2625
from .mixin import JsonMixin, StrMixin
2726

2827

28+
class CopyableWeakMethod(weakref.WeakMethod):
29+
"""
30+
A weak method that can be deep copied.
31+
"""
32+
33+
def __copy__(self):
34+
return self
35+
36+
def __deepcopy__(self, memo):
37+
return self.__copy__()
38+
39+
2940
class AutoWeakList(UserList):
3041
"""
3142
A list that automatically removes weak references to items.
@@ -38,14 +49,14 @@ def append(self, item):
3849
Otherwise, append the item itself.
3950
"""
4051
if inspect.ismethod(item):
41-
super().append(weakref.WeakMethod(item))
52+
super().append(CopyableWeakMethod(item))
4253
else:
4354
super().append(item)
4455

4556
def __iter__(self):
4657
"""Iterate over items in the list."""
4758
for item in self.data:
48-
if isinstance(item, weakref.WeakMethod):
59+
if isinstance(item, CopyableWeakMethod):
4960
func = item()
5061
if func is not None:
5162
yield func
@@ -55,22 +66,11 @@ def __iter__(self):
5566
def __getitem__(self, index):
5667
"""Get item at index."""
5768
item = super().__getitem__(index)
58-
if isinstance(item, weakref.WeakMethod):
69+
if isinstance(item, CopyableWeakMethod):
5970
func = item()
6071
return func
6172
return item
6273

63-
def __deepcopy__(self, memo):
64-
"""Deep copy the object using the provided memory map."""
65-
result = []
66-
for item in self.data:
67-
if isinstance(item, weakref.WeakMethod):
68-
func = weakref.WeakMethod(item())
69-
result.append(func)
70-
else:
71-
result.append(copy.deepcopy(item, memo))
72-
return result
73-
7474

7575
class BaseResult(dict, JsonMixin, StrMixin):
7676
"""Base class for result objects that can save themselves.

0 commit comments

Comments
 (0)