Skip to content

Commit cfe45cd

Browse files
committed
Fix Path deepcopy signature
**Problem** Path.__deepcopy__ does not accept a second(`memo`) parameter, so the common usage of the `deepcopy` function fails. **Solution** Add second parameter. While at it, also take care of the case of copying the `Path` without codes.
1 parent 0cfc7a8 commit cfe45cd

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/matplotlib/path.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,17 @@ def __copy__(self):
297297

298298
copy = __copy__
299299

300-
def __deepcopy__(self):
300+
def __deepcopy__(self, memo=None):
301301
"""
302302
Returns a deepcopy of the `Path`. The `Path` will not be
303303
readonly, even if the source `Path` is.
304304
"""
305+
try:
306+
codes = self.codes.copy()
307+
except AttributeError:
308+
codes = None
305309
return self.__class__(
306-
self.vertices.copy(), self.codes.copy(),
310+
self.vertices.copy(), codes,
307311
_interpolation_steps=self._interpolation_steps)
308312

309313
deepcopy = __deepcopy__

lib/matplotlib/tests/test_path.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import (absolute_import, division, print_function,
22
unicode_literals)
3+
import copy
34

45
import six
56

@@ -172,6 +173,16 @@ def test_path_to_polygons():
172173
assert_array_equal(p.to_polygons(closed_only=False), [data])
173174

174175

176+
def test_path_deepcopy():
177+
# Should not raise any error
178+
verts = [[0, 0], [1, 1]]
179+
codes = [Path.MOVETO, Path.LINETO]
180+
path1 = Path(verts)
181+
path2 = Path(verts, codes)
182+
copy.deepcopy(path1)
183+
copy.deepcopy(path2)
184+
185+
175186
if __name__ == '__main__':
176187
import nose
177188
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)