From 811c9a6b1fcd03a09a2256469fc48afe5598dac9 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 5 Oct 2021 10:19:36 +0200 Subject: [PATCH] Make `Path.__deepcopy__` interact better with subclasses, e.g. TextPath. --- lib/matplotlib/path.py | 11 ++++------- lib/matplotlib/tests/test_textpath.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 lib/matplotlib/tests/test_textpath.py diff --git a/lib/matplotlib/path.py b/lib/matplotlib/path.py index 4280d55eeacd..43d3f2fd95da 100644 --- a/lib/matplotlib/path.py +++ b/lib/matplotlib/path.py @@ -272,13 +272,10 @@ def __deepcopy__(self, memo=None): Return a deepcopy of the `Path`. The `Path` will not be readonly, even if the source `Path` is. """ - try: - codes = self.codes.copy() - except AttributeError: - codes = None - return self.__class__( - self.vertices.copy(), codes, - _interpolation_steps=self._interpolation_steps) + # Deepcopying arrays (vertices, codes) strips the writeable=False flag. + p = copy.deepcopy(super(), memo) + p._readonly = False + return p deepcopy = __deepcopy__ diff --git a/lib/matplotlib/tests/test_textpath.py b/lib/matplotlib/tests/test_textpath.py new file mode 100644 index 000000000000..e421d2623cad --- /dev/null +++ b/lib/matplotlib/tests/test_textpath.py @@ -0,0 +1,10 @@ +import copy + +from matplotlib.textpath import TextPath + + +def test_copy(): + tp = TextPath((0, 0), ".") + assert copy.deepcopy(tp).vertices is not tp.vertices + assert (copy.deepcopy(tp).vertices == tp.vertices).all() + assert copy.copy(tp).vertices is tp.vertices