<s> """<STR_LIT>""" <EOL> __docformat__ = "<STR_LIT>" <EOL> import logging <EOL> import select <EOL> from . interfaces import HandlerReady , PrepareAgain <EOL> from . base import MainLoopBase <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> class PollMainLoop ( MainLoopBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , settings = None , handlers = None ) : <EOL> self . _handlers = { } <EOL> self . _unprepared_handlers = { } <EOL> self . poll = select . poll ( ) <EOL> self . _timeout = None <EOL> MainLoopBase . __init__ ( self , settings , handlers ) <EOL> def _add_io_handler ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> self . _unprepared_handlers [ handler ] = None <EOL> self . _configure_io_handler ( handler ) <EOL> def _configure_io_handler ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> if self . check_events ( ) : <EOL> return <EOL> if handler in self . _unprepared_handlers : <EOL> old_fileno = self . _unprepared_handlers [ handler ] <EOL> prepared = self . _prepare_io_handler ( handler ) <EOL> else : <EOL> old_fileno = None <EOL> prepared = True <EOL> fileno = handler . fileno ( ) <EOL> if old_fileno is not None and fileno != old_fileno : <EOL> del self . _handlers [ old_fileno ] <EOL> self . poll . unregister ( old_fileno ) <EOL> if not prepared : <EOL> self . _unprepared_handlers [ handler ] = fileno <EOL> if not fileno : <EOL> return <EOL> self . _handlers [ fileno ] = handler <EOL> events = <NUM_LIT:0> <EOL> if handler . is_readable ( ) : <EOL> logger . debug ( "<STR_LIT>" . format ( handler ) ) <EOL> events |= select . POLLIN <EOL> if handler . is_writable ( ) : <EOL> logger . debug ( "<STR_LIT>" . format ( handler ) ) <EOL> events |= select . POLLOUT <EOL> if events : <EOL> logger . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( handler , fileno , events ) ) <EOL> self . poll . register ( fileno , events ) <EOL> def _prepare_io_handler ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( "<STR_LIT>" . format ( handler ) ) <EOL> ret = handler . prepare ( ) <EOL> logger . debug ( "<STR_LIT>" . format ( ret ) ) <EOL> if isinstance ( ret , HandlerReady ) : <EOL> del self . _unprepared_handlers [ handler ] <EOL> prepared = True <EOL> elif isinstance ( ret , PrepareAgain ) : <EOL> if ret . timeout is not None : <EOL> if self . _timeout is not None : <EOL> self . _timeout = min ( self . _timeout , ret . timeout ) <EOL> else : <EOL> self . _timeout = ret . timeout <EOL> prepared = False <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> return prepared <EOL> def _remove_io_handler ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> if handler in self . _unprepared_handlers : <EOL> old_fileno = self . _unprepared_handlers [ handler ] <EOL> del self . _unprepared_handlers [ handler ] <EOL> else : <EOL> old_fileno = handler . fileno ( ) <EOL> if old_fileno is not None : <EOL> try : <EOL> del self . _handlers [ old_fileno ] <EOL> self . poll . unregister ( old_fileno ) <EOL> except KeyError : <EOL> pass <EOL> def loop_iteration ( self , timeout = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> next_timeout , sources_handled = self . _call_timeout_handlers ( ) <EOL> if self . _quit : <EOL> return sources_handled <EOL> if self . _timeout is not None : <EOL> timeout = min ( timeout , self . _timeout ) <EOL> if next_timeout is not None : <EOL> timeout = min ( next_timeout , timeout ) <EOL> for handler in list ( self . _unprepared_handlers ) : <EOL> self . _configure_io_handler ( handler ) <EOL> events = self . poll . poll ( timeout ) <EOL> self . _timeout = None <EOL> for ( fileno , event ) in events : <EOL> if event & select . POLLHUP : <EOL> self . _handlers [ fileno ] . handle_hup ( ) <EOL> if event & select . POLLNVAL : <EOL> self . _handlers [ fileno ] . handle_nval ( ) <EOL> if event & select . POLLIN : <EOL> self . _handlers [ fileno ] . handle_read ( ) <EOL> elif event & select . POLLERR : <EOL> self . _handlers [ fileno ] . handle_err ( ) <EOL> if event & select . POLLOUT : <EOL> self . _handlers [ fileno ] . handle_write ( ) <EOL> sources_handled += <NUM_LIT:1> <EOL> self . _configure_io_handler ( self . _handlers [ fileno ] ) <EOL> return sources_handled </s>
