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

Skip to content

Commit ce1ead1

Browse files
committed
Cryptic (deep)copy of TextPath
This time, revalidation is enabled. Added test for member deepcopy().
1 parent abb83b9 commit ce1ead1

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/matplotlib/tests/test_textpath.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ def test_set_size():
1515

1616

1717
def test_deepcopy():
18-
# Should not raise any error
1918
path = TextPath((0, 0), ".")
2019
path_copy = deepcopy(path)
2120
assert isinstance(path_copy, TextPath)
2221
assert path is not path_copy
2322
assert path.vertices is not path_copy.vertices
2423
assert path.codes is not path_copy.codes
24+
path = TextPath((0, 0), ".")
25+
path_copy = path.deepcopy({})
26+
assert isinstance(path_copy, TextPath)
27+
assert path is not path_copy
28+
assert path.vertices is not path_copy.vertices
29+
assert path.codes is not path_copy.codes
2530

2631

2732
def test_copy():
28-
# Should not raise any error
2933
path = TextPath((0, 0), ".")
3034
path_copy = copy(path)
3135
assert path is not path_copy

lib/matplotlib/textpath.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from copy import deepcopy
12
from collections import OrderedDict
23
import functools
34
import logging
@@ -429,3 +430,23 @@ def _revalidate_path(self):
429430
self._cached_vertices = tr.transform(self._vertices)
430431
self._cached_vertices.flags.writeable = False
431432
self._invalid = False
433+
434+
def __deepcopy__(self, memo):
435+
# taken from https://stackoverflow.com/a/15774013
436+
self._revalidate_path()
437+
cls = self.__class__
438+
new_instance = cls.__new__(cls)
439+
memo[id(self)] = new_instance
440+
for k, v in self.__dict__.items():
441+
setattr(new_instance, k, deepcopy(v, memo))
442+
return new_instance
443+
444+
deepcopy = __deepcopy__
445+
446+
def __copy__(self):
447+
# taken from https://stackoverflow.com/a/15774013
448+
self._revalidate_path()
449+
cls = self.__class__
450+
new_instance = cls.__new__(cls)
451+
new_instance.__dict__.update(self.__dict__)
452+
return new_instance

0 commit comments

Comments
 (0)