diff --git a/lib/matplotlib/path.py b/lib/matplotlib/path.py index bfc3250df647..45826439b91f 100644 --- a/lib/matplotlib/path.py +++ b/lib/matplotlib/path.py @@ -9,6 +9,7 @@ visualisation. """ +import copy from functools import lru_cache from weakref import WeakValueDictionary @@ -259,16 +260,13 @@ def readonly(self): """ return self._readonly - def __copy__(self): + def copy(self): """ Return a shallow copy of the `Path`, which will share the vertices and codes with the source `Path`. """ - import copy return copy.copy(self) - copy = __copy__ - def __deepcopy__(self, memo=None): """ Return a deepcopy of the `Path`. The `Path` will not be diff --git a/lib/matplotlib/tests/test_path.py b/lib/matplotlib/tests/test_path.py index ed818257bb17..be8e9d2b6ced 100644 --- a/lib/matplotlib/tests/test_path.py +++ b/lib/matplotlib/tests/test_path.py @@ -1,4 +1,3 @@ -import copy import re import numpy as np @@ -333,8 +332,28 @@ def test_path_deepcopy(): codes = [Path.MOVETO, Path.LINETO] path1 = Path(verts) path2 = Path(verts, codes) - copy.deepcopy(path1) - copy.deepcopy(path2) + path1_copy = path1.deepcopy() + path2_copy = path2.deepcopy() + assert path1 is not path1_copy + assert path1.vertices is not path1_copy.vertices + assert path2 is not path2_copy + assert path2.vertices is not path2_copy.vertices + assert path2.codes is not path2_copy.codes + + +def test_path_shallowcopy(): + # Should not raise any error + verts = [[0, 0], [1, 1]] + codes = [Path.MOVETO, Path.LINETO] + path1 = Path(verts) + path2 = Path(verts, codes) + path1_copy = path1.copy() + path2_copy = path2.copy() + assert path1 is not path1_copy + assert path1.vertices is path1_copy.vertices + assert path2 is not path2_copy + assert path2.vertices is path2_copy.vertices + assert path2.codes is path2_copy.codes @pytest.mark.parametrize('phi', np.concatenate([