<s> """<STR_LIT>""" <EOL> import weakref <EOL> from envisage . safeweakref import ref <EOL> from traits . api import HasTraits <EOL> from traits . testing . unittest_tools import unittest <EOL> class SafeWeakrefTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> def test_can_create_weakref_to_bound_method ( self ) : <EOL> class Foo ( HasTraits ) : <EOL> def method ( self ) : <EOL> self . method_called = True <EOL> f = Foo ( ) <EOL> r = ref ( f . method ) <EOL> self . assertNotEqual ( None , r ( ) ) <EOL> r ( ) ( ) <EOL> self . assert_ ( f . method_called ) <EOL> del f <EOL> self . assertEqual ( None , r ( ) ) <EOL> return <EOL> def test_two_weakrefs_to_bound_method_are_identical ( self ) : <EOL> class Foo ( HasTraits ) : <EOL> def method ( self ) : <EOL> pass <EOL> f = Foo ( ) <EOL> self . assertIs ( ref ( f . method ) , ref ( f . method ) ) <EOL> return <EOL> def test_internal_cache_is_weak_too ( self ) : <EOL> cache = ref . _cache <EOL> class Foo ( HasTraits ) : <EOL> def method ( self ) : <EOL> pass <EOL> f = Foo ( ) <EOL> len_cache = len ( cache ) <EOL> r = ref ( f . method ) <EOL> self . assertEqual ( len_cache + <NUM_LIT:1> , len ( cache ) ) <EOL> del f <EOL> self . assertEqual ( None , r ( ) ) <EOL> self . assertEqual ( len_cache , len ( cache ) ) <EOL> return <EOL> def test_two_weakrefs_to_bound_method_are_equal ( self ) : <EOL> class Foo ( HasTraits ) : <EOL> def method ( self ) : <EOL> pass <EOL> f = Foo ( ) <EOL> r1 = ref ( f . method ) <EOL> r2 = ref ( f . method ) <EOL> self . assertEqual ( r1 , r2 ) <EOL> self . assert_ ( not r1 == <NUM_LIT> ) <EOL> return <EOL> def test_two_weakrefs_to_bound_method_hash_equally ( self ) : <EOL> class Foo ( HasTraits ) : <EOL> def method ( self ) : <EOL> pass <EOL> f = Foo ( ) <EOL> r1 = ref ( f . method ) <EOL> r2 = ref ( f . method ) <EOL> self . assertEqual ( hash ( r1 ) , hash ( r2 ) ) <EOL> r1 = ref ( Foo ) <EOL> r2 = ref ( Foo ) <EOL> self . assertEqual ( hash ( r1 ) , hash ( r2 ) ) <EOL> return <EOL> def test_get_builtin_weakref_for_non_bound_method ( self ) : <EOL> class Foo ( HasTraits ) : <EOL> pass <EOL> f = Foo ( ) <EOL> r = ref ( f ) <EOL> self . assertEqual ( weakref . ref , type ( r ) ) <EOL> return <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
