<s> import gc <EOL> import inspect <EOL> import os <EOL> import sys <EOL> import unittest <EOL> from pympler . garbagegraph import GarbageGraph , start_debug_garbage , end_debug_garbage <EOL> from pympler . refgraph import _Edge <EOL> class Foo : <EOL> def __init__ ( self ) : <EOL> self . foo = '<STR_LIT:foo>' <EOL> class Bar ( Foo ) : <EOL> def __init__ ( self ) : <EOL> Foo . __init__ ( self ) <EOL> self . bar = '<STR_LIT:bar>' <EOL> class FooNew ( object ) : <EOL> def __init__ ( self ) : <EOL> self . foo = '<STR_LIT:foo>' <EOL> class BarNew ( FooNew ) : <EOL> def __init__ ( self ) : <EOL> super ( BarNew , self ) . __init__ ( ) <EOL> class Enemy ( object ) : <EOL> def __del__ ( self ) : <EOL> pass <EOL> class GarbageTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> start_debug_garbage ( ) <EOL> gc . set_debug ( gc . DEBUG_SAVEALL ) <EOL> def tearDown ( self ) : <EOL> end_debug_garbage ( ) <EOL> del gc . garbage [ : ] <EOL> def test_empty ( self ) : <EOL> """<STR_LIT>""" <EOL> gb = GarbageGraph ( ) <EOL> self . assertEqual ( gb . count , <NUM_LIT:0> ) <EOL> self . assertEqual ( gb . render ( '<STR_LIT>' ) , False ) <EOL> def test_findgarbage ( self ) : <EOL> """<STR_LIT>""" <EOL> foo = Foo ( ) <EOL> bar = Bar ( ) <EOL> idfoo = id ( foo ) <EOL> idbar = id ( bar ) <EOL> foo . next = bar <EOL> bar . prev = foo <EOL> del foo <EOL> del bar <EOL> gb = GarbageGraph ( ) <EOL> self . assertEqual ( gb . count , len ( gc . garbage ) ) <EOL> self . assert_ ( gb . count >= <NUM_LIT:2> , gb . count ) <EOL> gfoo = [ x for x in gb . metadata if x . id == idfoo ] <EOL> self . assertEqual ( len ( gfoo ) , <NUM_LIT:1> ) <EOL> gfoo = gfoo [ <NUM_LIT:0> ] <EOL> self . assertEqual ( gfoo . type , '<STR_LIT>' ) <EOL> self . assert_ ( gfoo . size > <NUM_LIT:0> , gfoo . size ) <EOL> self . assertNotEqual ( gfoo . str , '<STR_LIT>' ) <EOL> gbar = [ x for x in gb . metadata if x . id == idbar ] <EOL> self . assertEqual ( len ( gbar ) , <NUM_LIT:1> ) <EOL> gbar = gbar [ <NUM_LIT:0> ] <EOL> self . assertEqual ( gbar . type , '<STR_LIT>' ) <EOL> self . assert_ ( gbar . size > <NUM_LIT:0> , gbar . size ) <EOL> self . assertNotEqual ( gbar . str , '<STR_LIT>' ) <EOL> def test_split ( self ) : <EOL> """<STR_LIT>""" <EOL> foo = Foo ( ) <EOL> bar = Bar ( ) <EOL> idfoo = id ( foo ) <EOL> idbar = id ( bar ) <EOL> idfd = id ( foo . __dict__ ) <EOL> idbd = id ( bar . __dict__ ) <EOL> foo . next = bar <EOL> bar . prev = foo <EOL> l = [ ] <EOL> l . append ( l ) <EOL> idl = id ( l ) <EOL> del foo <EOL> del bar <EOL> del l <EOL> gb = GarbageGraph ( ) <EOL> subs = gb . split_and_sort ( ) <EOL> self . assertEqual ( len ( subs ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( subs [ <NUM_LIT:0> ] . count , <NUM_LIT:4> ) <EOL> self . assertEqual ( subs [ <NUM_LIT:1> ] . count , <NUM_LIT:1> ) <EOL> fbg , lig = subs <EOL> self . assert_ ( isinstance ( fbg , GarbageGraph ) ) <EOL> self . assert_ ( isinstance ( lig , GarbageGraph ) ) <EOL> self . assertEqual ( len ( fbg . edges ) , <NUM_LIT:4> , fbg . edges ) <EOL> self . assertEqual ( len ( lig . edges ) , <NUM_LIT:1> , lig . edges ) <EOL> self . assert_ ( _Edge ( idl , idl , '<STR_LIT>' ) in lig . edges , lig . edges ) <EOL> self . assert_ ( _Edge ( idfoo , idfd , '<STR_LIT>' ) in fbg . edges , fbg . edges ) <EOL> self . assert_ ( _Edge ( idfd , idbar , '<STR_LIT>' ) in fbg . edges , fbg . edges ) <EOL> self . assert_ ( _Edge ( idbar , idbd , '<STR_LIT>' ) in fbg . edges , fbg . edges ) <EOL> self . assert_ ( _Edge ( idbd , idfoo , '<STR_LIT>' ) in fbg . edges , fbg . edges ) <EOL> def test_prune ( self ) : <EOL> """<STR_LIT>""" <EOL> foo = Foo ( ) <EOL> bar = Bar ( ) <EOL> foo . parent = foo <EOL> foo . leaf = bar <EOL> idb = id ( bar ) <EOL> del foo <EOL> del bar <EOL> gb1 = GarbageGraph ( ) <EOL> gb2 = GarbageGraph ( reduce = True ) <EOL> self . assertEqual ( gb1 . count , gb2 . count ) <EOL> self . assertTrue ( gb1 . count > gb2 . num_in_cycles ) <EOL> self . assertEqual ( gb2 . num_in_cycles , <NUM_LIT:2> ) <EOL> gbar = [ x for x in gb1 . metadata if x . id == idb ] <EOL> self . assertEqual ( len ( gbar ) , <NUM_LIT:1> ) <EOL> gbar = [ x for x in gb2 . metadata if x . id == idb ] <EOL> self . assertEqual ( len ( gbar ) , <NUM_LIT:0> ) <EOL> def test_edges_old ( self ) : <EOL> """<STR_LIT>""" <EOL> foo = Foo ( ) <EOL> bar = Bar ( ) <EOL> idfoo = id ( foo ) <EOL> idfd = id ( foo . __dict__ ) <EOL> idbar = id ( bar ) <EOL> idbd = id ( bar . __dict__ ) <EOL> foo . next = bar <EOL> bar . prev = foo <EOL> del foo <EOL> del bar <EOL> gb = GarbageGraph ( ) <EOL> self . assert_ ( _Edge ( idfoo , idfd , '<STR_LIT>' ) in gb . edges , gb . edges ) <EOL> self . assert_ ( _Edge ( idfd , idbar , '<STR_LIT>' ) in gb . edges , gb . edges ) <EOL> self . assert_ ( _Edge ( idbar , idbd , '<STR_LIT>' ) in gb . edges , gb . edges ) <EOL> self . assert_ ( _Edge ( idbd , idfoo , '<STR_LIT>' ) in gb . edges , gb . edges ) <EOL> def test_edges_new ( self ) : <EOL> """<STR_LIT>""" <EOL> foo = FooNew ( ) <EOL> bar = BarNew ( ) <EOL> idfoo = id ( foo ) <EOL> idfd = id ( foo . __dict__ ) <EOL> idbar = id ( bar ) <EOL> idbd = id ( bar . __dict__ ) <EOL> foo . next = bar <EOL> bar . prev = foo <EOL> del foo <EOL> del bar <EOL> gb = GarbageGraph ( ) <EOL> self . assert_ ( _Edge ( idfoo , idfd , '<STR_LIT>' ) in gb . edges , gb . edges ) <EOL> self . assert_ ( _Edge ( idfd , idbar , '<STR_LIT>' ) in gb . edges , gb . edges ) <EOL> self . assert_ ( _Edge ( idbar , idbd , '<STR_LIT>' ) in gb . edges , gb . edges ) <EOL> self . assert_ ( _Edge ( idbd , idfoo , '<STR_LIT>' ) in gb . edges , gb . edges ) <EOL> def test_uncollectable ( self ) : <EOL> """<STR_LIT>""" <EOL> foo = Foo ( ) <EOL> foo . parent = foo <EOL> enemy = Enemy ( ) <EOL> enemy . parent = enemy <EOL> idfoo = id ( foo ) <EOL> idenemy = id ( enemy ) <EOL> del foo <EOL> del enemy <EOL> gb = GarbageGraph ( collectable = <NUM_LIT:0> ) <EOL> gfoo = [ x for x in gb . metadata if x . id == idfoo ] <EOL> self . assertEqual ( len ( gfoo ) , <NUM_LIT:0> ) <EOL> genemy = [ x for x in gb . metadata if x . id == idenemy ] <EOL> if sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:4> ) : <EOL> self . assertEqual ( len ( genemy ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( gb . reduce_to_cycles ( ) , None ) <EOL> def test_write_graph ( self ) : <EOL> """<STR_LIT>""" <EOL> foo = Foo ( ) <EOL> foo . parent = foo <EOL> del foo <EOL> g = GarbageGraph ( ) <EOL> g . write_graph ( '<STR_LIT>' ) <EOL> os . unlink ( '<STR_LIT>' ) <EOL> def test_render ( self ) : <EOL> """<STR_LIT>""" <EOL> foo = Foo ( ) <EOL> foo . parent = foo <EOL> foo . constructor = foo . __init__ <EOL> def leak_frame ( ) : <EOL> frame = inspect . currentframe ( ) <EOL> leak_frame ( ) <EOL> del foo <EOL> g = GarbageGraph ( ) <EOL> try : <EOL> g . render ( '<STR_LIT>' ) <EOL> g . render ( '<STR_LIT>' , unflatten = True ) <EOL> except OSError : <EOL> pass <EOL> else : <EOL> os . unlink ( '<STR_LIT>' ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> suite = unittest . TestSuite ( ) <EOL> tclasses = [ GarbageTestCase ] <EOL> for tclass in tclasses : <EOL> names = unittest . getTestCaseNames ( tclass , '<STR_LIT>' ) <EOL> suite . addTests ( map ( tclass , names ) ) <EOL> if not unittest . TextTestRunner ( ) . run ( suite ) . wasSuccessful ( ) : <EOL> sys . exit ( <NUM_LIT:1> ) </s>
