<s> """<STR_LIT>""" <EOL> import gc <EOL> import sys <EOL> from . . has_traits import HasTraits , Property , on_trait_change <EOL> from . . trait_types import Bool , DelegatesTo , Instance , Int , List <EOL> from . . testing . unittest_tools import unittest <EOL> class Dummy ( HasTraits ) : <EOL> x = Int ( <NUM_LIT:10> ) <EOL> def _create_subclass ( ) : <EOL> class Subclass ( HasTraits ) : <EOL> pass <EOL> return Subclass <EOL> class Dummy2 ( HasTraits ) : <EOL> y = Int ( <NUM_LIT:20> ) <EOL> dummy = Instance ( Dummy ) <EOL> class DelegateMess ( HasTraits ) : <EOL> dummy1 = Instance ( Dummy , args = ( ) ) <EOL> dummy2 = Instance ( Dummy2 ) <EOL> y = DelegatesTo ( '<STR_LIT>' ) <EOL> handler_called = Bool ( False ) <EOL> def _dummy2_default ( self ) : <EOL> return Dummy2 ( dummy = self . dummy1 ) <EOL> @ on_trait_change ( '<STR_LIT>' ) <EOL> def _on_dummy1_x ( self ) : <EOL> self . handler_called = True <EOL> def _init_trait_listeners ( self ) : <EOL> """<STR_LIT>""" <EOL> for name in [ '<STR_LIT:y>' , '<STR_LIT>' ] : <EOL> data = self . __class__ . __listener_traits__ [ name ] <EOL> getattr ( self , '<STR_LIT>' % data [ <NUM_LIT:0> ] ) ( name , * data ) <EOL> class DelegateLeak ( HasTraits ) : <EOL> visible = Property ( Bool , depends_on = '<STR_LIT>' ) <EOL> can_enable = DelegatesTo ( '<STR_LIT>' , prefix = '<STR_LIT:x>' ) <EOL> flag = Instance ( Dummy , kw = { '<STR_LIT:x>' : <NUM_LIT> } ) <EOL> class Presenter ( HasTraits ) : <EOL> obj = Instance ( Dummy ) <EOL> y = Property ( Int ( ) , depends_on = '<STR_LIT>' ) <EOL> def _get_y ( self ) : <EOL> return self . obj . x <EOL> class ListUpdatesTest ( HasTraits ) : <EOL> a = List <EOL> b = List <EOL> events_received = Int ( <NUM_LIT:0> ) <EOL> @ on_trait_change ( '<STR_LIT>' ) <EOL> def _receive_events ( self ) : <EOL> self . events_received += <NUM_LIT:1> <EOL> class TestRegression ( unittest . TestCase ) : <EOL> def test_default_value_for_no_cache ( self ) : <EOL> """<STR_LIT>""" <EOL> dummy = Dummy ( ) <EOL> self . assertEqual ( dummy . __dict__ , { } ) <EOL> ctrait = dummy . trait ( '<STR_LIT:x>' ) <EOL> default = ctrait . default_value_for ( dummy , '<STR_LIT:x>' ) <EOL> self . assertEqual ( default , <NUM_LIT:10> ) <EOL> self . assertEqual ( dummy . __dict__ , { } ) <EOL> def test_subclasses_weakref ( self ) : <EOL> """<STR_LIT>""" <EOL> previous_subclasses = HasTraits . __subclasses__ ( ) <EOL> _create_subclass ( ) <EOL> _create_subclass ( ) <EOL> _create_subclass ( ) <EOL> _create_subclass ( ) <EOL> gc . collect ( ) <EOL> self . assertEqual ( previous_subclasses , HasTraits . __subclasses__ ( ) ) <EOL> def test_leaked_property_tuple ( self ) : <EOL> """<STR_LIT>""" <EOL> class A ( HasTraits ) : <EOL> prop = Property ( ) <EOL> a = A ( ) <EOL> self . assertEqual ( sys . getrefcount ( a . trait ( '<STR_LIT>' ) . property ( ) ) , <NUM_LIT:1> ) <EOL> def test_delegate_initializer ( self ) : <EOL> mess = DelegateMess ( ) <EOL> self . assertFalse ( mess . handler_called ) <EOL> mess . dummy1 . x = <NUM_LIT:20> <EOL> self . assertTrue ( mess . handler_called ) <EOL> def test_no_leaking_notifiers ( self ) : <EOL> """<STR_LIT>""" <EOL> dummy = Dummy ( ) <EOL> ctrait = dummy . _trait ( '<STR_LIT:x>' , <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( ctrait . _notifiers ( <NUM_LIT:1> ) ) , <NUM_LIT:0> ) <EOL> presenter = Presenter ( obj = dummy ) <EOL> self . assertEqual ( len ( ctrait . _notifiers ( <NUM_LIT:1> ) ) , <NUM_LIT:1> ) <EOL> del presenter <EOL> self . assertEqual ( len ( ctrait . _notifiers ( <NUM_LIT:1> ) ) , <NUM_LIT:0> ) <EOL> def test_init_list_depends ( self ) : <EOL> """<STR_LIT>""" <EOL> list_test = ListUpdatesTest ( ) <EOL> list_test . a . append ( <NUM_LIT:0> ) <EOL> list_test . b = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> list_test . b [ <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> self . assertEqual ( list_test . events_received , <NUM_LIT:3> ) <EOL> def test_has_traits_notifiers_refleak ( self ) : <EOL> warmup = <NUM_LIT:5> <EOL> cycles = <NUM_LIT:10> <EOL> counts = [ ] <EOL> def handler ( ) : <EOL> pass <EOL> def f ( ) : <EOL> obj = HasTraits ( ) <EOL> obj . on_trait_change ( handler ) <EOL> for _ in xrange ( cycles ) : <EOL> f ( ) <EOL> gc . collect ( ) <EOL> counts . append ( len ( gc . get_objects ( ) ) ) <EOL> self . assertEqual ( counts [ warmup : - <NUM_LIT:1> ] , counts [ warmup + <NUM_LIT:1> : ] ) <EOL> def test_delegation_refleak ( self ) : <EOL> warmup = <NUM_LIT:5> <EOL> cycles = <NUM_LIT:10> <EOL> counts = [ ] <EOL> for _ in xrange ( cycles ) : <EOL> DelegateLeak ( ) <EOL> gc . collect ( ) <EOL> counts . append ( len ( gc . get_objects ( ) ) ) <EOL> self . assertEqual ( counts [ warmup : - <NUM_LIT:1> ] , counts [ warmup + <NUM_LIT:1> : ] ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
