This file is indexed.

/usr/share/pyshared/traits/trait_notifiers.py is in python-traits 4.1.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
#------------------------------------------------------------------------------
#
#  Copyright (c) 2005, Enthought, Inc.
#  All rights reserved.
#
#  This software is provided without warranty under the terms of the BSD
#  license included in enthought/LICENSE.txt and may be redistributed only
#  under the conditions described in the aforementioned license.  The license
#  is also available online at http://www.enthought.com/licenses/BSD.txt
#
#  Thanks for using Enthought open source!
#
#  Author:        David C. Morrill
#  Original Date: 06/21/2002
#
#  Refactored into a separate module: 07/04/2003
#
#------------------------------------------------------------------------------

""" Defines the classes needed to implement and support the Traits change
    notification mechanism.
"""

#-------------------------------------------------------------------------------
#  Imports:
#-------------------------------------------------------------------------------

from __future__ import absolute_import

import weakref
import traceback
import sys

from threading import local as thread_local

from threading \
    import Thread

from thread \
    import get_ident

from types \
    import MethodType

from .trait_base \
    import Uninitialized

from .trait_errors \
    import TraitNotificationError

#-------------------------------------------------------------------------------
#  Global Data:
#-------------------------------------------------------------------------------

# The thread ID for the user interface thread
ui_thread = -1

# The handler for notifications that must be run on the UI thread
ui_handler = None

#-------------------------------------------------------------------------------
#  Sets up the user interface thread handler:
#-------------------------------------------------------------------------------

def set_ui_handler ( handler ):
    """ Sets up the user interface thread handler.
    """
    global ui_handler, ui_thread

    ui_handler = handler
    ui_thread  = get_ident()

def ui_dispatch( handler, *args, **kw ):
    if get_ident() == ui_thread:
        handler( *args, **kw )
    else:
        ui_handler( handler, *args, **kw )

#-------------------------------------------------------------------------------
#  'NotificationExceptionHandlerState' class:
#-------------------------------------------------------------------------------

class NotificationExceptionHandlerState ( object ):

    def __init__ ( self, handler, reraise_exceptions, locked ):
        self.handler            = handler
        self.reraise_exceptions = reraise_exceptions
        self.locked             = locked

#-------------------------------------------------------------------------------
#  'NotificationExceptionHandler' class:
#-------------------------------------------------------------------------------

class NotificationExceptionHandler ( object ):

    def __init__ ( self ):
        self.traits_logger = None
        self.main_thread   = None
        self.thread_local  = thread_local()

