VcLdZddlmZmZddlmZddlmZddlZddl Z ddl Z dZ dZ dZ ed gd Zd e_d ej_d ej_dej_dej_GddeZGddeZGddeZGddeZGddeZee drGddeZee drGdd eZee d!rGd"d#eZee d$rGd%d&eZd'Zed$reZdSedreZdSed!reZdSedreZdSeZdS)(z|Selectors module. This module allows high-level and efficient I/O multiplexing, built upon the `select` module primitives. )ABCMetaabstractmethod) namedtuple)MappingNcHt|tr|}na t|}n>#ttt f$r$t d|dwxYw|dkr"t d||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)fileobjfds "/usr/lib64/python3.11/selectors.py_fileobj_to_fdrs'3?  ?W^^%%&&BB :6 ? ? ?$$*F7OO55:> ? ? AvC6==bAABBB Is !<;A7 SelectorKey)rreventsdatazSelectorKey(fileobj, fd, events, data) Object used to associate a file object to its backing file descriptor, selected event mask, and attached data. zFile object registered.zUnderlying file descriptor.z3Events that must be waited for on this file object.zvOptional opaque data associated to this file object. For example, this could be used to store a per-client session ID.c*eZdZdZdZdZdZdZdS)_SelectorMappingz)Mapping of file objects to selector keys.c||_dSN) _selector)selfselectors r__init__z_SelectorMapping.__init__?s !c4t|jjSr)lenr _fd_to_keyrs r__len__z_SelectorMapping.__len__Bs4>,---r c |j|}|jj|S#t$r$td|dwxYwN{!r} is not registered)r_fileobj_lookupr#KeyErrorr)rrrs r __getitem__z_SelectorMapping.__getitem__Esk O//88B>,R0 0 O O O3::7CCDD$ N Os +..Ac4t|jjSr)iterrr#r$s r__iter__z_SelectorMapping.__iter__LsDN-...r N)__name__ __module__ __qualname____doc__rr%r+r.r rrr<sY33"""...OOO/////r rceZdZdZed dZedZd dZed dZdZ dZ ed Z d Z d Z 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. Nct)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 NotImplementedErrorrrrrs rregisterzBaseSelector.register_s *"!r ct)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) r7)rrs r unregisterzBaseSelector.unregisterv ""!r cZ||||||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:r9s rmodifyzBaseSelector.modifys,    }}Wfd333r ct)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 r7)rtimeouts rselectzBaseSelector.selectr=r cdS)zmClose the selector. This must be called to make sure that any underlying resource is freed. Nr3r$s rclosezBaseSelector.closes r c|}|td ||S#t$r$td|dwxYw)zzReturn the key associated to a registered file object. Returns: SelectorKey for this file object NzSelector is closedr()get_map RuntimeErrorr*r)rrmappings rget_keyzBaseSelector.get_keysw ,,..  5344 4 O7# # O O O3::7CCDD$ N Os /.Act)z2Return a mapping of file objects to selector keys.r7r$s rrFzBaseSelector.get_maps "!r c|Srr3r$s r __enter__zBaseSelector.__enter__s r c.|dSr)rD)rargss r__exit__zBaseSelector.__exit__s r r)r/r0r1r2rr:r<r?rBrDrIrFrLrOr3r rr5r5Ps  """^",""^"$4444""""^"$    O O O""^"r r5) metaclasscFeZdZdZdZdZd dZdZd dZdZ d Z d Z dS) _BaseSelectorImplzBase selector implementation.c<i|_t||_dSr)r#r_mapr$s rrz_BaseSelectorImpl.__init__s$T** r c t|S#t$r2|jD]}|j|ur |jccYSwxYw)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. )rrr#valuesrrrrkeys rr)z!_BaseSelectorImpl._fileobj_lookupsu !'** *   --// " ";')"6MMMMM"   s7A  A NcH|r|ttzzr"td|t |||||}|j|jvr(td||j||j|j<|S)NzInvalid events: {!r}z"{!r} (FD {}) is already registered) EVENT_READ EVENT_WRITErrrr)rr#r*rrrrrXs rr:z_BaseSelectorImpl.registers DFzK'?%@@ D3::6BBCC C'4#7#7#@#@&$OO 6T_ $ 5?"F7CF3355 5#& r c |j||}n1#t$r$td|dwxYw|Sr')r#popr)r*rrWs rr<z_BaseSelectorImpl.unregistersp O/%%d&:&:7&C&CDDCC O O O3::7CCDD$ N O s -0.Ac| |j||}n1#t$r$td|dwxYw||jkr-||||||}n0||jkr%||}||j|j <|S)Nr()r) r#r)r*rrr<r:r_replacerr\s rr?z_BaseSelectorImpl.modifys O/$"6"6w"?"?@CC O O O3::7CCDD$ N O SZ  * OOG $ $ $--66CC SX  *,,D,))C&)DOCF # s #.AcF|jd|_dSr)r#clearrTr$s rrDz_BaseSelectorImpl.close s!  r c|jSr)rTr$s rrFz_BaseSelectorImpl.get_maps yr c@ |j|S#t$rYdSwxYw)zReturn the key associated to a given file descriptor. Parameters: fd -- file descriptor Returns: corresponding key, or None if not found N)r#r*)rrs r _key_from_fdz_BaseSelectorImpl._key_from_fds6 ?2& &   44 s  r) r/r0r1r2rr)r:r<r?rDrFrer3r rrRrRs''+++ &             r rRcneZdZdZfdZd fd ZfdZejdkrd dZ ne j Z d dZ xZ S) SelectSelectorzSelect-based selector.ctt|_t|_dSr)superrset_readers_writersr __class__s rrzSelectSelector.__init__%s3   r Nct|||}|tzr|j|j|t zr|j|j|Sr)rir:rZrkaddrr[rl)rrrrrXrns rr:zSelectSelector.register*smggw55 J  & M  cf % % % K  & M  cf % % % r ct|}|j|j|j|j|Sr)rir<rkdiscardrrlrrrXrns rr<zSelectSelector.unregister2sOgg  )) cf%%% cf%%% r win32cHtj||||\}}}|||zgfSr)rB)rrw_rAxs r_selectzSelectSelector._select9s-mAq!W55GAq!a!eR< r c|dnt|d}g} ||j|jg|\}}}n#t$r|cYSwxYwt |}t |}||zD]V}d}||vr |t z}||vr |tz}||}|r| |||j zfW|SNr) maxrzrkrlInterruptedErrorrjrZr[reappendr) rrAreadyrvrwrxrrrXs rrBzSelectSelector.select?s!>$$s7A ll4=$-WMMGAq!!   LLL  FF FFa% 9 9BFQw %*$Qw &+%##B''C 9 c6CJ#67888 s&? A Ar) r/r0r1r2rr:r<sysplatformrzrB __classcell__rns@rrgrg"s     |w      -r rgcXeZdZdZdZdZdZfdZdfd ZfdZ dfd Z ddZ xZ S) _PollLikeSelectorz>>> ""7+++G 38  G  *,,f4,88C&)DOCF # s $A= B$Cc|d}n |dkrd}ntj|dz}g} |j|}n#t$r|cYSwxYw|D]g\}}d}||jzr |t z}||jzr |tz}| |}|r| |||j zfh|S)Nr@@) mathceilrpollr~rr[rrZrerr)rrAr fd_event_listreventrrXs rrBz_PollLikeSelector.selects  /GG \ /GGi# ..G  N//88MM   LLL & 9 9IBF((( &+%))) %*$##B''C 9 c6CJ#67888 sA AAr) r/r0r1r2rrrrr:r<r?rBrrs@rrrUsFFMKL.....      6r rrc<eZdZdZejZejZej Z dS) PollSelectorzPoll-based selector.N) r/r0r1r2rBrrPOLLINrPOLLOUTrr3r rrrs)"" m ~ r repollcZeZdZdZejZejZej Z dZ ddZfdZ xZ S) EpollSelectorzEpoll-based selector.c4|jSrrr r$s rr zEpollSelector.fileno>((** *r Nc|d}n#|dkrd}ntj|dzdz}tt|jd}g} |j||}n#t$r|cYSwxYw|D]q\}}d}|tj zr |tz}|tj zr |tz}| |}|r||||jzfr|S)NrrgMbP?r)rrr}r"r#rrr~rBEPOLLINr[EPOLLOUTrZrerr) rrAmax_evrrrrrrXs rrBzEpollSelector.selects. :A :)GcM22T9 T_--q11FE  $ 3 3GV D D #     * = = EFN?**k)FFO++)j(F''++=LL#v ':!;<<<LsA** A98A9cz|jtdSrrrDrirms rrDzEpollSelector.close, N " " " GGMMOOOOOr r)r/r0r1r2rBrrrrrrr rDrrs@rrrsw## n   + + +    @         r rdevpollcReZdZdZejZejZej Z dZ fdZ xZ S)DevpollSelectorzSolaris /dev/poll selector.c4|jSrrr$s rr zDevpollSelector.filenorr cz|jtdSrrrms rrDzDevpollSelector.closerr )r/r0r1r2rBrrrrrrr rDrrs@rrrsb)) m ~  + + +         r rkqueuecPeZdZdZfdZdZd fd ZfdZd dZfdZ xZ S) KqueueSelectorzKqueue-based selector.cxttj|_dSr)rirrBrrrms rrzKqueueSelector.__init__s* GG     #]__DNNNr c4|jSrrr$s rr zKqueueSelector.filenorr Nct|||} |tzrLtj|jtjtj}|j |gdd|tzrLtj|jtj tj}|j |gddn'#t |xYw|Sr|) rir:rZrBkeventrKQ_FILTER_READ KQ_EV_ADDrcontrolr[KQ_FILTER_WRITEr<)rrrrrXkevrns rr:zKqueueSelector.registers''""7FD99C J&8 -0E(.(8::CN**C5!Q777K'8 -0F(.(8::CN**C5!Q777 ""7+++Js B,C$C7ct|}|jtzr^t j|jtjtj} |j |gddn#t$rYnwxYw|jtzr^t j|jtj tj} |j |gddn#t$rYnwxYw|Sr|)rir<rrZrBrrr KQ_EV_DELETErrrr[r)rrrXrrns rr<zKqueueSelector.unregisters ''$$W--CzJ& mCFF,A$*$799N**C5!Q7777DzK' mCFF,B$*$799N**C5!Q7777DJs$"B B  B C-- C:9C:c|dnt|d}tt|jd}g} |jd||}n#t $r|cYSwxYw|D]|}|j}|j}d}|tj kr |tz}|tj kr |tz}| |} | r|| || jzf}|S)Nrr)r}r"r#rrr~identfilterrBrrZrr[rerr) rrArrkev_listrrflagrrXs rrBzKqueueSelector.select)s%Bdd3w??GT_--q11FE >11$HH#      = =Yz600)j(F611*k)F''++=LL#v ':!;<<<LsA A&%A&cz|jtdSrrrms rrDzKqueueSelector.closeBrr r) r/r0r1r2rr r:r<rBrDrrs@rrrs$$ - - - - - + + +            *    2         r rctt|d}|dS |}|dkr|dn|dS#t$rYdSwxYw)zJCheck if we can use the selector depending upon the operating system. NFrrT)getattrrBrrDr)methodr selector_objs r_can_userGsvvt,,Hu xzz V  !   a     t uus:A A&%A&)r2abcrr collectionsrcollections.abcrrrBrrZr[rrrrrrrr5rRrgrhasattrrrrrrDefaultSelectorr3r rrsb('''''''""""""######   2j(K(K(KLL   8 6 R E /////w///({{{{{W{{{{|QQQQQ QQQh00000&000fWWWWW)WWWt 766&&&&&&(&&& 767-+++++)+++\ 769      +    768LJJJJJ*JJJZ4 8H %$OOO Xg%#OOO Xi%%OOO Xf%"OOO$OOOr