<s> import greenlet <EOL> import sys <EOL> from collections import deque <EOL> from . import hubs <EOL> from . timeout import Timeout <EOL> from . hubs import get_hub <EOL> from . hubs . switch import gyield <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class _NONE : <EOL> __slots__ = [ ] <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> _NONE = _NONE ( ) <EOL> class Event : <EOL> """<STR_LIT>""" <EOL> _result = None <EOL> _exc = None <EOL> def __init__ ( self ) : <EOL> self . _waiters = set ( ) <EOL> self . reset ( ) <EOL> def __str__ ( self ) : <EOL> params = ( self . __class__ . __name__ , hex ( id ( self ) ) , <EOL> self . _result , self . _exc , len ( self . _waiters ) ) <EOL> return '<STR_LIT>' % params <EOL> def reset ( self ) : <EOL> assert self . _result is not _NONE , '<STR_LIT>' <EOL> self . _result = _NONE <EOL> self . _exc = None <EOL> def ready ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _result is not _NONE <EOL> def has_exception ( self ) : <EOL> return self . _exc is not None <EOL> def has_result ( self ) : <EOL> return self . _result is not _NONE and self . _exc is None <EOL> def poll ( self , notready = None ) : <EOL> if self . ready ( ) : <EOL> return self . wait ( ) <EOL> return notready <EOL> def poll_exception ( self , notready = None ) : <EOL> if self . has_exception ( ) : <EOL> return self . wait ( ) <EOL> return notready <EOL> def poll_result ( self , notready = None ) : <EOL> if self . has_result ( ) : <EOL> return self . wait ( ) <EOL> return notready <EOL> def wait ( self ) : <EOL> """<STR_LIT>""" <EOL> current = greenlet . getcurrent ( ) <EOL> if self . _result is _NONE : <EOL> self . _waiters . add ( current ) <EOL> try : <EOL> gyield ( False ) <EOL> finally : <EOL> self . _waiters . discard ( current ) <EOL> if self . _exc is not None : <EOL> current . throw ( * self . _exc ) <EOL> return self . _result <EOL> def send ( self , result = None , exc = None ) : <EOL> """<STR_LIT>""" <EOL> assert self . _result is _NONE , '<STR_LIT>' <EOL> self . _result = result <EOL> if exc is not None and not isinstance ( exc , tuple ) : <EOL> exc = ( exc , ) <EOL> self . _exc = exc <EOL> hub = hubs . get_hub ( ) <EOL> for waiter in self . _waiters : <EOL> hub . schedule_call_global ( <EOL> <NUM_LIT:0> , self . _do_send , self . _result , self . _exc , waiter ) <EOL> def _do_send ( self , result , exc , waiter ) : <EOL> if waiter in self . _waiters : <EOL> if exc is None : <EOL> waiter . switch ( result ) <EOL> else : <EOL> waiter . throw ( * exc ) <EOL> def send_exception ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> return self . send ( None , args ) <EOL> class TEvent : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _links = set ( ) <EOL> self . _todo = set ( ) <EOL> self . _flag = False <EOL> self . hub = get_hub ( ) <EOL> self . _notifier = None <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> 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 . _todo . update ( self . _links ) <EOL> if self . _todo and not self . _notifier : <EOL> self . _notifier = True <EOL> self . hub . schedule_call_now ( self . _notify_links ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _flag = False <EOL> def wait ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _flag : <EOL> return self . _flag <EOL> else : <EOL> switch = greenlet . getcurrent ( ) . switch <EOL> self . rawlink ( switch ) <EOL> try : <EOL> timer = Timeout ( timeout ) <EOL> try : <EOL> try : <EOL> gyield ( False ) <EOL> except Timeout as ex : <EOL> if ex is not timer : <EOL> raise <EOL> finally : <EOL> timer . cancel ( ) <EOL> finally : <EOL> self . unlink ( switch ) <EOL> return self . _flag <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> if self . _flag and not self . _notifier : <EOL> self . _todo . add ( callback ) <EOL> self . _notifier = True <EOL> self . hub . schedule_call_now ( self . _notify_links ) <EOL> def unlink ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _links . remove ( callback ) <EOL> except ValueError : <EOL> pass <EOL> def _notify_links ( self ) : <EOL> while self . _todo : <EOL> link = self . _todo . pop ( ) <EOL> if link in self . _links : <EOL> try : <EOL> link ( self ) <EOL> except : <EOL> self . hub . handle_error ( ( link , self ) , * sys . exc_info ( ) ) <EOL> def _reset_internal_locks ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class AsyncResult : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _links = deque ( ) <EOL> self . value = None <EOL> self . _exception = _NONE <EOL> self . hub = get_hub ( ) <EOL> self . _notifier = None <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 . _exception is not _NONE <EOL> def successful ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _exception is None <EOL> @ property <EOL> def exception ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _exception is not _NONE : <EOL> return self . _exception <EOL> def set ( self , value = None ) : <EOL> """<STR_LIT>""" <EOL> self . value = value <EOL> self . _exception = None <EOL> if self . _links and not self . _notifier : <EOL> self . _notifier = self . hub . loop . run_callback ( self . _notify_links ) <EOL> def set_exception ( self , exception ) : <EOL> """<STR_LIT>""" <EOL> self . _exception = exception <EOL> if self . _links and not self . _notifier : <EOL> self . _notifier = self . hub . loop . run_callback ( self . _notify_links ) <EOL> def get ( self , block = True , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _exception is not _NONE : <EOL> if self . _exception is None : <EOL> return self . value <EOL> raise self . _exception <EOL> elif block : <EOL> switch = greenlet . getcurrent ( ) . switch <EOL> self . rawlink ( switch ) <EOL> try : <EOL> timer = Timeout ( timeout ) <EOL> try : <EOL> result = self . hub . switch ( ) <EOL> assert result is self , '<STR_LIT>' % ( result , ) <EOL> finally : <EOL> timer . cancel ( ) <EOL> except : <EOL> self . unlink ( switch ) <EOL> raise <EOL> if self . _exception is None : <EOL> return self . value <EOL> raise self . _exception <EOL> else : <EOL> raise Timeout <EOL> def get_nowait ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get ( block = False ) <EOL> def wait ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _exception is not _NONE : <EOL> return self . value <EOL> else : <EOL> switch = greenlet . getcurrent ( ) . switch <EOL> self . rawlink ( switch ) <EOL> try : <EOL> timer = Timeout ( timeout ) <EOL> try : <EOL> result = self . hub . switch ( ) <EOL> assert result is self , '<STR_LIT>' % ( result , ) <EOL> finally : <EOL> timer . cancel ( ) <EOL> except Timeout as exc : <EOL> self . unlink ( switch ) <EOL> if exc is not timer : <EOL> raise <EOL> except : <EOL> self . unlink ( switch ) <EOL> raise <EOL> return self . value <EOL> def _notify_links ( self ) : <EOL> while self . _links : <EOL> link = self . _links . popleft ( ) <EOL> try : <EOL> link ( self ) <EOL> except : <EOL> self . hub . handle_error ( ( link , self ) , * sys . exc_info ( ) ) <EOL> def rawlink ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> if not callable ( callback ) : <EOL> raise TypeError ( '<STR_LIT>' % ( callback , ) ) <EOL> self . _links . append ( callback ) <EOL> if self . ready ( ) and not self . _notifier : <EOL> self . _notifier = self . hub . loop . run_callback ( self . _notify_links ) <EOL> def unlink ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _links . remove ( callback ) <EOL> except ValueError : <EOL> pass <EOL> def __call__ ( self , source ) : <EOL> if source . successful ( ) : <EOL> self . set ( source . value ) <EOL> else : <EOL> self . set_exception ( source . exception ) </s>