#-- Private Methods ------------------------------------------------------------

    def _push_handler ( self, handler = None, reraise_exceptions = False,
                              main = False, locked = False ):
        """ Pushes a new traits notification exception handler onto the stack,
            making it the new exception handler. Returns a
            NotificationExceptionHandlerState object describing the previous
            exception handler.

            Parameters
            ----------
            handler : handler
                The new exception handler, which should be a callable or
                None. If None (the default), then the default traits
                notification exception handler is used. If *handler* is not
                None, then it must be a callable which can accept four
                arguments: object, trait_name, old_value, new_value.
            reraise_exceptions : Boolean
                Indicates whether exceptions should be reraised after the
                exception handler has executed. If True, exceptions will be
                re-raised after the specified handler has been executed.
                The default value is False.
            main : Boolean
                Indicates whether the caller represents the main application
                thread. If True, then the caller's exception handler is
                made the default handler for any other threads that are
                created. Note that a thread can explictly set its own exception
                handler if desired. The *main* flag is provided to make it
                easier to set a global application policy without having to
                explicitly set it for each thread. The default value is
                False.
            locked : Boolean
                Indicates whether further changes to the Traits notification
                exception handler state should be allowed. If True, then
                any subsequent calls to _push_handler() or _pop_handler() for
                that thread will raise a TraitNotificationError. The default
                value is False.
        """
        handlers = self._get_handlers()
        self._check_lock( handlers )
        if handler is None:
            handler = self._log_exception
        handlers.append( NotificationExceptionHandlerState( handler,
                                                  reraise_exceptions, locked ) )
        if main:
            self.main_thread = handlers

        return handlers[-2]

    def _pop_handler ( self ):
        """ Pops the traits notification exception handler stack, restoring
            the exception handler in effect prior to the most recent
            _push_handler() call. If the stack is empty or locked, a
            TraitNotificationError exception is raised.

            Note that each thread has its own independent stack. See the
            description of the _push_handler() method for more information on
            this.
        """
        handlers = self._get_handlers()
        self._check_lock( handlers )
        if len( handlers ) > 1:
            handlers.pop()
        else:
            raise TraitNotificationError(
                      'Attempted to pop an empty traits notification exception '
                      'handler stack.' )

    def _handle_exception ( self, object, trait_name, old, new  ):
        """ Handles a traits notification exception using the handler defined
            by the topmost stack entry for the corresponding thread.
        """
        excp_class, excp = sys.exc_info()[:2]
        handler_info     = self._get_handlers()[-1]
        handler_info.handler( object, trait_name, old, new )
        if (handler_info.reraise_exceptions or
            isinstance( excp, TraitNotificationError )):
            raise

    def _get_handlers ( self ):
        """ Returns the handler stack associated with the currently executing
            thread.
        """
        thread_local = self.thread_local
        if isinstance( thread_local, dict ):
            id       = get_ident()
            handlers = thread_local.get( id )
        else:
            handlers = getattr( thread_local, 'handlers', None )

        if handlers is None:
            if self.main_thread is not None:
                handler = self.main_thread[-1]
            else:
                handler = NotificationExceptionHandlerState(
                              self._log_exception, False, False )
            handlers = [ handler ]
            if isinstance( thread_local, dict ):
                thread_local[ id ] = handlers
            else:
                thread_local.handlers = handlers

        return handlers

    def _check_lock ( self, handlers ):
        """ Raises an exception if the specified handler stack is locked.
        """
        if handlers[-1].locked:
            raise TraitNotificationError(
                      'The traits notification exception handler is locked. '
                      'No changes are allowed.' )

    #---------------------------------------------------------------------------
    #  This method defines the default notification exception handling
    #  behavior of traits. However, it can be completely overridden by pushing
    #  a new handler using the '_push_handler' method.
    #
    #  It logs any exceptions generated in a trait notification handler.
    #---------------------------------------------------------------------------

    def _log_exception ( self, object, trait_name, old, new ):
        """ Logs any exceptions generated in a trait notification handler.
        """
        # When the stack depth is too great, the logger can't always log the
        # message. Make sure that it goes to the console at a minimum:
        excp_class, excp = sys.exc_info()[:2]
        if ((excp_class is RuntimeError) and
            (excp.args[0] == 'maximum recursion depth exceeded')):
            sys.__stderr__.write( 'Exception occurred in traits notification '
                'handler for object: %s, trait: %s, old value: %s, '
                'new value: %s.\n%s\n' % ( object, trait_name, old, new,
                ''.join( traceback.format_exception( *sys.exc_info() ) ) ) )

        logger = self.traits_logger
        if logger is None:
            import logging

            self.traits_logger = logger = logging.getLogger(
                                                  'traits' )
            handler = logging.StreamHandler()
            handler.setFormatter( logging.Formatter( '%(message)s' ) )
            logger.addHandler( handler )
            print ('Exception occurred in traits notification handler.\n'
                   'Please check the log file for details.')

        try:
            logger.exception(
                'Exception occurred in traits notification handler for '
                'object: %s, trait: %s, old value: %s, new value: %s' %
                ( object, trait_name, old, new ) )
        except Exception:
            # Ignore anything we can't log the above way:
            pass

#-------------------------------------------------------------------------------
#  Traits global notification exception handler:
#-------------------------------------------------------------------------------

notification_exception_handler = NotificationExceptionHandler()

push_exception_handler = notification_exception_handler._push_handler
pop_exception_handler  = notification_exception_handler._pop_handler
handle_exception       = notification_exception_handler._handle_exception

#-------------------------------------------------------------------------------
#  'StaticAnyTraitChangeNotifyWrapper' class:
#-------------------------------------------------------------------------------

class StaticAnyTraitChangeNotifyWrapper:

    def __init__ ( self, handler ):
        n = handler.func_code.co_argcount
        if n > 4:
            raise TraitNotificationError(
                ('Invalid number of arguments for the static anytrait change '
                 'notification handler: %s. A maximum of 4 arguments is '
                 'allowed, but %s were specified.') % ( handler.__name__, n ) )

        self.handler  = handler
        self.call_method = 'call_%d' % n

    def __call__(self, object, trait_name, old, new):
        """ Dispatch to the appropriate method.

        We do explicit dispatch instead of assigning to the .__call__ instance
        attribute to avoid reference cycles.
        """
        getattr(self, self.call_method)(object, trait_name, old, new)

    def equals ( self, handler ):
        return False

    def call_0 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            try:
                self.handler()
            except:
                handle_exception( object, trait_name, old, new )

    def call_1 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            try:
                self.handler( object )
            except:
                handle_exception( object, trait_name, old, new )

    def call_2 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            try:
                self.handler( object, trait_name )
            except:
                handle_exception( object, trait_name, old, new )

    def call_3 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            try:
                self.handler( object, trait_name, new )
            except:
                handle_exception( object, trait_name, old, new )

    def call_4 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            try:
                self.handler( object, trait_name, old, new )
            except:
                handle_exception( object, trait_name, old, new )

#-------------------------------------------------------------------------------
#  'StaticTraitChangeNotifyWrapper' class:
#-------------------------------------------------------------------------------

class StaticTraitChangeNotifyWrapper:

    def __init__ ( self, handler ):
        n = handler.func_code.co_argcount
        if n > 4:
            raise TraitNotificationError(
                ('Invalid number of arguments for the static trait change '
                 'notification handler: %s. A maximum of 4 arguments is '
                 'allowed, but %s were specified.') % ( handler.__name__, n ) )

        self.handler  = handler
        self.call_method = 'call_%d' % n

    def __call__(self, object, trait_name, old, new):
        """ Dispatch to the appropriate method.

        We do explicit dispatch instead of assigning to the .__call__ instance
        attribute to avoid reference cycles.
        """
        getattr(self, self.call_method)(object, trait_name, old, new)

    def equals ( self, handler ):
        return False

    def call_0 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            try:
                self.handler()
            except:
                handle_exception( object, trait_name, old, new )

    def call_1 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            try:
                self.handler( object )
            except:
                handle_exception( object, trait_name, old, new )

    def call_2 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            try:
                self.handler( object, new )
            except:
                handle_exception( object, trait_name, old, new )

    def call_3 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            try:
                self.handler( object, old, new )
            except:
                handle_exception( object, trait_name, old, new )

    def call_4 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            try:
                self.handler( object, trait_name, old, new )
            except:
                handle_exception( object, trait_name, old, new )

#-------------------------------------------------------------------------------
#  'TraitChangeNotifyWrapper' class:
#-------------------------------------------------------------------------------

