U e5dH@sdZddlmZmZddlmZddlmZddlZddl Z ddl Z dZ dZ dd Z ed d d d dgZde_e jdkrdej_dej_dej_dej_GdddeZGdddedZGdddeZGdddeZGdddeZee d rGd!d"d"eZee d#r Gd$d%d%eZee d&rtd|dYnXdSN{!r} is not registered)r_fileobj_lookuprKeyErrorr)rrrrrr __getitem__Es  z_SelectorMapping.__getitem__cCs t|jjSr)iterrrr rrr__iter__Lsz_SelectorMapping.__iter__N)__name__ __module__ __qualname____doc__rr!r&r(rrrrr<s rc@sneZdZdZedddZeddZdddZedd 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. NcCstdS)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 NNotImplementedErrorrrrrrrrregister_szBaseSelector.registercCstdS)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) Nr.)rrrrr unregistervszBaseSelector.unregistercCs||||||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 )r2r1r0rrrmodifys zBaseSelector.modifycCstdS)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 Nr.)rtimeoutrrrselectszBaseSelector.selectcCsdS)zmClose the selector. This must be called to make sure that any underlying resource is freed. Nrr rrrcloseszBaseSelector.closecCsL|}|dkrtdz ||WStk rFtd|dYnXdS)zzReturn the key associated to a registered file object. Returns: SelectorKey for this file object NzSelector is closedr#)get_map RuntimeErrorr%r)rrmappingrrrget_keys zBaseSelector.get_keycCstdS)z2Return a mapping of file objects to selector keys.Nr.r rrrr7szBaseSelector.get_mapcCs|Srrr rrr __enter__szBaseSelector.__enter__cGs |dSr)r6)rargsrrr__exit__szBaseSelector.__exit__)N)N)N)r)r*r+r,rr1r2r3r5r6r:r7r;r=rrrrr-Ps     r-) metaclassc@sTeZdZdZddZddZdddZd d Zdd d Zd dZ ddZ ddZ dS)_BaseSelectorImplzBase selector implementation.cCsi|_t||_dSr)rr_mapr rrrrsz_BaseSelectorImpl.__init__cCsNz t|WStk rH|jD]}|j|kr$|jYSq$YnXdS)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 rvaluesrrrrkeyrrrr$s   z!_BaseSelectorImpl._fileobj_lookupNcCsb|r|ttB@r td|t|||||}|j|jkrRtd||j||j|j<|S)NzInvalid events: {!r}z"{!r} (FD {}) is already registered) EVENT_READ EVENT_WRITEr rrr$rrr%rrrrrCrrrr1s  z_BaseSelectorImpl.registercCs@z|j||}Wn$tk r:td|dYnX|Sr")rpopr$r%rrBrrrr2s z_BaseSelectorImpl.unregistercCsz|j||}Wn$tk r8td|dYnX||jkr^||||||}n"||jkr|j|d}||j|j <|S)Nr#)r) rr$r%rrr2r1r_replacerrFrrrr3s     z_BaseSelectorImpl.modifycCs|jd|_dSr)rclearr@r rrrr6 s z_BaseSelectorImpl.closecCs|jSr)r@r rrrr7sz_BaseSelectorImpl.get_mapcCs(z |j|WStk r"YdSXdS)zReturn the key associated to a given file descriptor. Parameters: fd -- file descriptor Returns: corresponding key, or None if not found N)rr%)rrrrr _key_from_fds  z_BaseSelectorImpl._key_from_fd)N)N) r)r*r+r,rr$r1r2r3r6r7rJrrrrr?s  r?cs`eZdZdZfddZdfdd ZfddZejd krHdd d Z ne j Z dd d Z Z S)SelectSelectorzSelect-based selector.cstt|_t|_dSr)superrset_readers_writersr  __class__rrr%s zSelectSelector.__init__Ncs@t|||}|t@r&|j|j|t@r<|j|j|Sr)rLr1rDrNaddrrErOrFrPrrr1*s zSelectSelector.registercs,t|}|j|j|j|j|Sr)rLr2rNdiscardrrOrBrPrrr22s zSelectSelector.unregisterZwin32cCs$t||||\}}}|||gfSr)r5)rrw_r4xrrr_select9szSelectSelector._selectc Cs|dkr dnt|d}g}z||j|jg|\}}}Wntk rP|YSXt|}t|}||BD]J}d}||kr|tO}||kr|tO}||}|rj| |||j @fqj|SNr) maxrXrNrOInterruptedErrorrMrDrErJappendr) rr4readyrTrUrVrrrCrrrr5?s$   zSelectSelector.select)N)N)N) r)r*r+r,rr1r2sysplatformrXr5 __classcell__rrrPrrK"s    rKcs^eZdZdZdZdZdZfddZd fdd ZfddZ dfd d Z dd d Z Z S)_PollLikeSelectorzsL   ~T3Z  .  M