Skip to content

Commit 2745016

Browse files
committed
Rebased, and fix some more copying issues
1 parent 7eac6f2 commit 2745016

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

cycler.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,18 @@ def __init__(self, left, right=None, op=None):
117117
if isinstance(left, Cycler):
118118
self._left = Cycler(left._left, left._right, left._op)
119119
elif left is not None:
120-
self._left = list(left)
120+
# Need to copy the dictionary or else that will be a residual
121+
# mutable that could lead to strange errors
122+
self._left = [copy.copy(v) for v in left]
121123
else:
122124
self._left = None
123125

124126
if isinstance(right, Cycler):
125127
self._right = Cycler(right._left, right._right, right._op)
126128
elif right is not None:
127-
self._right = list(right)
129+
# Need to copy the dictionary or else that will be a residual
130+
# mutable that could lead to strange errors
131+
self._right = [copy.copy(v) for v in right]
128132
else:
129133
self._right = None
130134

@@ -171,9 +175,7 @@ def change_key(self, old, new):
171175
# It should be completely safe at this point to
172176
# assume that the old key can be found in each
173177
# iteration.
174-
for entry in self._left:
175-
entry[new] = entry[old]
176-
del entry[old]
178+
self._left = [{new: entry[old]} for entry in self._left]
177179

178180
def _compose(self):
179181
"""

test_cycler.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,22 @@ def test_keychange():
226226

227227
c = c1 + c2
228228
c.change_key('lw', 'linewidth')
229+
# Changing a key in one cycler should have no
230+
# impact in the original cycler.
231+
assert_equal(c2, cycler('lw', [1, 2, 3]))
229232
assert_equal(c, c1 + cycler('linewidth', c2))
230233

231234
c = (c1 + c2) * c3
232235
c.change_key('c', 'color')
236+
assert_equal(c1, cycler('c', 'rgb'))
233237
assert_equal(c, (cycler('color', c1) + c2) * c3)
234238

235239
# Perfectly fine, it is a no-op
236240
c.change_key('color', 'color')
237241
assert_equal(c, (cycler('color', c1) + c2) * c3)
238242

239243
# Can't change a key to one that is already in there
240-
assert_raises(ValueError, Cycler.change_key, c, 'color', 'linewidth')
244+
assert_raises(ValueError, Cycler.change_key, c, 'color', 'lw')
241245
# Can't change a key you don't have
242246
assert_raises(KeyError, Cycler.change_key, c, 'c', 'foobar')
243247

0 commit comments

Comments
 (0)