class TraitChangeNotifyWrapper:

    def __init__ ( self, handler, owner, target=None ):
        self.init( handler, owner, target )

    def init ( self, handler, owner, target=None ):
        # If target is not None and handler is a function
        # then the handler will be removed when target
        # is deleted.
        func = handler
        if type( handler ) is MethodType:
            func   = handler.im_func
            object = handler.im_self
            if object is not None:
                self.object = weakref.ref( object, self.listener_deleted )
                self.name   = handler.__name__
                self.owner  = owner
                arg_count   = func.func_code.co_argcount - 1
                if arg_count > 4:
                    raise TraitNotificationError(
                        ('Invalid number of arguments for the dynamic trait '
                         'change notification handler: %s. A maximum of 4 '
                         'arguments is allowed, but %s were specified.') %
                        ( func.__name__, arg_count ) )

                self.call_method = 'rebind_call_%d' % arg_count

                return arg_count
        elif target is not None:
            # Set up so the handler will be removed when the target
            # is deleted
            self.object = weakref.ref( target, self.listener_deleted )
            self.owner = owner

        arg_count = handler.func_code.co_argcount
        if arg_count > 4:
            raise TraitNotificationError(
                ('Invalid number of arguments for the dynamic trait change '
                 'notification handler: %s. A maximum of 4 arguments is '
                 'allowed, but %s were specified.') %
                ( handler.__name__, arg_count ) )

        self.name     = None
        self.handler  = handler
        self.call_method = 'call_%d' % arg_count

        return arg_count

    def __call__(self, object, trait_name, old, new):
        """ Dispatch to the appropriate method.

        We do explicit dispatch instead of assigning to the .__call__ instance
        attribute to avoid reference cycles.
        """
        getattr(self, self.call_method)(object, trait_name, old, new)

    # NOTE: This method is normally the only one that needs to be overridden in
    # a subclass to implement the subclass's dispatch mechanism:
    def dispatch ( self, handler, *args ):
        handler( *args )

    def equals ( self, handler ):
        if handler is self:
            return True

        if (type( handler ) is MethodType) and (handler.im_self is not None):
            return ((handler.__name__ == self.name) and
                    (handler.im_self is self.object()))

        return ((self.name is None) and (handler == self.handler))

    def listener_deleted ( self, ref ):
        self.owner.remove( self )
        self.object = self.owner = None

    def dispose ( self ):
        self.object = None

    def call_0 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            try:
                self.dispatch( self.handler )
            except:
                handle_exception( object, trait_name, old, new )

    def call_1 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            try:
                self.dispatch( self.handler, new )
            except:
                handle_exception( object, trait_name, old, new )

    def call_2 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            try:
                self.dispatch( self.handler, trait_name, new )
            except:
                handle_exception( object, trait_name, old, new )

    def call_3 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            try:
                self.dispatch( self.handler, object, trait_name, new )
            except:
                handle_exception( object, trait_name, old, new )

    def call_4 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            try:
                self.dispatch( self.handler, object, trait_name, old, new )
            except:
                handle_exception( object, trait_name, old, new )

    def rebind_call_0 ( self, object, trait_name, old, new ):
        if (self.object is not None) and (old is not Uninitialized):
            try:
                self.dispatch( getattr( self.object(), self.name ) )
            except:
                handle_exception( object, trait_name, old, new )

    def rebind_call_1 ( self, object, trait_name, old, new ):
        if (self.object is not None) and (old is not Uninitialized):
            try:
                self.dispatch( getattr( self.object(), self.name ), new )
            except:
                handle_exception( object, trait_name, old, new )

    def rebind_call_2 ( self, object, trait_name, old, new ):
        if (self.object is not None) and (old is not Uninitialized):
            try:
                self.dispatch( getattr( self.object(), self.name ),
                               trait_name, new )
            except:
                handle_exception( object, trait_name, old, new )

    def rebind_call_3 ( self, object, trait_name, old, new ):
        if (self.object is not None) and (old is not Uninitialized):
            try:
                self.dispatch( getattr( self.object(), self.name ),
                               object, trait_name, new )
            except:
                handle_exception( object, trait_name, old, new )

    def rebind_call_4 ( self, object, trait_name, old, new ):
        if (self.object is not None) and (old is not Uninitialized):
            try:
                self.dispatch( getattr( self.object(), self.name ),
                               object, trait_name, old, new )
            except:
                handle_exception( object, trait_name, old, new )

#-------------------------------------------------------------------------------
#  'ExtendedTraitChangeNotifyWrapper' class:
#-------------------------------------------------------------------------------

class ExtendedTraitChangeNotifyWrapper ( TraitChangeNotifyWrapper ):

    def call_0 ( self, object, trait_name, old, new ):
        try:
            self.dispatch( self.handler )
        except:
            handle_exception( object, trait_name, old, new )

    def call_1 ( self, object, trait_name, old, new ):
        try:
            self.dispatch( self.handler, new )
        except:
            handle_exception( object, trait_name, old, new )

    def call_2 ( self, object, trait_name, old, new ):
        try:
            self.dispatch( self.handler, trait_name, new )
        except:
            handle_exception( object, trait_name, old, new )

    def call_3 ( self, object, trait_name, old, new ):
        try:
            self.dispatch( self.handler, object, trait_name, new )
        except:
            handle_exception( object, trait_name, old, new )

    def call_4 ( self, object, trait_name, old, new ):
        try:
            self.dispatch( self.handler, object, trait_name, old, new )
        except:
            handle_exception( object, trait_name, old, new )

    def rebind_call_0 ( self, object, trait_name, old, new ):
        if self.object is not None:
            try:
                self.dispatch( getattr( self.object(), self.name ) )
            except:
                handle_exception( object, trait_name, old, new )

    def rebind_call_1 ( self, object, trait_name, old, new ):
        if self.object is not None:
            try:
                self.dispatch( getattr( self.object(), self.name ), new )
            except:
                handle_exception( object, trait_name, old, new )

    def rebind_call_2 ( self, object, trait_name, old, new ):
        if self.object is not None:
            try:
                self.dispatch( getattr( self.object(), self.name ),
                               trait_name, new )
            except:
                handle_exception( object, trait_name, old, new )

    def rebind_call_3 ( self, object, trait_name, old, new ):
        if self.object is not None:
            try:
                self.dispatch( getattr( self.object(), self.name ),
                               object, trait_name, new )
            except:
                handle_exception( object, trait_name, old, new )

    def rebind_call_4 ( self, object, trait_name, old, new ):
        if self.object is not None:
            try:
                self.dispatch( getattr( self.object(), self.name ),
                               object, trait_name, old, new )
            except:
                handle_exception( object, trait_name, old, new )

#-------------------------------------------------------------------------------
#  'FastUITraitChangeNotifyWrapper' class:
#-------------------------------------------------------------------------------

class FastUITraitChangeNotifyWrapper ( TraitChangeNotifyWrapper ):

    def dispatch ( self, handler, *args ):
        if get_ident() == ui_thread:
            handler( *args )
        else:
            ui_handler( handler, *args )

#-------------------------------------------------------------------------------
#  'UITraitChangeNotifyWrapper' class:
#-------------------------------------------------------------------------------

