VcLcsdZddlmZmZddlmZddlmZddlZddl 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)MappingNicsHt|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: {})Z isinstanceZintfilenoZAttributeErrorZ TypeError ValueErrorformat)fileobjfd "/usr/lib64/python3.11/selectors.py_fileobj_to_fdr s'3?  ?W^^%%&&BB :6 ? ? ?$$*F7OO55:> ? ? AvC6==bAABBB Is !<;A7 SelectorKey)r r eventsdatazSelectorKey(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.cs*eZdZdZdZdZdZdZdS)_SelectorMappingz)Mapping of file objects to selector keys.cs||_dSN) _selector)selfselectorr r __init__z_SelectorMapping.__init__?s !c4t|jjSr)lenr _fd_to_keyr r __len__z_SelectorMapping.__len__Bs4>,---rcs |j|}|jj|S#t$r$td|dwxYwN{!r} is not registered)r_fileobj_lookuprKeyErrorr)rr r  r __getitem__z_SelectorMapping.__getitem__Esk O//88B>,R0 0 O O O3::7CCDD$ N Os +..Acrr)Ziterrrrrr __iter__z_SelectorMapping.__iter__LsDN-...rN)__name__ __module__ __qualname____doc__rrr#r$rr rr<sY33"""...OOO/////rrcseZdZdZed 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 ZNotImplementedErrorrr rr r registerzBaseSelector.register_s *"!rcr+)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) r,)rr r r unregisterzBaseSelector.unregisterv ""!rcsZ||||||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 )r0r/r-r.r modifyzBaseSelector.modifys,    }}Wfd333rcr+)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 r,)rtimeoutr r selectzBaseSelector.selectr1rcsdS)zmClose the selector. This must be called to make sure that any underlying resource is freed. Nr)rrr closezBaseSelector.closes rcs|}|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_mapZ RuntimeErrorr!r)rr Zmappingr"r get_keyzBaseSelector.get_keysw ,,..  5344 4 O7# # O O O3::7CCDD$ N Os /.Acr+)z2Return a mapping of file objects to selector keys.r,rrr r6zBaseSelector.get_maps "!rcs|Srr)rrr __enter__zBaseSelector.__enter__s rcs.|dSr)r5)rZargsr r __exit__zBaseSelector.__exit__s rr)r%r&r'r(rr/r0r2r4r5r7r6r8r9r)rr r*r*Ps  """^",""^"$4444""""^"$    O O O""^"rr*)Z metaclasscsFeZdZdZdZdZd dZdZd dZdZ d Z d Z dS) _BaseSelectorImplzBase selector implementation.cs<i|_t||_dSr)rr_maprrr rz_BaseSelectorImpl.__init__s$T** rcs 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. )r rrZvaluesr r rr keyr"r r z!_BaseSelectorImpl._fileobj_lookupsu !'** *   --// " ";')"6MMMMM"   s7A  A NcsH|r|ttzzr"td|t |||||}|j|jvr(td||j||j|j<|S)NzInvalid events: {!r}z"{!r} (FD {}) is already registered) EVENT_READ EVENT_WRITErrrr r rr!rr rrr= r r/z_BaseSelectorImpl.registers DFzK'?%@@ D3::6BBCC C'4#7#7#@#@&$OO 6T_ $ 5?"F7CF3355 5#& rcs |j||}n1#t$r$td|dwxYw|Sr)rZpopr r!rr<r"r r0z_BaseSelectorImpl.unregistersp O/%%d&:&:7&C&CDDCC O O O3::7CCDD$ N O s -0.Acs| |j||}n1#t$r$td|dwxYw||jkr-||||||}n0||jkr%||}||j|j <|S)Nr)r) rr r!rrr0r/r_replacer r@rAr r2z_BaseSelectorImpl.modifys O/$"6"6w"?"?@CC O O O3::7CCDD$ N O SZ  * OOG $ $ $--66CC SX  *,,D,))C&)DOCF # s #.AcsF|jd|_dSr)rZclearr;rrr r5z_BaseSelectorImpl.close s!  rcs|jSr)r;rrr r6z_BaseSelectorImpl.get_maps yrcs@ |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)rr!)rr r r _key_from_fdz_BaseSelectorImpl._key_from_fds6 ?2& &   44 s  r) r%r&r'r(rr r/r0r2r5r6rCr)rr r:r:s''+++ &             rr:csneZdZdZfdZd fd ZfdZejdkrd dZ ne j Z d dZ xZ S) SelectSelectorzSelect-based selector.cstt|_t|_dSr)superrset_readers_writersr __class__ r rzSelectSelector.__init__%s3   rNcst|||}|tzr|j|j|t zr|j|j|Sr)rEr/r>rGZaddr r?rH)rr rrr=rJs r r/zSelectSelector.register*smggw55 J  & M  cf % % % K  & M  cf % % % rcst|}|j|j|j|j|Sr)rEr0rGZdiscardr rHrr r=rJ r r0zSelectSelector.unregister2sOgg  )) cf%%% cf%%% rZwin32csHtj||||\}}}|||zgfSr)r4)rrw_r3Zxs r _selectzSelectSelector._select9s-mAq!W55GAq!a!eR< rcs|dnt|d}g} ||j|jg|\}}}n#t$r|cYSwxYwt |}t |}||zD]V}d}||vr |t z}||vr |tz}||}|r| |||j zfW|SNr) maxrQrGrHInterruptedErrorrFr>r?rCappendr) rr3readyrNrOrPr rr= r r4zSelectSelector.select?s!>$$s7A ll4=$-WMMGAq!!   LLL  FF FFa% 9 9BFQw %*$Qw &+%##B''C 9 c6CJ#67888 s&? A Ar) r%r&r'r(rr/r0sysZplatformrQr4 __classcell__rJ@r rDrD"s     |w      -rrDcsXeZdZdZdZdZdZfdZdfd ZfdZ dfd Z ddZ xZ S) _PollLikeSelectorz _EVENT_READr? _EVENT_WRITErr r0)rr rrr=Z poller_eventsrJ r r/z_PollLikeSelector.register_sggw55 J  . T- -M K  / T. .M  N # #CFM : : : :  GG  w ' ' '  s  A11$Bcst|} |j|jn#t$rYnwxYw|Sr)rEr0rr OSErrorrLrMr r0z_PollLikeSelector.unregistermscgg  ))  N % %cf - - - -    D  sA AAcs |j||}n!#t$rt|ddwxYwd}||jkrud}|tzr ||jz}|t zr ||jz} |j |j |n'#t |xYwd}||j krd}|r&|||}||j|j <|S)Nz is not registeredFrT)rr)rr r!rr>r^r?r_rr2r rEr0rrB)rr rrr=ZchangedZselector_eventsrJs r r2z_PollLikeSelector.modifyws; G/$"6"6w"?"?@CC G G Gg;;;<<$ F G SZ  O " 44#33 # 54#44 %%cfo>>>> ""7+++G 38  G  *,,f4,88C&)DOCF # s $A= B$Ccs|d}n |dkrd}ntj|dz}g} |j|}n#t$r|cYSwxYw|D]g\}}d}||jzr |t z}||jzr |tz}| |}|r| |||j zfh|S)Nr@@) mathceilrpollrTr^r?r_r>rCrUr)rr3rV fd_event_listr eventrr=s r r4z_PollLikeSelector.selects  /GG \ /GGi# ..G  N//88MM   LLL & 9 9IBF((( &+%))) %*$##B''C 9 c6CJ#67888 sA AAr) r%r&r'r(r]r^r_rr/r0r2r4rYrZr[r r\r\UsFFMKL.....      6rr\recs<eZdZdZejZejZej Z dS) PollSelectorzPoll-based selector.N) r%r&r'r(r4rer]POLLINr^POLLOUTr_r)rr rhrhs)"" m ~ rrhepollcsZeZdZdZejZejZej Z dZ ddZfdZ xZ S) EpollSelectorzEpoll-based selector.c4|jSrrrrrr rzEpollSelector.fileno>((** *rNcs|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)NirrbgMbP?r)rcrdrSrrrrerTr4EPOLLINr?EPOLLOUTr>rCrUr) rr3max_evrVrfr rgrr=rWr r4zEpollSelector.selects. :A :)GcM22T9 T_--q11FE  $ 3 3GV D D #     * = = EFN?**k)FFO++)j(F''++=LL#v ':!;<<<LsA** A98A9cz|jtdSrrr5rErIrKr r5zEpollSelector.close, N " " " GGMMOOOOOrr)r%r&r'r(r4rkr]rpr^rqr_rr5rYrZr[r rlrlsw## n   + + +    @         rrldevpollcsReZdZdZejZejZej Z dZ fdZ xZ S)DevpollSelectorzSolaris /dev/poll selector.crmrrnrrr rzDevpollSelector.filenororcrsrrtrIrKr r5zDevpollSelector.closerur)r%r&r'r(r4rvr]rir^rjr_rr5rYrZr[r rwrwsb)) m ~  + + +         rrwkqueuecsPeZdZdZfdZdZd fd ZfdZd dZfdZ xZ S) KqueueSelectorzKqueue-based selector.csxttj|_dSr)rErr4rxrrIrKr rzKqueueSelector.__init__s* GG     #]__DNNNrcrmrrnrrr rzKqueueSelector.filenororNcst|||} |tzrLtj|jtjtj}|j |gdd|tzrLtj|jtj tj}|j |gddn'#t |xYw|SrR) rEr/r>r4keventr KQ_FILTER_READZ KQ_EV_ADDrcontrolr?KQ_FILTER_WRITEr0)rr rrr=kevrJr`r r/zKqueueSelector.registers''""7FD99C J&8 -0E(.(8::CN**C5!Q777K'8 -0F(.(8::CN**C5!Q777 ""7+++Js B,C$C7cst|}|jtzr^t j|jtjtj} |j |gddn#t$rYnwxYw|jtzr^t j|jtj tj} |j |gddn#t$rYnwxYw|SrR)rEr0rr>r4rzr r{Z KQ_EV_DELETErr|rar?r})rr r=r~rJs r r0zKqueueSelector.unregisters ''$$W--CzJ& mCFF,A$*$799N**C5!Q7777DzK' mCFF,B$*$799N**C5!Q7777DJs$"B B  B C-- C:9C:cs|dnt|d}tt|jd}g} |jd||}n#t $r|cYSwxYw|D]|}|j}|j}d}|tj kr |tz}|tj kr |tz}| |} | r|| || jzf}|S)Nrr)rSrrrr|rTZidentZfilterr4r{r>r}r?rCrUr) rr3rrrVZkev_listr~r Zflagrr=s r r4zKqueueSelector.select)s%Bdd3w??GT_--q11FE >11$HH#      = =Yz600)j(F611*k)F''++=LL#v ':!;<<<LsA A&%A&crsrrtrIrKr r5zKqueueSelector.closeBrurr) r%r&r'r(rrr/r0r4r5rYrZr[r ryrys$$ - - - - - + + +            *    2         rrycstt|d}|dS |}|dkr|dn|dS#t$rYdSwxYw)zJCheck if we can use the selector depending upon the operating system. NFrerT)Zgetattrr4rer5ra)ZmethodrZ selector_objr"r _can_userGsvvt,,Hu xzz V  !   a     t uus:A A&%A&)r(ZabcrrZ collectionsrZcollections.abcrrcr4rXr>r?r rr r rrrr*r:rDr\ZhasattrrhrlrwryrZDefaultSelectorr)rr rsb('''''''""""""######   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