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

Commitc7edce3

Browse files
Implement Path.__deepcopy__ avoiding infinite recursion
To deep copy an object without calling deepcopy on the object itself,create a new object of the correct class and iterate calling deepcopy onits __dict__.Closes#29157 without relying on private CPython methods.Does not fix the other issue with TransformNode.__copy__.Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parentfed8c20 commitc7edce3

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

‎lib/matplotlib/path.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,17 +275,37 @@ def copy(self):
275275
"""
276276
returncopy.copy(self)
277277

278-
def__deepcopy__(self,memo=None):
278+
def__deepcopy__(self,memo):
279279
"""
280280
Return a deepcopy of the `Path`. The `Path` will not be
281281
readonly, even if the source `Path` is.
282282
"""
283283
# Deepcopying arrays (vertices, codes) strips the writeable=False flag.
284-
p=copy.deepcopy(super(),memo)
284+
cls=type(self)
285+
memo[id(self)]=p=cls.__new__(cls)
286+
287+
fork,vinself.__dict__.items():
288+
setattr(p,k,copy.deepcopy(v,memo))
289+
285290
p._readonly=False
286291
returnp
287292

288-
deepcopy=__deepcopy__
293+
defdeepcopy(self,memo=None):
294+
"""
295+
Return a deep copy of the `Path`. The `Path` will not be readonly,
296+
even if the source `Path` is.
297+
298+
Parameters
299+
----------
300+
memo : dict, optional
301+
A dictionary to use for memoizing, passed to `copy.deepcopy`.
302+
303+
Returns
304+
-------
305+
Path
306+
A deep copy of the `Path`, but not readonly.
307+
"""
308+
returncopy.deepcopy(self,memo)
289309

290310
@classmethod
291311
defmake_compound_path_from_polys(cls,XY):

‎lib/matplotlib/path.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class Path:
4444
@property
4545
defreadonly(self)->bool: ...
4646
defcopy(self)->Path: ...
47-
def__deepcopy__(self,memo:dict[int,Any]|None= ...)->Path: ...
48-
deepcopy=__deepcopy__
47+
def__deepcopy__(self,memo:dict[int,Any])->Path: ...
48+
defdeepcopy(self,memo:dict[int,Any]|None=None)->Path: ...
4949

5050
@classmethod
5151
defmake_compound_path_from_polys(cls,XY:ArrayLike)->Path: ...

‎lib/matplotlib/tests/test_path.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,15 +355,49 @@ def test_path_deepcopy():
355355
# Should not raise any error
356356
verts= [[0,0], [1,1]]
357357
codes= [Path.MOVETO,Path.LINETO]
358-
path1=Path(verts)
359-
path2=Path(verts,codes)
358+
path1=Path(verts,readonly=True)
359+
path2=Path(verts,codes,readonly=True)
360360
path1_copy=path1.deepcopy()
361361
path2_copy=path2.deepcopy()
362362
assertpath1isnotpath1_copy
363363
assertpath1.verticesisnotpath1_copy.vertices
364+
assertnp.all(path1.vertices==path1_copy.vertices)
365+
assertpath1.readonly
366+
assertnotpath1_copy.readonly
364367
assertpath2isnotpath2_copy
365368
assertpath2.verticesisnotpath2_copy.vertices
369+
assertnp.all(path2.vertices==path2_copy.vertices)
366370
assertpath2.codesisnotpath2_copy.codes
371+
assertall(path2.codes==path2_copy.codes)
372+
assertpath2.readonly
373+
assertnotpath2_copy.readonly
374+
375+
376+
deftest_path_deepcopy_cycle():
377+
classPathWithCycle(Path):
378+
def__init__(self,*args,**kwargs):
379+
super().__init__(*args,**kwargs)
380+
self.x=self
381+
382+
p=PathWithCycle([[0,0], [1,1]],readonly=True)
383+
p_copy=p.deepcopy()
384+
assertp_copyisnotp
385+
assertp.readonly
386+
assertnotp_copy.readonly
387+
assertp_copy.xisp_copy
388+
389+
classPathWithCycle2(Path):
390+
def__init__(self,*args,**kwargs):
391+
super().__init__(*args,**kwargs)
392+
self.x= [self]*2
393+
394+
p2=PathWithCycle2([[0,0], [1,1]],readonly=True)
395+
p2_copy=p2.deepcopy()
396+
assertp2_copyisnotp2
397+
assertp2.readonly
398+
assertnotp2_copy.readonly
399+
assertp2_copy.x[0]isp2_copy
400+
assertp2_copy.x[1]isp2_copy
367401

368402

369403
deftest_path_shallowcopy():

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp