<s> """<STR_LIT>""" <EOL> import operator <EOL> import sys <EOL> import traceback <EOL> import weakref <EOL> try : <EOL> callable <EOL> except NameError : <EOL> def callable ( object ) : <EOL> return hasattr ( object , '<STR_LIT>' ) <EOL> if sys . version_info < ( <NUM_LIT:3> , ) : <EOL> get_self = operator . attrgetter ( '<STR_LIT>' ) <EOL> get_func = operator . attrgetter ( '<STR_LIT>' ) <EOL> else : <EOL> get_self = operator . attrgetter ( '<STR_LIT>' ) <EOL> get_func = operator . attrgetter ( '<STR_LIT>' ) <EOL> def safe_ref ( target , on_delete = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> im_self = get_self ( target ) <EOL> except AttributeError : <EOL> if callable ( on_delete ) : <EOL> return weakref . ref ( target , on_delete ) <EOL> else : <EOL> return weakref . ref ( target ) <EOL> else : <EOL> if im_self is not None : <EOL> assert hasattr ( target , '<STR_LIT>' ) or hasattr ( target , '<STR_LIT>' ) , ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % target ) <EOL> reference = BoundMethodWeakref ( target = target , on_delete = on_delete ) <EOL> return reference <EOL> class BoundMethodWeakref ( object ) : <EOL> """<STR_LIT>""" <EOL> _all_instances = weakref . WeakValueDictionary ( ) <EOL> def __new__ ( cls , target , on_delete = None , * arguments , ** named ) : <EOL> """<STR_LIT>""" <EOL> key = cls . calculate_key ( target ) <EOL> current = cls . _all_instances . get ( key ) <EOL> if current is not None : <EOL> current . deletion_methods . append ( on_delete ) <EOL> return current <EOL> else : <EOL> base = super ( BoundMethodWeakref , cls ) . __new__ ( cls ) <EOL> cls . _all_instances [ key ] = base <EOL> base . __init__ ( target , on_delete , * arguments , ** named ) <EOL> return base <EOL> def __init__ ( self , target , on_delete = None ) : <EOL> """<STR_LIT>""" <EOL> def remove ( weak , self = self ) : <EOL> """<STR_LIT>""" <EOL> methods = self . deletion_methods [ : ] <EOL> del self . deletion_methods [ : ] <EOL> try : <EOL> del self . __class__ . _all_instances [ self . key ] <EOL> except KeyError : <EOL> pass <EOL> for function in methods : <EOL> try : <EOL> if callable ( function ) : <EOL> function ( self ) <EOL> except Exception : <EOL> try : <EOL> traceback . print_exc ( ) <EOL> except AttributeError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( self , function , e ) ) <EOL> self . deletion_methods = [ on_delete ] <EOL> self . key = self . calculate_key ( target ) <EOL> im_self = get_self ( target ) <EOL> im_func = get_func ( target ) <EOL> self . weak_self = weakref . ref ( im_self , remove ) <EOL> self . weak_func = weakref . ref ( im_func , remove ) <EOL> self . self_name = str ( im_self ) <EOL> self . func_name = str ( im_func . __name__ ) <EOL> def calculate_key ( cls , target ) : <EOL> """<STR_LIT>""" <EOL> return ( id ( get_self ( target ) ) , id ( get_func ( target ) ) ) <EOL> calculate_key = classmethod ( calculate_key ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , <EOL> self . self_name , <EOL> self . func_name , <EOL> ) <EOL> __repr__ = __str__ <EOL> def __nonzero__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self ( ) is not None <EOL> def __cmp__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( other , self . __class__ ) : <EOL> return cmp ( self . __class__ , type ( other ) ) <EOL> return cmp ( self . key , other . key ) <EOL> def __call__ ( self ) : <EOL> """<STR_LIT>""" <EOL> target = self . weak_self ( ) <EOL> if target is not None : <EOL> function = self . weak_func ( ) <EOL> if function is not None : <EOL> return function . __get__ ( target ) <EOL> return None </s>
