-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix problem with (deep)copy of TextPath #20921
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
Changes from all commits
d950e5a
6db9e95
c2785a1
c633e33
065528c
ebebec0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from copy import copy, deepcopy | ||
|
||
from numpy.testing import assert_array_equal | ||
from matplotlib.textpath import TextPath | ||
|
||
|
||
def test_set_size(): | ||
"""Set size with zero offset should scale vertices and retain codes.""" | ||
path = TextPath((0, 0), ".") | ||
_size = path.get_size() | ||
verts = path.vertices.copy() | ||
codes = path.codes.copy() | ||
path.set_size(20) | ||
assert_array_equal(verts/_size*path.get_size(), path.vertices) | ||
assert_array_equal(codes, path.codes) | ||
|
||
|
||
def test_deepcopy(): | ||
path = TextPath((0, 0), ".") | ||
path_copy = deepcopy(path) | ||
assert isinstance(path_copy, TextPath) | ||
assert path is not path_copy | ||
assert path.vertices is not path_copy.vertices | ||
assert path.codes is not path_copy.codes | ||
path = TextPath((0, 0), ".") | ||
path_copy = path.deepcopy({}) | ||
assert isinstance(path_copy, TextPath) | ||
assert path is not path_copy | ||
assert path.vertices is not path_copy.vertices | ||
assert path.codes is not path_copy.codes | ||
|
||
|
||
def test_copy(): | ||
path = TextPath((0, 0), ".") | ||
path_copy = copy(path) | ||
assert path is not path_copy | ||
assert path.vertices is path_copy.vertices | ||
assert path.codes is path_copy.codes | ||
path = TextPath((0, 0), ".") | ||
path_copy = path.copy() | ||
assert path is not path_copy | ||
assert path.vertices is path_copy.vertices | ||
assert path.codes is path_copy.codes |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from copy import deepcopy | ||
from collections import OrderedDict | ||
import functools | ||
import logging | ||
|
@@ -429,3 +430,23 @@ def _revalidate_path(self): | |
self._cached_vertices = tr.transform(self._vertices) | ||
self._cached_vertices.flags.writeable = False | ||
self._invalid = False | ||
|
||
def __deepcopy__(self, memo): | ||
"""Update path and create deep copy.""" | ||
self._revalidate_path() | ||
cls = self.__class__ | ||
new_instance = cls.__new__(cls) | ||
memo[id(self)] = new_instance | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need to fill in the memo yourself, |
||
for k, v in self.__dict__.items(): | ||
setattr(new_instance, k, deepcopy(v, memo)) | ||
return new_instance | ||
|
||
deepcopy = __deepcopy__ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need this as a public method? I'd say |
||
|
||
def __copy__(self): | ||
"""Update path and create shallow copy.""" | ||
self._revalidate_path() | ||
cls = self.__class__ | ||
new_instance = cls.__new__(cls) | ||
new_instance.__dict__.update(self.__dict__) | ||
return new_instance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this get a docstring, even if very cursory? I can't really tell what it is supposed to be testing...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added a simple docstring. I have revisited the test and I think that it tests the required functionality.