<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import sys <EOL> from gevent . hub import get_hub , getcurrent , _NONE <EOL> from gevent . _compat import reraise <EOL> from gevent . hub import InvalidSwitchError <EOL> from gevent . timeout import Timeout <EOL> from gevent . _tblib import dump_traceback , load_traceback <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class _AbstractLinkable ( object ) : <EOL> _notifier = None <EOL> def __init__ ( self ) : <EOL> self . _links = set ( ) <EOL> self . hub = get_hub ( ) <EOL> def ready ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def _check_and_notify ( self ) : <EOL> if self . ready ( ) : <EOL> if self . _links and not self . _notifier : <EOL> self . _notifier = self . hub . loop . run_callback ( self . _notify_links ) <EOL> def rawlink ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> if not callable ( callback ) : <EOL> raise TypeError ( '<STR_LIT>' % ( callback , ) ) <EOL> self . _links . add ( callback ) <EOL> self . _check_and_notify ( ) <EOL> def unlink ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _links . remove ( callback ) <EOL> except KeyError : <EOL> pass <EOL> def _notify_links ( self ) : <EOL> todo = set ( self . _links ) <EOL> for link in todo : <EOL> if link in self . _links : <EOL> try : <EOL> link ( self ) <EOL> except : <EOL> self . hub . handle_error ( ( link , self ) , * sys . exc_info ( ) ) <EOL> if getattr ( link , '<STR_LIT>' , None ) : <EOL> self . unlink ( link ) <EOL> del todo <EOL> del self . _notifier <EOL> def _wait_core ( self , timeout , catch = Timeout ) : <EOL> switch = getcurrent ( ) . switch <EOL> self . rawlink ( switch ) <EOL> try : <EOL> timer = Timeout . _start_new_or_dummy ( timeout ) <EOL> try : <EOL> try : <EOL> result = self . hub . switch ( ) <EOL> if result is not self : <EOL> raise InvalidSwitchError ( '<STR_LIT>' % ( result , ) ) <EOL> return True <EOL> except catch as ex : <EOL> if ex is not timer : <EOL> raise <EOL> return False <EOL> finally : <EOL> timer . cancel ( ) <EOL> finally : <EOL> self . unlink ( switch ) <EOL> def _wait_return_value ( self , waited , wait_success ) : <EOL> return None <EOL> def _wait ( self , timeout = None ) : <EOL> if self . ready ( ) : <EOL> return self . _wait_return_value ( False , False ) <EOL> gotit = self . _wait_core ( timeout ) <EOL> return self . _wait_return_value ( True , gotit ) <EOL> class Event ( _AbstractLinkable ) : <EOL> """<STR_LIT>""" <EOL> _flag = False <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , ( self . _flag and '<STR_LIT>' ) or '<STR_LIT>' , len ( self . _links ) ) <EOL> def is_set ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _flag <EOL> isSet = is_set <EOL> ready = is_set <EOL> def set ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _flag = True <EOL> self . _check_and_notify ( ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _flag = False <EOL> def _wait_return_value ( self , waited , gotit ) : <EOL> if not waited : <EOL> flag = self . _flag <EOL> assert flag , "<STR_LIT>" <EOL> return flag <EOL> return gotit <EOL> def wait ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _wait ( timeout ) <EOL> def _reset_internal_locks ( self ) : <EOL> pass <EOL> class AsyncResult ( _AbstractLinkable ) : <EOL> """<STR_LIT>""" <EOL> _value = _NONE <EOL> _exc_info = ( ) <EOL> _notifier = None <EOL> @ property <EOL> def _exception ( self ) : <EOL> return self . _exc_info [ <NUM_LIT:1> ] if self . _exc_info else _NONE <EOL> @ property <EOL> def value ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _value if self . _value is not _NONE else None <EOL> @ property <EOL> def exc_info ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _exc_info : <EOL> return ( self . _exc_info [ <NUM_LIT:0> ] , self . _exc_info [ <NUM_LIT:1> ] , load_traceback ( self . _exc_info [ <NUM_LIT:2> ] ) ) <EOL> return ( ) <EOL> def __str__ ( self ) : <EOL> result = '<STR_LIT>' % ( self . __class__ . __name__ , ) <EOL> if self . value is not None or self . _exception is not _NONE : <EOL> result += '<STR_LIT>' % self . value <EOL> if self . _exception is not None and self . _exception is not _NONE : <EOL> result += '<STR_LIT>' % self . _exception <EOL> if self . _exception is _NONE : <EOL> result += '<STR_LIT>' <EOL> return result + '<STR_LIT>' % len ( self . _links ) <EOL> def ready ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _exc_info or self . _value is not _NONE <EOL> def successful ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _value is not _NONE <EOL> @ property <EOL> def exception ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _exc_info : <EOL> return self . _exc_info [ <NUM_LIT:1> ] <EOL> def set ( self , value = None ) : <EOL> """<STR_LIT>""" <EOL> self . _value = value <EOL> self . _check_and_notify ( ) <EOL> def set_exception ( self , exception , exc_info = None ) : <EOL> """<STR_LIT>""" <EOL> if exc_info : <EOL> self . _exc_info = ( exc_info [ <NUM_LIT:0> ] , exc_info [ <NUM_LIT:1> ] , dump_traceback ( exc_info [ <NUM_LIT:2> ] ) ) <EOL> else : <EOL> self . _exc_info = ( type ( exception ) , exception , dump_traceback ( None ) ) <EOL> self . _check_and_notify ( ) <EOL> def _raise_exception ( self ) : <EOL> reraise ( * self . exc_info ) <EOL> def get ( self , block = True , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _value is not _NONE : <EOL> return self . _value <EOL> if self . _exc_info : <EOL> return self . _raise_exception ( ) <EOL> if not block : <EOL> raise Timeout ( ) <EOL> self . _wait_core ( timeout , ( ) ) <EOL> return self . get ( block = False ) <EOL> def get_nowait ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get ( block = False ) <EOL> def _wait_return_value ( self , waited , gotit ) : <EOL> return self . value <EOL> def wait ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _wait ( timeout ) <EOL> def __call__ ( self , source ) : <EOL> if source . successful ( ) : <EOL> self . set ( source . value ) <EOL> else : <EOL> self . set_exception ( source . exception , getattr ( source , '<STR_LIT>' , None ) ) <EOL> def result ( self , timeout = None ) : <EOL> return self . get ( timeout = timeout ) <EOL> set_result = set <EOL> def done ( self ) : <EOL> return self . ready ( ) <EOL> def cancel ( self ) : <EOL> return False <EOL> def cancelled ( self ) : <EOL> return False </s>