class UITraitChangeNotifyWrapper ( TraitChangeNotifyWrapper ):

    def __init__ ( self, handler, owner, target ):
        self.init( handler, owner, target )
        self.deferred = None

    def call_0 ( self, object, trait_name, old, new ):
        if (old is not Uninitialized) and (self.deferred is None):
            self.deferred = DeferredTraitNotification( object, trait_name, old,
                                                       new )
            ui_handler( self.dispatch_0, self.deferred )

    def dispatch_0 ( self, deferred ):
        self.deferred = None
        try:
            self.handler()
        except:
            handle_exception( deferred.object, deferred.trait_name,
                              deferred.old, deferred.new )

    def call_1 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            if self.deferred is None:
                self.deferred = DeferredTraitNotification( object, trait_name,
                                                           old, new )
                ui_handler( self.dispatch_1, self.deferred )
            else:
                self.deferred.new = new

    def dispatch_1 ( self, deferred ):
        self.deferred = None
        try:
            self.handler( deferred.new )
        except:
            handle_exception( deferred.object, deferred.trait_name,
                              deferred.old, deferred.new )

    def call_2 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            if ((self.deferred is None) or
                (self.deferred.trait_name != trait_name)):
                self.deferred = DeferredTraitNotification( object, trait_name,
                                                           old, new )
                ui_handler( self.dispatch_2, self.deferred )
            else:
                self.deferred.new = new

    def dispatch_2 ( self, deferred ):
        self.deferred = None
        try:
            self.handler( deferred.trait_name, deferred.new )
        except:
            handle_exception( deferred.object, deferred.trait_name,
                              deferred.old, deferred.new )

    def call_3 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            deferred = self.deferred
            if ((deferred is None) or (deferred.trait_name != trait_name) or
                (deferred.object is not object)):
                self.deferred = DeferredTraitNotification( object, trait_name,
                                                           old, new )
                ui_handler( self.dispatch_3, self.deferred )
            else:
                self.deferred.new = new

    def dispatch_3 ( self, deferred ):
        self.deferred = None
        try:
            self.handler( deferred.object, deferred.trait_name, deferred.new )
        except:
            handle_exception( deferred.object, deferred.trait_name,
                              deferred.old, deferred.new )

    def call_4 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            deferred = self.deferred
            if ((deferred is None) or (deferred.trait_name != trait_name) or
                (deferred.object is not object)):
                self.deferred = DeferredTraitNotification( object, trait_name,
                                                           old, new )
                ui_handler( self.dispatch_4, self.deferred )
            else:
                self.deferred.new = new

    def dispatch_4 ( self, deferred ):
        self.deferred = None
        try:
            self.handler( deferred.object, deferred.trait_name, deferred.old,
                          deferred.new )
        except:
            handle_exception( deferred.object, deferred.trait_name,
                              deferred.old, deferred.new )

    def rebind_call_0 ( self, object, trait_name, old, new ):
        if (old is not Uninitialized) and (self.deferred is None):
            self.deferred = DeferredTraitNotification( object, trait_name, old,
                                                       new )
            ui_handler( self.rebind_dispatch_0, self.deferred )

    def rebind_dispatch_0 ( self, deferred ):
        self.deferred = None
        if self.object is not None:
            try:
                getattr( self.object(), self.name )()
            except:
                handle_exception( deferred.object, deferred.trait_name,
                                  deferred.old, deferred.new )

    def rebind_call_1 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            if self.deferred is None:
                self.deferred = DeferredTraitNotification( object, trait_name,
                                                           old, new )
                ui_handler( self.rebind_dispatch_1, self.deferred )
            else:
                self.deferred.new = new

    def rebind_dispatch_1 ( self, deferred ):
        self.deferred = None
        if self.object is not None:
            try:
                getattr( self.object(), self.name )( deferred.new )
            except:
                handle_exception( deferred.object, deferred.trait_name,
                                  deferred.old, deferred.new )

    def rebind_call_2 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            if ((self.deferred is None) or
                (self.deferred.trait_name != trait_name)):
                self.deferred = DeferredTraitNotification( object, trait_name,
                                                           old, new )
                ui_handler( self.rebind_dispatch_2, self.deferred )
            else:
                self.deferred.new = new

    def rebind_dispatch_2 ( self, deferred ):
        self.deferred = None
        if self.object is not None:
            try:
                getattr( self.object(), self.name )( deferred.trait_name,
                                                            deferred.new )
            except:
                handle_exception( deferred.object, deferred.trait_name,
                                  deferred.old, deferred.new )

    def rebind_call_3 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            deferred = self.deferred
            if ((deferred is None) or (deferred.trait_name != trait_name) or
                (deferred.object is not object)):
                self.deferred = DeferredTraitNotification( object, trait_name,
                                                           old, new )
                ui_handler( self.rebind_dispatch_3, self.deferred )
            else:
                self.deferred.new = new

    def rebind_dispatch_3 ( self, deferred ):
        self.deferred = None
        if self.object is not None:
            try:
                getattr( self.object(), self.name )( deferred.object,
                         deferred.trait_name, deferred.new )
            except:
                handle_exception( deferred.object, deferred.trait_name,
                                  deferred.old, deferred.new )

    def rebind_call_4 ( self, object, trait_name, old, new ):
        if old is not Uninitialized:
            deferred = self.deferred
            if ((deferred is None) or (deferred.trait_name != trait_name) or
                (deferred.object is not object)):
                self.deferred = DeferredTraitNotification( object, trait_name,
                                                           old, new )
                ui_handler( self.rebind_dispatch_4, self.deferred )
            else:
                self.deferred.new = new

    def rebind_dispatch_4 ( self, deferred ):
        self.deferred = None
        if self.object is not None:
            try:
                getattr( self.object(), self.name )( deferred.object,
                         deferred.trait_name, deferred.old, deferred.new )
            except:
                handle_exception( deferred.object, deferred.trait_name,
                                  deferred.old, deferred.new )

class DeferredTraitNotification ( object ):

    def __init__ ( self, object, trait_name, old, new ):
        self.object     = object
        self.trait_name = trait_name
        self.old        = old
        self.new        = new

#-------------------------------------------------------------------------------
#  'NewTraitChangeNotifyWrapper' class:
#-------------------------------------------------------------------------------

class NewTraitChangeNotifyWrapper ( TraitChangeNotifyWrapper ):

    def dispatch ( self, handler, *args ):
        Thread( target = handler, args = args ).start()