diff --git a/lib/matplotlib/path.py b/lib/matplotlib/path.py index a021706fb1e5..e91b7ad19513 100644 --- a/lib/matplotlib/path.py +++ b/lib/matplotlib/path.py @@ -11,6 +11,7 @@ import copy from functools import lru_cache +import sys from weakref import WeakValueDictionary import numpy as np @@ -281,11 +282,25 @@ def __deepcopy__(self, memo=None): 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) + 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 deep copy of the `Path`, with copies of 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 7228f05bcf9e..8288a5b14baf 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -38,6 +38,7 @@ import copy import functools import itertools +import sys import textwrap import weakref import math @@ -139,7 +140,12 @@ def __setstate__(self, data_dict): 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 = {}