Skip to content

Fix/deep copy recurse #29393

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/matplotlib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import copy
from functools import lru_cache
import sys
from weakref import WeakValueDictionary

import numpy as np
Expand Down Expand Up @@ -281,11 +282,25 @@
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)

Check warning on line 288 in lib/matplotlib/path.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/path.py#L286-L288

Added lines #L286 - L288 were not covered by tests
if memo is not None:
memo[id(self)] = p
_keep_alive(self, memo)

Check warning on line 291 in lib/matplotlib/path.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/path.py#L290-L291

Added lines #L290 - L291 were not covered by tests

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):
Expand Down
8 changes: 7 additions & 1 deletion lib/matplotlib/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import copy
import functools
import itertools
import sys
import textwrap
import weakref
import math
Expand Down Expand Up @@ -139,7 +140,12 @@
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)

Check warning on line 146 in lib/matplotlib/transforms.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/transforms.py#L144-L146

Added lines #L144 - L146 were not covered by tests
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 = {}
Expand Down
Loading