[_A@sdZddlmZmZddlmZmZddlZddlZddl Z dZ d Z ddZ edd d d d gZ Gd ddeZGddddeZGdddeZGdddeZeedr GdddeZneedr4GdddeZneedr\GdddeZndekrteZn6dekreZndekreZneZdS)!z|Selectors module. This module allows high-level and efficient I/O multiplexing, built upon the `select` module primitives. )ABCMetaabstractmethod) namedtupleMappingNc Cst|tr|}nLyt|j}Wn3tttfk rctdj|dYnX|dkrtdj|n|S)zReturn a file descriptor from a file object. Parameters: fileobj -- file object or file descriptor Returns: corresponding file descriptor Raises: ValueError if the object is invalid zInvalid file object: {!r}NrzInvalid file descriptor: {}) isinstanceintfilenoAttributeError TypeError ValueErrorformat)fileobjfdr/usr/lib/python3.4/selectors.py_fileobj_to_fds    r SelectorKeyrreventsdatac@sFeZdZdZddZddZddZdd Zd S) _SelectorMappingz)Mapping of file objects to selector keys.cCs ||_dS)N) _selector)selfZselectorrrr__init__5sz_SelectorMapping.__init__cCst|jjS)N)lenr _fd_to_key)rrrr__len__8sz_SelectorMapping.__len__c CsUy$|jj|}|jj|SWn*tk rPtdj|dYnXdS)Nz{!r} is not registered)r_fileobj_lookuprKeyErrorr )rrrrrr __getitem__;s  z_SelectorMapping.__getitem__cCst|jjS)N)iterrr)rrrr__iter__Bsz_SelectorMapping.__iter__N)__name__ __module__ __qualname____doc__rrrr!rrrrr2s    rc@seZdZdZedddZeddZdddZedd d Zd d Z d dZ eddZ ddZ ddZ dS) BaseSelectora-Selector abstract base class. A selector supports registering file objects to be monitored for specific I/O events. A file object is a file descriptor or any object with a `fileno()` method. An arbitrary object can be attached to the file object, which can be used for example to store context information, a callback, etc. A selector can use various implementations (select(), poll(), epoll()...) depending on the platform. The default `Selector` class uses the most efficient implementation on the current platform. NcCs tdS)a3Register a file object. Parameters: fileobj -- file object or file descriptor events -- events to monitor (bitwise mask of EVENT_READ|EVENT_WRITE) data -- attached data Returns: SelectorKey instance Raises: ValueError if events is invalid KeyError if fileobj is already registered OSError if fileobj is closed or otherwise is unacceptable to the underlying system call (if a system call is made) Note: OSError may or may not be raised N)NotImplementedError)rrrrrrrregisterUszBaseSelector.registercCs tdS)ajUnregister a file object. Parameters: fileobj -- file object or file descriptor Returns: SelectorKey instance Raises: KeyError if fileobj is not registered Note: If fileobj is registered but has since been closed this does *not* raise OSError (even if the wrapped syscall does) N)r')rrrrr unregisterlszBaseSelector.unregistercCs |j||j|||S)ayChange a registered file object monitored events or attached data. Parameters: fileobj -- file object or file descriptor events -- events to monitor (bitwise mask of EVENT_READ|EVENT_WRITE) data -- attached data Returns: SelectorKey instance Raises: Anything that unregister() or register() raises )r)r()rrrrrrrmodifys zBaseSelector.modifycCs tdS)aqPerform the actual selection, until some monitored file objects are ready or a timeout expires. Parameters: timeout -- if timeout > 0, this specifies the maximum wait time, in seconds if timeout <= 0, the select() call won't block, and will report the currently ready file objects if timeout is None, select() will block until a monitored file object becomes ready Returns: list of (key, events) for ready file objects `events` is a bitwise mask of EVENT_READ|EVENT_WRITE N)r')rtimeoutrrrselectszBaseSelector.selectcCsdS)zmClose the selector. This must be called to make sure that any underlying resource is freed. Nr)rrrrcloseszBaseSelector.closec Cs^|j}y!|dkr$tn||SWn*tk rYtdj|dYnXdS)zzReturn the key associated to a registered file object. Returns: SelectorKey for this file object Nz{!r} is not registered)get_maprr )rrmappingrrrget_keys     zBaseSelector.get_keycCs tdS)z2Return a mapping of file objects to selector keys.N)r')rrrrr.szBaseSelector.get_mapcCs|S)Nr)rrrr __enter__szBaseSelector.__enter__cGs|jdS)N)r-)rargsrrr__exit__szBaseSelector.__exit__)r"r#r$r%rr(r)r*r,r-r0r.r1r3rrrrr&Fs    r& metaclassc@s|eZdZdZddZddZdddZd d Zdd d Zd dZ ddZ ddZ dS)_BaseSelectorImplzBase selector implementation.cCsi|_t||_dS)N)rr_map)rrrrrs z_BaseSelectorImpl.__init__c CsZyt|SWnEtk rUx-|jjD]}|j|kr.|jSq.WYnXdS)alReturn a file descriptor from a file object. This wraps _fileobj_to_fd() to do an exhaustive search in case the object is invalid but we still have it in our map. This is used by unregister() so we can unregister an object that was previously registered even if it is closed. It is also used by _SelectorMapping. N)rr rvaluesrr)rrkeyrrrrs   z!_BaseSelectorImpl._fileobj_lookupNcCs| s|ttB@r.tdj|nt||j|||}|j|jkr|tdj||jn||j|j<|S)NzInvalid events: {!r}z"{!r} (FD {}) is already registered) EVENT_READ EVENT_WRITEr r rrrrr)rrrrr8rrrr(s z_BaseSelectorImpl.registerc CsPy|jj|j|}Wn*tk rKtdj|dYnX|S)Nz{!r} is not registered)rpoprrr )rrr8rrrr)s  z_BaseSelectorImpl.unregisterc Csy|j|j|}Wn*tk rFtdj|dYnX||jkr{|j||j|||}n4||jkr|jd|}||j|j rrrrr5s      r5cseZdZdZfddZdfddZfddZejd krldd d Z n e j Z dd d Z S)SelectSelectorzSelect-based selector.cs)tjt|_t|_dS)N)superrset_readers_writers)r) __class__rrrs  zSelectSelector.__init__Ncs\tj|||}|t@r8|jj|jn|t@rX|jj|jn|S)N)r@r(r9rBaddrr:rC)rrrrr8)rDrrr(!s   zSelectSelector.registercs<tj|}|jj|j|jj|j|S)N)r@r)rBdiscardrrC)rrr8)rDrrr))szSelectSelector.unregisterZwin32cCs2tj||||\}}}|||gfS)N)r,)rrw_r+xrrr_select0s!zSelectSelector._selectc Cs|dkrdn t|d}g}y+|j|j|jg|\}}}Wntk rj|SYnXt|}t|}x|||BD]p}d}||kr|tO}n||kr|tO}n|j|}|r|j |||j @fqqW|S)Nr) maxrKrBrCInterruptedErrorrAr9r:r>appendr) rr+readyrGrHrIrrr8rrrr,6s$!+        !zSelectSelector.select) r"r#r$r%rr(r)sysplatformrKr,rr)rDrr?s  r?pollcs^eZdZdZfddZdfddZfddZdd d ZS) PollSelectorzPoll-based selector.cs tjtj|_dS)N)r@rr,rR_poll)r)rDrrrQs zPollSelector.__init__Ncsltj|||}d}|t@r8|tjO}n|t@rR|tjO}n|jj|j||S)Nr) r@r(r9r,POLLINr:POLLOUTrTr)rrrrr8Z poll_events)rDrrr(Us  zPollSelector.registercs)tj|}|jj|j|S)N)r@r)rTr)rrr8)rDrrr)_szPollSelector.unregisterc Cs|dkrd}n(|dkr*d}ntj|d}g}y|jj|}Wntk rq|SYnXx|D]z\}}d}|tj@r|tO}n|tj@r|t O}n|j |}|ry|j |||j @fqyqyW|S)Nrg@@) mathceilrTrRrMr,rUr:rVr9r>rNr)rr+rO fd_event_listreventrr8rrrr,ds(        !zPollSelector.select)r"r#r$r%rr(r)r,rr)rDrrSNs  rSepollcs|eZdZdZfddZddZdfddZfd d Zdd d Zfd dZ S) EpollSelectorzEpoll-based selector.cs tjtj|_dS)N)r@rr,r[_epoll)r)rDrrrs zEpollSelector.__init__cCs |jjS)N)r]r )rrrrr szEpollSelector.filenoNcsltj|||}d}|t@r8|tjO}n|t@rR|tjO}n|jj|j||S)Nr) r@r(r9r,EPOLLINr:EPOLLOUTr]r)rrrrr8Z epoll_events)rDrrr(s  zEpollSelector.registerc sBtj|}y|jj|jWntk r=YnX|S)N)r@r)r]rOSError)rrr8)rDrrr)s  zEpollSelector.unregisterc Cs|dkrd}n,|dkr*d}ntj|dd}tt|jd}g}y|jj||}Wntk r|SYnXx|D]z\}}d}|tj @r|t O}n|tj @r|t O}n|j |}|r|j|||j@fqqW|S)Nrrg@@gMbP?)rWrXrLrrr]rRrMr,r^r:r_r9r>rNr) rr+max_evrOrYrrZrr8rrrr,s*        !zEpollSelector.selectcs|jjtjdS)N)r]r-r@)r)rDrrr-s zEpollSelector.close) r"r#r$r%rr r(r)r,r-rr)rDrr\s     r\kqueuecs|eZdZdZfddZddZdfddZfd d Zdd d Zfd dZ S)KqueueSelectorzKqueue-based selector.cs tjtj|_dS)N)r@rr,rc_kqueue)r)rDrrrs zKqueueSelector.__init__cCs |jjS)N)rer )rrrrr szKqueueSelector.filenoNcstj|||}|t@r\tj|jtjtj}|jj |gddn|t @rtj|jtj tj}|jj |gddn|S)Nr) r@r(r9r,keventrKQ_FILTER_READZ KQ_EV_ADDrecontrolr:KQ_FILTER_WRITE)rrrrr8kev)rDrrr(s    zKqueueSelector.registercstj|}|jt@rrtj|jtjtj}y|j j |gddWqrt k rnYqrXn|jt @rtj|jtj tj}y|j j |gddWqt k rYqXn|S)Nr)r@r)rr9r,rfrrgZ KQ_EV_DELETErerhr`r:ri)rrr8rj)rDrrr)s       zKqueueSelector.unregisterc Cs|dkrdn t|d}t|j}g}y|jjd||}Wntk rj|SYnXx|D]}|j}|j}d}|tj kr|t O}n|tj kr|t O}n|j |} | rr|j| || j@fqrqrW|S)Nr)rLrrrerhrMZidentfilterr,rgr9rir:r>rNr) rr+rbrOZkev_listrjrZflagrr8rrrr,s&!       !zKqueueSelector.selectcs|jjtjdS)N)rer-r@)r)rDrrr-s zKqueueSelector.close) r"r#r$r%rr r(r)r,r-rr)rDrrds   rdr)r%abcrr collectionsrrrWr,rPr9r:rrrr&r5r?hasattrrSr\rdglobalsZDefaultSelectorrrrrs4    ~U31CH