Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 45528c3

Browse files
authored
Merge pull request #20731 from deep-jkl/path-copy
Improved implementation of Path.copy and deepcopy
2 parents d2a9710 + dda3a9a commit 45528c3

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

lib/matplotlib/path.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
visualisation.
1010
"""
1111

12+
import copy
1213
from functools import lru_cache
1314
from weakref import WeakValueDictionary
1415

@@ -259,16 +260,13 @@ def readonly(self):
259260
"""
260261
return self._readonly
261262

262-
def __copy__(self):
263+
def copy(self):
263264
"""
264265
Return a shallow copy of the `Path`, which will share the
265266
vertices and codes with the source `Path`.
266267
"""
267-
import copy
268268
return copy.copy(self)
269269

270-
copy = __copy__
271-
272270
def __deepcopy__(self, memo=None):
273271
"""
274272
Return a deepcopy of the `Path`. The `Path` will not be

lib/matplotlib/tests/test_path.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import copy
21
import re
32

43
import numpy as np
@@ -333,8 +332,28 @@ def test_path_deepcopy():
333332
codes = [Path.MOVETO, Path.LINETO]
334333
path1 = Path(verts)
335334
path2 = Path(verts, codes)
336-
copy.deepcopy(path1)
337-
copy.deepcopy(path2)
335+
path1_copy = path1.deepcopy()
336+
path2_copy = path2.deepcopy()
337+
assert path1 is not path1_copy
338+
assert path1.vertices is not path1_copy.vertices
339+
assert path2 is not path2_copy
340+
assert path2.vertices is not path2_copy.vertices
341+
assert path2.codes is not path2_copy.codes
342+
343+
344+
def test_path_shallowcopy():
345+
# Should not raise any error
346+
verts = [[0, 0], [1, 1]]
347+
codes = [Path.MOVETO, Path.LINETO]
348+
path1 = Path(verts)
349+
path2 = Path(verts, codes)
350+
path1_copy = path1.copy()
351+
path2_copy = path2.copy()
352+
assert path1 is not path1_copy
353+
assert path1.vertices is path1_copy.vertices
354+
assert path2 is not path2_copy
355+
assert path2.vertices is path2_copy.vertices
356+
assert path2.codes is path2_copy.codes
338357

339358

340359
@pytest.mark.parametrize('phi', np.concatenate([

0 commit comments

Comments
 (0)