Show More
@@ -1366,7 +1366,7 b' def test_hold_trait_notifications():' | |||
|
1366 | 1366 | nt.assert_equal(t.a, 4) |
|
1367 | 1367 | nt.assert_equal(changes, []) |
|
1368 | 1368 | |
|
1369 |
nt.assert_equal(changes, [( |
|
|
1369 | nt.assert_equal(changes, [(0, 4)]) | |
|
1370 | 1370 | # Test roll-back |
|
1371 | 1371 | try: |
|
1372 | 1372 | with t.hold_trait_notifications(): |
@@ -597,50 +597,49 b' class HasTraits(py3compat.with_metaclass(MetaHasTraits, object)):' | |||
|
597 | 597 | yield |
|
598 | 598 | return |
|
599 | 599 | else: |
|
600 | self._cross_validation_lock = True | |
|
601 | 600 | cache = {} |
|
602 | notifications = {} | |
|
603 | 601 | _notify_trait = self._notify_trait |
|
604 | 602 | |
|
605 | def cache_values(*a): | |
|
606 | cache[a[0]] = a | |
|
607 | ||
|
608 | def hold_notifications(*a): | |
|
609 | notifications[a[0]] = a | |
|
603 | def merge(previous, current): | |
|
604 | """merges notifications of the form (name, old, value)""" | |
|
605 | if previous is None: | |
|
606 | return current | |
|
607 | else: | |
|
608 | return (current[0], previous[1], current[2]) | |
|
610 | 609 | |
|
611 | self._notify_trait = cache_values | |
|
610 | def hold(*a): | |
|
611 | cache[a[0]] = merge(cache.get(a[0]), a) | |
|
612 | 612 | |
|
613 | 613 | try: |
|
614 | self._notify_trait = hold | |
|
615 | self._cross_validation_lock = True | |
|
614 | 616 | yield |
|
617 | for name in cache: | |
|
618 | if hasattr(self, '_%s_validate' % name): | |
|
619 | cross_validate = getattr(self, '_%s_validate' % name) | |
|
620 | setattr(self, name, cross_validate(getattr(self, name), self)) | |
|
621 | except TraitError as e: | |
|
622 | self._notify_trait = lambda *x: None | |
|
623 | for name in cache: | |
|
624 | if cache[name][1] is not Undefined: | |
|
625 | setattr(self, name, cache[name][1]) | |
|
626 | else: | |
|
627 | delattr(self, name) | |
|
628 | cache = {} | |
|
629 | raise e | |
|
615 | 630 | finally: |
|
616 | try: | |
|
617 | self._notify_trait = hold_notifications | |
|
618 | for name in cache: | |
|
619 | if hasattr(self, '_%s_validate' % name): | |
|
620 | cross_validate = getattr(self, '_%s_validate' % name) | |
|
621 | setattr(self, name, cross_validate(getattr(self, name), self)) | |
|
622 | except TraitError as e: | |
|
623 |
self._notify_trait |
|
|
624 | for name in cache: | |
|
625 | if cache[name][1] is not Undefined: | |
|
626 | setattr(self, name, cache[name][1]) | |
|
627 |
|
|
|
628 | delattr(self, name) | |
|
629 | cache = {} | |
|
630 | notifications = {} | |
|
631 | raise e | |
|
632 | finally: | |
|
633 | self._notify_trait = _notify_trait | |
|
634 | self._cross_validation_lock = False | |
|
635 | if isinstance(_notify_trait, types.MethodType): | |
|
636 | # FIXME: remove when support is bumped to 3.4. | |
|
637 | # when original method is restored, | |
|
638 | # remove the redundant value from __dict__ | |
|
639 | # (only used to preserve pickleability on Python < 3.4) | |
|
640 | self.__dict__.pop('_notify_trait', None) | |
|
641 | # trigger delayed notifications | |
|
642 | for v in dict(cache, **notifications).values(): | |
|
643 | self._notify_trait(*v) | |
|
631 | self._notify_trait = _notify_trait | |
|
632 | self._cross_validation_lock = False | |
|
633 | if isinstance(_notify_trait, types.MethodType): | |
|
634 | # FIXME: remove when support is bumped to 3.4. | |
|
635 | # when original method is restored, | |
|
636 | # remove the redundant value from __dict__ | |
|
637 | # (only used to preserve pickleability on Python < 3.4) | |
|
638 | self.__dict__.pop('_notify_trait', None) | |
|
639 | ||
|
640 | # trigger delayed notifications | |
|
641 | for v in cache.values(): | |
|
642 | self._notify_trait(*v) | |
|
644 | 643 | |
|
645 | 644 | def _notify_trait(self, name, old_value, new_value): |
|
646 | 645 |
General Comments 0
You need to be logged in to leave comments.
Login now