Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit996b647

Browse files
committed
Implement Path.__deepcopy__ avoiding infinite recursion
Give it a metaclass that lets us remove the __deepcopy__ methodfrom sight when executing that method.Closes#29157 without relying on private CPython methods.Does not fix the other issue with TransformNode.__copy__.
1 parentbebb263 commit996b647

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

‎lib/matplotlib/path.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,38 @@
1616
importnumpyasnp
1717

1818
importmatplotlibasmpl
19+
1920
from .import_api,_path
2021
from .cbookimport_to_unmasked_float_array,simple_linear_interpolation
2122
from .bezierimportBezierSegment
2223

2324

24-
classPath:
25+
class_HideDeepcopyMeta(type):
26+
"""Metaclass that allows conditionally hiding the __deepcopy__ method.
27+
28+
Set __hide_deepcopy__ to True to hide the __deepcopy__ method,
29+
which will then be looked up in the usual method resolution order.
30+
"""
31+
32+
def__new__(cls,name,bases,namespace):
33+
orig_ga=namespace.get("__getattribute__")orobject.__getattribute__
34+
35+
def__getattribute__(self,attr_name):
36+
ifattr_name=="__deepcopy__"andorig_ga(self,"__hide_deepcopy__"):
37+
forbaseintype(self).__mro__[1:]:
38+
ifattr_nameinbase.__dict__:
39+
method=base.__dict__[attr_name]
40+
returnmethod.__get__(self,type(self))
41+
raiseAttributeError(
42+
f"'{type(self).__name__}' object has no attribute '{attr_name}'"
43+
)
44+
returnorig_ga(self,attr_name)
45+
46+
namespace["__getattribute__"]=__getattribute__
47+
returnsuper().__new__(cls,name,bases,namespace)
48+
49+
50+
classPath(metaclass=_HideDeepcopyMeta):
2551
"""
2652
A series of possibly disconnected, possibly closed, line and curve
2753
segments.
@@ -281,9 +307,13 @@ def __deepcopy__(self, memo=None):
281307
readonly, even if the source `Path` is.
282308
"""
283309
# Deepcopying arrays (vertices, codes) strips the writeable=False flag.
284-
p=copy.deepcopy(super(),memo)
285-
p._readonly=False
286-
returnp
310+
self.__hide_deepcopy__=True
311+
try:
312+
p=copy.deepcopy(self,memo)
313+
p._readonly=False
314+
returnp
315+
finally:
316+
self.__hide_deepcopy__=False
287317

288318
deepcopy=__deepcopy__
289319

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp