From caf65e3e55bbf7d60712beb9053ee51a134608b0 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 31 Dec 2024 15:08:31 -0500 Subject: [PATCH] MNT: use copy.deepcopy to implement deepcopy method We provide deepcopy and copy methods as a shortcut for users to avoid having to import the copy module and call the required function. Directly calling `__deepcopy__` side-steps all of the memoization machinery and may lead to incorrect results. --- lib/matplotlib/path.py | 20 ++++++++++++++++++-- lib/matplotlib/transforms.py | 8 +++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/path.py b/lib/matplotlib/path.py index 5f5a0f3..eac8cbd 100644 --- a/lib/matplotlib/path.py +++ b/lib/matplotlib/path.py @@ -11,6 +11,7 @@ visualisation. import copy from functools import lru_cache +import sys from weakref import WeakValueDictionary import numpy as np @@ -282,11 +283,26 @@ class Path: readonly, even if the source `Path` is. """ # Deepcopying arrays (vertices, codes) strips the writeable=False flag. - p = copy.deepcopy(super(), memo) + if sys.version_info >= (3, 14): + from copy import _reconstruct, _keep_alive + rv = super().__reduce_ex__(4) + assert memo is not None + p = _reconstruct(self, memo, *rv) + if memo is not None: + memo[id(self)] = p + _keep_alive(self, memo) + + else: + p = copy.deepcopy(super(), memo) p._readonly = False return p - deepcopy = __deepcopy__ + def deepcopy(self, memo=None): + """ + Return a shallow copy of the `Path`, which will share the + vertices and codes with the source `Path`. + """ + return copy.deepcopy(self, memo=memo) @classmethod def make_compound_path_from_polys(cls, XY): diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index 2934b0a..74d3c68 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -38,6 +38,7 @@ of how to use transforms. import copy import functools import itertools +import sys import textwrap import weakref import math @@ -141,7 +142,12 @@ class TransformNode: for k, v in self._parents.items() if v is not None} def __copy__(self): - other = copy.copy(super()) + if sys.version_info >= (3, 14): + from copy import _reconstruct + rv = super().__reduce_ex__(4) + other = _reconstruct(self, None, *rv) + else: + other = copy.copy(super()) # If `c = a + b; a1 = copy(a)`, then modifications to `a1` do not # propagate back to `c`, i.e. we need to clear the parents of `a1`. other._parents = {} -- 2.48.1