Skip to content

Commit

Permalink
Merge pull request #3 from SylvainCorlay/cache_keys
Browse files Browse the repository at this point in the history
Avoid modifications in dictionary during iteration

closes #5
  • Loading branch information
minrk committed Apr 14, 2015
2 parents 80bf9a7 + 4091adc commit 86850a2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 18 additions & 0 deletions traitlets/tests/test_traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,24 @@ def assign_rollback():
self.assertRaises(TraitError, assign_rollback)


class CacheModification(HasTraits):
foo = Int()
bar = Int()

def _bar_validate(self, value, trait):
self.foo = value
return value

def _foo_validate(self, value, trait):
self.bar = value
return value


def test_cache_modification():
CacheModification(foo=1)
CacheModification(bar=1)


class OrderTraits(HasTraits):
notified = Dict()

Expand Down
8 changes: 4 additions & 4 deletions traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,15 +613,15 @@ def hold(*a):
self._notify_trait = hold
self._cross_validation_lock = True
yield
for name in cache:
for name in list(cache.keys()):
if hasattr(self, '_%s_validate' % name):
cross_validate = getattr(self, '_%s_validate' % name)
setattr(self, name, cross_validate(getattr(self, name), self))
except TraitError as e:
self._notify_trait = lambda *x: None
for name in cache:
if cache[name][1] is not Undefined:
setattr(self, name, cache[name][1])
for name, value in cache.items():
if value[1] is not Undefined:
setattr(self, name, value[1])
else:
self._trait_values.pop(name)
cache = {}
Expand Down

0 comments on commit 86850a2

Please sign in to comment.