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

Skip to content

Improved implementation of Path.copy and deepcopy #20731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/matplotlib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
visualisation.
"""

import copy
from functools import lru_cache
from weakref import WeakValueDictionary

Expand Down Expand Up @@ -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
Expand Down
25 changes: 22 additions & 3 deletions lib/matplotlib/tests/test_path.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import copy
import re

import numpy as np
Expand Down Expand Up @@ -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([
Expand Down