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

Commit54ac172

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 commit54ac172

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

‎lib/matplotlib/cbook.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,3 +2437,28 @@ def _auto_format_str(fmt, value):
24372437
returnfmt% (value,)
24382438
except (TypeError,ValueError):
24392439
returnfmt.format(value)
2440+
2441+
2442+
class_HideDeepcopyMeta(type):
2443+
"""Metaclass that allows conditionally hiding the __deepcopy__ method.
2444+
2445+
Set __hide_deepcopy__ to True to hide the __deepcopy__ method,
2446+
which will then be looked up in the usual method resolution order.
2447+
"""
2448+
2449+
def__new__(cls,name,bases,namespace,**kwargs):
2450+
orig_ga=namespace.get("__getattribute__")orobject.__getattribute__
2451+
2452+
def__getattribute__(self,attr_name):
2453+
ifattr_name=="__deepcopy__"andorig_ga(self,"__hide_deepcopy__"):
2454+
forbaseintype(self).__mro__[1:]:
2455+
ifattr_nameinbase.__dict__:
2456+
method=base.__dict__[attr_name]
2457+
returnmethod.__get__(self,type(self))
2458+
raiseAttributeError(
2459+
f"'{type(self).__name__}' object has no attribute '{attr_name}'"
2460+
)
2461+
returnorig_ga(self,attr_name)
2462+
2463+
namespace["__getattribute__"]=__getattribute__
2464+
returnsuper().__new__(cls,name,bases,namespace)

‎lib/matplotlib/path.py

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

1818
importmatplotlibasmpl
19+
1920
from .import_api,_path
20-
from .cbookimport_to_unmasked_float_array,simple_linear_interpolation
21+
from .cbookimport (
22+
_to_unmasked_float_array,simple_linear_interpolation,_HideDeepcopyMeta
23+
)
2124
from .bezierimportBezierSegment
2225

2326

24-
classPath:
27+
28+
classPath(metaclass=_HideDeepcopyMeta):
2529
"""
2630
A series of possibly disconnected, possibly closed, line and curve
2731
segments.
@@ -281,9 +285,13 @@ def __deepcopy__(self, memo=None):
281285
readonly, even if the source `Path` is.
282286
"""
283287
# Deepcopying arrays (vertices, codes) strips the writeable=False flag.
284-
p=copy.deepcopy(super(),memo)
285-
p._readonly=False
286-
returnp
288+
self.__hide_deepcopy__=True
289+
try:
290+
p=copy.deepcopy(self,memo)
291+
p._readonly=False
292+
returnp
293+
finally:
294+
self.__hide_deepcopy__=False
287295

288296
deepcopy=__deepcopy__
289297

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp