QfLtdZddlmZmZddlmZddlmZddlZddl Z ddl Z dZ dZ dZ ed gd Zd e_d ej_d ej _dej"_dej$_GddeZGddeZGddeZGddeZGddeZee dr GddeZee dr Gdd eZee d!r Gd"d#eZee d$r Gd%d&eZd'Zed$reZyedreZyed!reZyedreZyeZy)(z|Selectors module. This module allows high-level and efficient I/O multiplexing, built upon the `select` module primitives. )ABCMetaabstractmethod) namedtuple)MappingNct|tr|}n t|j}|dkrt dj ||S#ttt f$rt dj |dwxYw)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 0/opt/alt/python312/lib64/python3.12/selectors.py_fileobj_to_fdrs'3  ?W^^%&B Av6==bABB I  :6 ?$$*F7O5:> ? ?s A0A? 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(eZdZdZdZdZdZdZy)_SelectorMappingz)Mapping of file objects to selector keys.c||_yN) _selector)selfselectors r__init__z_SelectorMapping.__init__?s !c@t|jjSr)lenr _fd_to_keyrs r__len__z_SelectorMapping.__len__Bs4>>,,--r c |jj|}|jj|S#t$rtdj |dwxYwN{!r} is not registered)r_fileobj_lookupr#KeyErrorr)rrrs r __getitem__z_SelectorMapping.__getitem__Es[ O//8B>>,,R0 0 O3::7CD$ N Os 36%Ac@t|jjSr)iterrr#r$s r__iter__z_SelectorMapping.__iter__LsDNN--..r N)__name__ __module__ __qualname____doc__rr%r+r.r rrr<s3".O/r rcteZdZdZed dZedZd dZed dZdZ dZ ed Z d Z d Z y) 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 cJ|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:r9s rmodifyzBaseSelector.modifys#  }}Wfd33r 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 cy)zmClose the selector. This must be called to make sure that any underlying resource is freed. Nr3r$s rclosezBaseSelector.closes r c|j}| td ||S#t$rtdj|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_keys[ ,,. ?34 4 O7# # O3::7CD$ N Os $%A ct)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$|jyr)rD)rargss r__exit__zBaseSelector.__exit__s  r r)r/r0r1r2rr:r<r?rBrDrIrFrLrOr3r rr5r5Psq "",""$4"""$  O""r r5) metaclasscDeZdZdZdZdZd dZdZd dZdZ d Z d Z y) _BaseSelectorImplzBase selector implementation.c2i|_t||_yr)r#r_mapr$s rrz_BaseSelectorImpl.__init__s$T* r c t|S#t$r?|jjD] }|j|us|j ccYSwxYw)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_lookupsR !'* * --/;;')66M0   s 4AAANcF|r|ttzzrtdj|t ||j |||}|j |jvr%tdj||j ||j|j <|S)NzInvalid events: {!r}z"{!r} (FD {}) is already registered) EVENT_READ EVENT_WRITErrrr)rr#r*rrrrrXs rr:z_BaseSelectorImpl.registersFzK'?%@@3::6BC C'4#7#7#@&$O 66T__ $?"F7CFF35 5#& r c |jj|j|}|S#t$rtdj |dwxYwr')r#popr)r*rrWs rr<z_BaseSelectorImpl.unregistersZ O//%%d&:&:7&CDC  O3::7CD$ N Os *.%Acr |j|j|}||jk7r&|j ||j |||}|S||jk7r+|j|}||j|j<|S#t$rtdj|dwxYw)Nr()r) r#r)r*rrr<r:r_replacerr\s rr?z_BaseSelectorImpl.modifys O//$"6"6w"?@C SZZ  OOG $--6C  SXX ,,D,)C&)DOOCFF #  O3::7CD$ N Os B%B6cF|jjd|_yr)r#clearrTr$s rrDz_BaseSelectorImpl.close s  r c|jSr)rTr$s rrFz_BaseSelectorImpl.get_maps yyr c@ |j|S#t$rYywxYw)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_fds( ??2& &  s  r) r/r0r1r2rr)r:r<r?rDrFrer3r rrRrRs-'+ &   r rRceZdZdZfdZdfd ZfdZejdk(rddZ n e jZ ddZ xZ S) SelectSelectorzSelect-based selector.c^t|t|_t|_yr)superrset_readers_writersr __class__s rrzSelectSelector.__init__%s    r ct||||}|tzr%|jj |j |t zr%|jj |j |Sr)rir:rZrkaddrr[rl)rrrrrXrns rr:zSelectSelector.register*sWgw5 J  MM  cff % K  MM  cff % r ct||}|jj|j|j j|j|Sr)rir<rkdiscardrrlrrrXrns rr<zSelectSelector.unregister2sCg ) cff% cff% r win32cJtj||||\}}}|||zgfSr)rB)rrw_rAxs r_selectzSelectSelector._select9s,mmAq!W5GAq!a!eR< r c|dn t|d}g} |j|j|jg|\}}}t |}t |}||zD]R}d}||vr |t z}||vr |tz}|j|}|s3|j|||jzfT|S#t$r|cYSwxYwNr) maxrzrkrlInterruptedErrorrjrZr[reappendr) rrAreadyrvrwrxrrrXs rrBzSelectSelector.select?s!/$s7A ll4==$--WMGAq! F Fa%BFQw*$Qw+%##B'C c6CJJ#678   L s,B22 C?Cr) r/r0r1r2rr:r<sysplatformrzrB __classcell__rns@rrgrg"s8    ||w --r rgcXeZdZdZdZdZdZfdZdfd ZfdZ dfd Z ddZ xZ S) _PollLikeSelectorzG 388 G ,,f4,8C&)DOOCFF # - Gg[(:;<$ F G "7+sC%&C*C'*C<c|d}n |dkrd}ntj|dz}g} |jj|}|D]m\}}d}||j zr |t z}||jzr |tz}|j|}|sN|j|||jzfo|S#t$r|cYSwxYw)Nr@@) mathceilrpollr~rr[rrZrerr)rrAr fd_event_listreventrrXs rrBz_PollLikeSelector.selects ?G \Gii# .G  NN//8M'IBF((((+%))))*$##B'C c6CJJ#678'   L sB88 CCr) r/r0r1r2rrrrr:r<r?rBrrs@rrrUs.FMKL. 6r rrcXeZdZdZej ZejZejZ y) PollSelectorzPoll-based selector.N) r/r0r1r2rBrrPOLLINrPOLLOUTrr3r rrrs " mm ~~ r repollcxeZdZdZej ZejZejZ dZ ddZfdZ xZ S) EpollSelectorzEpoll-based selector.c6|jjSrrr r$s rr zEpollSelector.fileno>>((* *r c|d}n#|dkrd}ntj|dzdz}tt|jd}g} |j j ||}|D]u\}}d}|tjzr |tz}|tjzr |tz}|j|}|sV|j|||jzfw|S#t$r|cYSwxYw)NrrgMbP?r)rrr}r"r#rrr~rBEPOLLINr[EPOLLOUTrZrerr) rrAmax_evrrrrrrXs rrBzEpollSelector.selectsA))GcM2T9 T__-q1FE  $ 3 3GV D + EFNN?*k)FFOO++j(F''+LL#v ':!;<+L$   s C## C10C1cV|jjt|yrrrDrirms rrDzEpollSelector.close NN " GMOr r)r/r0r1r2rBrrrrrrr rDrrs@rrrs6# nn   + @  r rdevpollcpeZdZdZej ZejZejZ dZ fdZ xZ S)DevpollSelectorzSolaris /dev/poll selector.c6|jjSrrr$s rr zDevpollSelector.filenorr cV|jjt|yrrrms rrDzDevpollSelector.closerr )r/r0r1r2rBrrrrrrr rDrrs@rrrs0) mm ~~  +  r rkqueuecPeZdZdZfdZdZdfd ZfdZddZfdZ xZ S) KqueueSelectorzKqueue-based selector.cbt|tj|_d|_yr|)rirrBrr _max_eventsrms rrzKqueueSelector.__init__s# G  #]]_DN D r c6|jjSrrr$s rr zKqueueSelector.filenorr c:t||||} |tzrptj|j tj tj}|jj|gdd|xjdz c_ |tzrptj|j tjtj}|jj|gdd|xjdz c_ |S#t|5|xYwNrr)rir:rZrBkeventrKQ_FILTER_READ KQ_EV_ADDrcontrolrr[KQ_FILTER_WRITEr<)rrrrrXkevrns rr:zKqueueSelector.registers'"7FD9C J& --0E0E(.(8(8:CNN**C5!Q7$$)$K' --0F0F(.(8(8:CNN**C5!Q7$$)$J "7+s C2DDcxt||}|jtzrqt j |j tjtj}|xjdzc_ |jj|gdd|jtzrst j |j tjtj}|xjdzc_ |jj|gdd|S|S#t$rYwxYw#t$rY|SwxYw)Nrr)rir<rrZrBrrr KQ_EV_DELETErrrrr[r)rrrXrrns rr<zKqueueSelector.unregisters '$W-CzzJ&mmCFFF,A,A$*$7$79  A% NN**C5!Q7 zzK'mmCFFF,B,B$*$7$79  A% NN**C5!Q7J3JJs$7D;D, D)(D), D98D9c|dn t|d}|jxsd}g} |jjd||}|D]}|j }|j }d}|tjk(r |tz}|tjk(r |tz}|j|} | si|j| || jzf|S#t$r|cYSwxYwr)r}rrrr~identfilterrBrrZrr[rerr) rrArrkev_listrrflagrrXs rrBzKqueueSelector.select.s%od3w?G%%*FE >>11$H YYzz6000j(F6111k)F''+LL#v ':!;< L$   sC CCcV|jjt|yrrrms rrDzKqueueSelector.closeGrr r) r/r0r1r2rr r:r<rBrDrrs@rrrs)$ !  + $ . 2  r rctt|d}|y |}|dk(r|jdy|jy#t$rYywxYw)zJCheck if we can use the selector depending upon the operating system. NFrrT)getattrrBrrDr)methodr selector_objs r_can_userLsevvt,H z V    a      sAA AA)r2abcrr collectionsrcollections.abcrrrBrrZr[rrrrrrrr5rRrgrhasattrrrrrrDefaultSelectorr3r rrss("#   2(KL   8 6 R E /w/({W{|Q Qh0&0fW)Wt 66&(& 67+)+\ 69 +  68O*Od4 H$O g#O i%O f"O$Or