<s> import select as select_orig <EOL> error = select_orig . error <EOL> from greenlet import getcurrent <EOL> from . . hubs import get_hub <EOL> from . . const import READ , WRITE <EOL> ERROR = '<STR_LIT:error>' <EOL> __patched__ = [ '<STR_LIT>' ] <EOL> def get_fileno ( obj ) : <EOL> try : <EOL> f = obj . fileno <EOL> except AttributeError : <EOL> if not isinstance ( obj , int ) : <EOL> raise TypeError ( "<STR_LIT>" + type ( obj ) ) <EOL> return obj <EOL> else : <EOL> rv = f ( ) <EOL> if not isinstance ( rv , int ) : <EOL> raise TypeError ( "<STR_LIT>" + type ( rv ) ) <EOL> return rv <EOL> def select ( read_list , write_list , error_list , timeout = None ) : <EOL> if timeout is not None : <EOL> try : <EOL> timeout = float ( timeout ) <EOL> except ValueError : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> hub = get_hub ( ) <EOL> timers = [ ] <EOL> current = getcurrent ( ) <EOL> assert hub is not current , '<STR_LIT>' <EOL> files = { } <EOL> for r in read_list : <EOL> files [ get_fileno ( r ) ] = { READ : r } <EOL> for w in write_list : <EOL> files . setdefault ( get_fileno ( w ) , { } ) [ WRITE ] = w <EOL> for e in error_list : <EOL> files . setdefault ( get_fileno ( e ) , { } ) [ ERROR ] = e <EOL> listeners = [ ] <EOL> def on_read ( d ) : <EOL> original = files [ get_fileno ( d ) ] [ READ ] <EOL> current . switch ( ( [ original ] , [ ] , [ ] ) ) <EOL> def on_write ( d ) : <EOL> original = files [ get_fileno ( d ) ] [ WRITE ] <EOL> current . switch ( ( [ ] , [ original ] , [ ] ) ) <EOL> def on_error ( d , _err = None ) : <EOL> original = files [ get_fileno ( d ) ] [ ERROR ] <EOL> current . switch ( ( [ ] , [ ] , [ original ] ) ) <EOL> def on_timeout2 ( ) : <EOL> current . switch ( ( [ ] , [ ] , [ ] ) ) <EOL> def on_timeout ( ) : <EOL> timers . append ( hub . schedule_call_global ( <NUM_LIT:0> , on_timeout2 ) ) <EOL> if timeout is not None : <EOL> timers . append ( hub . schedule_call_global ( timeout , on_timeout ) ) <EOL> try : <EOL> for fd , v in files . items ( ) : <EOL> if v . get ( READ ) : <EOL> listeners . append ( hub . add ( READ , fd , on_read , on_error , ( fd , ) ) ) <EOL> if v . get ( WRITE ) : <EOL> listeners . append ( hub . add ( WRITE , fd , on_write , on_error , ( fd , ) ) ) <EOL> try : <EOL> return hub . switch ( ) <EOL> finally : <EOL> for l in listeners : <EOL> hub . remove ( l ) <EOL> finally : <EOL> for t in timers : <EOL> t . cancel ( ) </s>
