<s> from nose . plugins import Plugin <EOL> import gc <EOL> from greenlet import greenlet <EOL> import traceback <EOL> class GreenletLeak ( Plugin ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> Plugin . __init__ ( self ) <EOL> def options ( self , parser , env ) : <EOL> super ( GreenletLeak , self ) . options ( parser , env = env ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> def configure ( self , options , conf ) : <EOL> super ( GreenletLeak , self ) . configure ( options , conf ) <EOL> self . _greenlet_kill = options . greenlet_kill <EOL> def begin ( self ) : <EOL> self . _gls_by_test = { } <EOL> @ classmethod <EOL> def get_greenlets ( cls ) : <EOL> """<STR_LIT>""" <EOL> return { obj for obj in gc . get_objects ( ) if isinstance ( obj , greenlet ) and not obj . dead } <EOL> def beforeTest ( self , test ) : <EOL> self . _pre_gls = self . get_greenlets ( ) <EOL> def afterTest ( self , test ) : <EOL> post_gls = self . get_greenlets ( ) <EOL> gls_added = post_gls . difference ( self . _pre_gls ) <EOL> gls_deleted = self . _pre_gls . difference ( post_gls ) <EOL> if len ( gls_added ) > <NUM_LIT:0> : <EOL> self . _gls_by_test [ test . id ( ) ] = gls_added <EOL> @ classmethod <EOL> def format_greenlet ( cls , gl ) : <EOL> """<STR_LIT>""" <EOL> if gl . dead : <EOL> return ( [ "<STR_LIT>" ] , [ ] ) <EOL> else : <EOL> selfstr = "<STR_LIT>" <EOL> funcstr = "<STR_LIT>" <EOL> if hasattr ( gl , "<STR_LIT>" ) : <EOL> boundstr = "<STR_LIT>" <EOL> if hasattr ( gl . _run , '<STR_LIT>' ) : <EOL> selfstr = str ( gl . _run . im_self ) <EOL> boundstr = "<STR_LIT>" <EOL> try : <EOL> if hasattr ( gl . _run , '<STR_LIT>' ) : <EOL> funcstr = "<STR_LIT>" % ( boundstr , gl . _run . im_class . __name__ , gl . _run . im_func . __name__ ) <EOL> elif hasattr ( gl . _run , '<STR_LIT>' ) : <EOL> funcstr = "<STR_LIT>" % ( boundstr , func_name ) <EOL> except Exception as ex : <EOL> pass <EOL> selfstr = "<STR_LIT>" + selfstr <EOL> funcstr = "<STR_LIT>" + funcstr <EOL> gltb = traceback . format_stack ( gl . gr_frame ) <EOL> return ( [ selfstr , funcstr ] , gltb ) <EOL> def report ( self , stream ) : <EOL> table = [ ] <EOL> for tid , gls in self . _gls_by_test . iteritems ( ) : <EOL> gls = list ( gls ) <EOL> ptid = "<STR_LIT:.>" . join ( tid . split ( "<STR_LIT:.>" ) [ - <NUM_LIT:2> : ] ) <EOL> table . append ( [ ptid , str ( gls [ <NUM_LIT:0> ] ) ] ) <EOL> def append_gl_status ( gl ) : <EOL> status = self . format_greenlet ( gl ) <EOL> table . append ( [ "<STR_LIT>" , "<STR_LIT:\n>" . join ( status [ <NUM_LIT:0> ] ) ] ) <EOL> for line in status [ <NUM_LIT:1> ] : <EOL> for subline in line . split ( "<STR_LIT:\n>" ) [ <NUM_LIT:0> : <NUM_LIT:2> ] : <EOL> table . append ( [ "<STR_LIT>" , "<STR_LIT:U+0020>" + subline ] ) <EOL> append_gl_status ( gls [ <NUM_LIT:0> ] ) <EOL> table . append ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> for gl in gls [ <NUM_LIT:1> : ] : <EOL> table . append ( [ "<STR_LIT>" , str ( gl ) ] ) <EOL> append_gl_status ( gl ) <EOL> table . append ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> table . insert ( <NUM_LIT:0> , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> widths = [ max ( [ len ( row [ x ] ) for row in table ] ) for x in xrange ( len ( table [ <NUM_LIT:0> ] ) ) ] <EOL> fmt_out = [ "<STR_LIT:U+0020>" . join ( [ x . ljust ( widths [ i ] ) for i , x in enumerate ( row ) ] ) for row in table ] <EOL> fmt_out . insert ( <NUM_LIT:1> , "<STR_LIT:U+0020>" . join ( [ '<STR_LIT>' . ljust ( widths [ i ] , '<STR_LIT:=>' ) for i in xrange ( len ( widths ) ) ] ) ) <EOL> stream . write ( "<STR_LIT>" ) <EOL> stream . write ( "<STR_LIT:\n>" . join ( fmt_out ) ) <EOL> stream . write ( "<STR_LIT:\n>" ) </s>
