
    \dXV                     "   d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	m
Z
mZ ddlmZmZ ddlmZmZmZmZmZmZmZmZmZmZ ddlmZmZ dd	lmZmZ dd
l m!Z!m"Z" ddl#m$Z$m%Z%  e	j&        d          Z' e	j&        d          Z(	 ddl)m*Z+ e+Z*n# e,$ r dZ*Y nw xY w	 ddlm-Z. e.Z-n# e,$ r dZ-Y nw xY we"dk    Z/dZ0e/r
ddlm1Z1m2Z2 dZ0 e!j3                    r	 ddl4Z4dZ0n# e,$ r dZ4Y nw xY w G d d          Z5 eeee           G d dee5e                      Z6 G d d          Z7 ee           G d de7e5                      Z8e*e- ee6e           e/r ee6ee           e0r ee6e            e9edd           ee6e           dgZ:dS )z
Posix reactor base class
    N)Sequence)classImplementsimplementer)errortcpudp)ReactorBase_SignalReactorMixin)
IHalfCloseableDescriptorIReactorFDSetIReactorMulticastIReactorProcessIReactorSocketIReactorSSLIReactorTCPIReactorUDPIReactorUNIXIReactorUNIXDatagram)CONNECTION_DONECONNECTION_LOST)failurelog)platformplatformType   )_SIGCHLDWaker_WakerzHandler has no fileno methodzFile descriptor lost)tls)sslposixF)processunixTc                       e Zd ZdZej         ej         ej                              ej         ej         ej                              ifdZ	dS )_DisconnectSelectableMixinz>
    Mixin providing the C{_disconnectSelectable} method.
    c                    |                      |           |                    |j                  }|rn|r@|j        t          j        k    r+t          j        |          r|                    |           dS |                     |           |	                    |           dS |                     |           |	                    t          j        |                     dS )z
        Utility function for disconnecting a selectable.

        Supports half-close notification, isRead should be boolean indicating
        whether error resulted from doRead().
        N)removeReaderget	__class__r   ConnectionDoner   
providedByreadConnectionLostremoveWriterconnectionLostr   Failure)self
selectablewhyisReadfaildictfs         :lib/python3.11/site-packages/twisted/internet/posixbase.py_disconnectSelectablez0_DisconnectSelectableMixin._disconnectSelectableM   s      	*%%%LL'' 	<-MU%999,7
CC : --a00000!!*---))!,,,,,j)))%%goc&:&:;;;;;    N)
__name__
__module____qualname____doc__r   r)   r   r.   ConnectionLostr6    r7   r5   r$   r$   H   sy           /'/2F%2F2H2H"I"I /'/2F%2F2H2H"I"I
< < < < < <r7   r$   c                       e Zd ZU dZeZd ZdZd Zd Z	di dddddfdZ
ddZ	 ddZd dZd!dZd"dZ	 d#dZer%ej        ej        ej        fZeej                 ed<   nej        ej        fZd Zd Z	 d$dZd%dZd&dZ	 d&dZd%dZ d Z!dS )'PosixReactorBasez
    A basis for reactors that use file descriptors.

    @ivar _childWaker: L{None} or a reference to the L{_SIGCHLDWaker}
        which is used to properly notice child process termination.
    c                     | j         sU|                     |           | _         | j                            | j                    |                     | j                    dS dS )z
        Install a `waker' to allow threads and signals to wake up the IO thread.

        We use the self-pipe trick (http://cr.yp.to/docs/selfpipe.html) to wake
        the reactor. On Windows we use a pair of sockets.
        N)waker_wakerFactory_internalReadersadd	addReaderr/   s    r5   installWakerzPosixReactorBase.installWaker{   s`     z 	'++D11DJ!%%dj111NN4:&&&&&	' 	'r7   Nc                 Z   t          j        |            t          dk    rt          r| j        sMt          |           | _        | j                            | j                   |                     | j                   | j        	                                 t          j                     dS dS dS )z
        Extend the basic signal handling logic to also support
        handling SIGCHLD to know when to try to reap child processes.
        r    N)r
   _handleSignalsr   processEnabled_childWakerr   rC   rD   rE   installr!   reapAllProcessesrF   s    r5   rI   zPosixReactorBase._handleSignals   s    
 	*40007""~"# 1#0#6#6 %))$*:;;;t/000$$&&& $&&&&& #"""r7   c                 J    | j         r| j                                          dS dS )a  
        If a child waker was created and installed, uninstall it now.

        Since this disables reactor functionality and is only called
        when the reactor is stopping, it doesn't provide any directly
        useful functionality, but the cleanup of reactor-related
        process-global state that it does helps in unit tests
        involving multiple reactors and is generally just a nice
        thing.
        N)rK   	uninstallrF   s    r5   _uninstallHandlerz"PosixReactorBase._uninstallHandler   s4      	)&&(((((	) 	)r7   r=   r   c
                    t           dk    rK|r-|	t          d          t          j        | ||||||||	  	        S t          j        | ||||||||		  	        S t           dk    rp|t          d          |t          d          |rt          d          |	rt          d          t
          rdd	lm}
  |
| |||||          S t          d
          t          d          )Nr    z1Using childFDs is not supported with usePTY=True.win32z,Setting UID is unsupported on this platform.z,Setting GID is unsupported on this platform.z1The usePTY parameter is not supported on Windows.z1Customizing childFDs is not supported on Windows.r   )Processz:spawnProcess not available since pywin32 is not installed.z0spawnProcess only available on Windows or POSIX.)r   
ValueErrorr!   
PTYProcessrS   win32processtwisted.internet._dumbwin32procNotImplementedError)r/   processProtocol
executableargsenvpathuidgidusePTYchildFDsrS   s              r5   spawnProcesszPosixReactorBase.spawnProcess   sS    7"" '$K   )*dCSRX   #
 
 
 W$$ !OPPP !OPPP V !TUUU V !TUUU CCCCCCwt_j$TRRR)P   &B  r7        c                 ^    t          j        |||||           }|                                 |S )zConnects a given L{DatagramProtocol} to the given numeric UDP port.

        @returns: object conforming to L{IListeningPort}.
        )r   PortstartListening)r/   portprotocol	interfacemaxPacketSizeps         r5   	listenUDPzPosixReactorBase.listenUDP   s2    
 HT8YtDD	r7   Fc                 `    t          j        ||||| |          }|                                 |S )zConnects a given DatagramProtocol to the given numeric UDP port.

        EXPERIMENTAL.

        @returns: object conforming to IListeningPort.
        )r   MulticastPortrg   )r/   rh   ri   rj   rk   listenMultiplerl   s          r5   listenMulticastz PosixReactorBase.listenMulticast   s<     (I}dN
 
 	
r7      c                     t           s
J d            t          j        |||| |          }|                                 |S NUNIX support is not present)unixEnabledr"   	Connectorconnect)r/   addressfactorytimeoutcheckPIDcs         r5   connectUNIXzPosixReactorBase.connectUNIX  s@    999999N7GWdHEE			r7   2     c                     t           s
J d            t          j        ||||| |          }|                                 |S rt   )rv   r"   rf   rg   )r/   ry   rz   backlogmodewantPIDrl   s          r5   
listenUNIXzPosixReactorBase.listenUNIX  sG    999999IgwtWEE	r7   c                     t           s
J d            t          j        |||||           }|                                 |S )z
        Connects a given L{DatagramProtocol} to the given path.

        EXPERIMENTAL.

        @returns: object conforming to L{IListeningPort}.
        ru   )rv   r"   DatagramPortrg   )r/   ry   ri   rk   r   rl   s         r5   listenUNIXDatagramz#PosixReactorBase.listenUNIXDatagram  sH     999999gxdKK	r7   c                     t           s
J d            t          j        ||||||           }|                                 |S )zd
        Connects a L{ConnectedDatagramProtocol} instance to a path.

        EXPERIMENTAL.
        ru   )rv   r"   ConnectedDatagramPortrg   )r/   ry   ri   rk   r   bindAddressrl   s          r5   connectUNIXDatagramz$PosixReactorBase.connectUNIXDatagram   sQ     999999&X}dK
 
 	
r7   _supportedAddressFamiliesc                    || j         vrt          j        |          t          r2|t          j        k    r"t          j                            | ||          }n"t          j                            | |||          }|
                                 |S )a0  
        Create a new L{IListeningPort} from an already-initialized socket.

        This just dispatches to a suitable port implementation (eg from
        L{IReactorTCP}, etc) based on the specified C{addressFamily}.

        @see: L{twisted.internet.interfaces.IReactorSocket.adoptStreamPort}
        )r   r   UnsupportedAddressFamilyrv   socketAF_UNIXr"   rf   _fromListeningDescriptorr   rg   )r/   fileDescriptoraddressFamilyrz   rl   s        r5   adoptStreamPortz PosixReactorBase.adoptStreamPort=  s      >>>0??? 	=FN::	224QQAA11nmW A 	
r7   c                     || j         vrt          j        |          t          r1|t          j        k    r!t          j                            |||           S t          j                            ||||           S )zg
        @see:
            L{twisted.internet.interfaces.IReactorSocket.adoptStreamConnection}
        )
r   r   r   rv   r   r   r"   Server_fromConnectedSocketr   )r/   r   r   rz   s       r5   adoptStreamConnectionz&PosixReactorBase.adoptStreamConnectionR  st    
  >>>0??? 	=FN::;33NGTRRR:22w  r7   c                     |t           j        t           j        fvrt          j        |          t
          j                            | ||||          }|                                 |S )N)rk   )	r   AF_INETAF_INET6r   r   r   rf   r   rg   )r/   r   r   ri   rk   rl   s         r5   adoptDatagramPortz"PosixReactorBase.adoptDatagramPorta  si      AAA0???H--.- . 
 
 	
r7   c                 ^    t          j        |||||           }|                                 |S N)r   rf   rg   )r/   rh   rz   r   rj   rl   s         r5   	listenTCPzPosixReactorBase.listenTCPo  s0    HT7GY==	r7   c                 `    t          j        ||||||           }|                                 |S r   )r   rw   rx   )r/   hostrh   rz   r{   r   r}   s          r5   
connectTCPzPosixReactorBase.connectTCPt  s-    M$gwTJJ			r7   c           	         t           5t                               |d|          }|                     |||||          S t          6t                              |||||||           }|                                 |S J d            )NTFSSL support is not present)r   TLSMemoryBIOFactoryr   r   rw   rx   )	r/   r   rh   rz   contextFactoryr{   r   
tlsFactoryr}   s	            r5   
connectSSLzPosixReactorBase.connectSSL{  s     ?00wOOJ??4z7KPPP_dG^Wk4 A IIKKKH66666r7   c                    t           =t                               |d|          }|                     ||||          }d|_        |S t          5t                              ||||||           }|                                 |S J d            )NFTLSr   )r   r   r   _typer   rf   rg   )r/   rh   rz   r   r   rj   r   rl   s           r5   	listenSSLzPosixReactorBase.listenSSL  s    ?00PPJ>>$
GYGGDDJK_wDQQAH66666r7   c                     t          |          | j        z
  }|D ]}|                     |           t          |          }|D ]}|                     |           t	          ||z            S )ag  
        Remove all readers and writers, and list of removed L{IReadDescriptor}s
        and L{IWriteDescriptor}s.

        Meant for calling from subclasses, to implement removeAll, like::

          def removeAll(self):
              return self._removeAll(self._reads, self._writes)

        where C{self._reads} and C{self._writes} are iterables.
        )setrC   r&   r,   list)r/   readerswritersremovedReadersreaderremovedWriterswriters          r5   
_removeAllzPosixReactorBase._removeAll  s     W(==$ 	& 	&Ff%%%%W$ 	& 	&Ff%%%%N^3444r7   )rc   rd   )rc   rd   F)rr   r   )r   r   r   )rd   r   )rd   r   N)rd   )r   rc   )rr   N)"r8   r9   r:   r;   r   rB   rG   rK   rI   rP   rb   rm   rq   r~   r   r   r   rv   r   r   r   r   r   r   AddressFamily__annotations__r   r   r   r   r   r   r   r   r=   r7   r5   r?   r?   n   s           M
' 
' 
' K' ' '&) ) ). 6 6 6 6t    PU   "          NR   "  

NONE
!8F,@#A 	
 	
 	
 	
 NO%
!
  *    FJ
 
 
 
   
    LP7 7 7 77 7 7 75 5 5 5 5r7   r?   c                       e Zd ZdZd ZdS )_PollLikeMixina  
    Mixin for poll-like reactors.

    Subclasses must define the following attributes::

      - _POLL_DISCONNECTED - Bitmask for events indicating a connection was
        lost.
      - _POLL_IN - Bitmask for events indicating there is input to read.
      - _POLL_OUT - Bitmask for events indicating output can be written.

    Must be mixed in to a subclass of PosixReactorBase (for
    _disconnectSelectable).
    c                    d}d}|| j         z  r%|| j        z  s|| j        v r
d}t          }nt          }n	 |                                dk    rt          }nB|| j        z  r|                                }d}|s || j        z  r|	                                }d}n<# t          $ r/ t          j                    d         }t          j                     Y nw xY w|r|                     |||           dS dS )zg
        fd is available for read or write, do the work and raise errors if
        necessary.
        NFTr   )_POLL_DISCONNECTED_POLL_IN_readsr   r   fileno_NO_FILEDESCdoRead	_POLL_OUTdoWriteBaseExceptionsysexc_infor   errr6   )r/   r0   fdeventr1   inReads         r5   _doReadOrWritez_PollLikeMixin._doReadOrWrite  s>   
 4** .	EDM4I .	 T[  
 % & $$&&",, 'CCt}, &(//11!% '54>#9 ' )0022!&     lnnQ'						
  	@&&z3?????	@ 	@s   A"B 6CCN)r8   r9   r:   r;   r   r=   r7   r5   r   r     s2         7@ 7@ 7@ 7@ 7@r7   r   c                   f    e Zd ZdZdZdZdZd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd ZdS )_ContinuousPollinga  
    Schedule reads and writes based on the passage of time, rather than
    notification.

    This is useful for supporting polling filesystem files, which C{epoll(7)}
    does not support.

    The implementation uses L{_PollLikeMixin}, which is a bit hacky, but
    re-implementing and testing the relevant code yet again is unappealing.

    @ivar _reactor: The L{EPollReactor} that is using this instance.

    @ivar _loop: A C{LoopingCall} that drives the polling, or L{None}.

    @ivar _readers: A C{set} of C{FileDescriptor} objects that should be read
        from.

    @ivar _writers: A C{set} of C{FileDescriptor} objects that should be
        written to.
    r         c                 n    || _         d | _        t                      | _        t                      | _        d S r   )_reactor_loopr   _readers_writers)r/   reactors     r5   __init__z_ContinuousPolling.__init__  s*    
r7   c                    | j         s| j        rU| j        Lddlm}m}  || j                  | _        | j        | j        _        | j        	                    |d           dS dS | j        r"| j        
                                 d| _        dS dS )zh
        Start or stop a C{LoopingCall} based on whether there are readers and
        writers.
        Nr   )_EPSILONLoopingCallF)now)r   r   r   twisted.internet.taskr   r   iterater   clockstartstop)r/   r   r   s      r5   
_checkLoopz_ContinuousPolling._checkLoop  s    
 = 	DM 	z!GGGGGGGG([66
#'=
  
  u 55555 "! Z 	JOODJJJ	 	r7   c                     t          | j                  D ]}|                     ||| j                   t          | j                  D ]}|                     ||| j                   dS )zX
        Call C{doRead} and C{doWrite} on all readers and writers respectively.
        N)r   r   r   r   r   r   )r/   r   r   s      r5   r   z_ContinuousPolling.iterate+  s~     4=)) 	? 	?F>>>>4=)) 	@ 	@F????	@ 	@r7   c                 b    | j                             |           |                                  dS )zU
        Add a C{FileDescriptor} for notification of data available to read.
        N)r   rD   r   r/   r   s     r5   rE   z_ContinuousPolling.addReader4  0     	&!!!r7   c                 b    | j                             |           |                                  dS )zV
        Add a C{FileDescriptor} for notification of data available to write.
        N)r   rD   r   r/   r   s     r5   	addWriterz_ContinuousPolling.addWriter;  r   r7   c                     	 | j                             |           n# t          $ r Y dS w xY w|                                  dS )zY
        Remove a C{FileDescriptor} from notification of data available to read.
        N)r   removeKeyErrorr   r   s     r5   r&   z_ContinuousPolling.removeReaderB  sX    	M  (((( 	 	 	FF	    
++c                     	 | j                             |           n# t          $ r Y dS w xY w|                                  dS )zb
        Remove a C{FileDescriptor} from notification of data available to
        write.
        N)r   r   r   r   r   s     r5   r,   z_ContinuousPolling.removeWriterL  sX    
	M  (((( 	 	 	FF	r   c                     t          | j        | j        z            }| j                                         | j                                         |S )z1
        Remove all readers and writers.
        )r   r   r   clear)r/   results     r5   	removeAllz_ContinuousPolling.removeAllW  sI     dmdm344 	r7   c                 *    t          | j                  S )z/
        Return a list of the readers.
        )r   r   rF   s    r5   
getReadersz_ContinuousPolling.getReadersb       DM"""r7   c                 *    t          | j                  S )z/
        Return a list of the writers.
        )r   r   rF   s    r5   
getWritersz_ContinuousPolling.getWritersh  r   r7   c                     || j         v S )aj  
        Checks if the file descriptor is currently being observed for read
        readiness.

        @param fd: The file descriptor being checked.
        @type fd: L{twisted.internet.abstract.FileDescriptor}
        @return: C{True} if the file descriptor is being observed for read
            readiness, C{False} otherwise.
        @rtype: C{bool}
        )r   r/   r   s     r5   	isReadingz_ContinuousPolling.isReadingn       T]""r7   c                     || j         v S )al  
        Checks if the file descriptor is currently being observed for write
        readiness.

        @param fd: The file descriptor being checked.
        @type fd: L{twisted.internet.abstract.FileDescriptor}
        @return: C{True} if the file descriptor is being observed for write
            readiness, C{False} otherwise.
        @rtype: C{bool}
        )r   r   s     r5   	isWritingz_ContinuousPolling.isWriting{  r   r7   N)r8   r9   r:   r;   r   r   r   r   r   r   rE   r   r&   r,   r   r   r   r   r   r=   r7   r5   r   r     s         , HI    $@ @ @      	 	 		 	 	# # ## # ## # ## # # # #r7   r   fromfd);r;   r   r   typingr   zope.interfacer   r   twisted.internetr   r   r   twisted.internet.baser	   r
   twisted.internet.interfacesr   r   r   r   r   r   r   r   r   r   twisted.internet.mainr   r   twisted.pythonr   r   twisted.python.runtimer   r   _signalsr   r   ConnectionFdescWentAway
_NO_FILENOr   twisted.protocolsr   _tlsImportErrorr   _sslrv   rJ   r!   r"   	isWindowsrV   r$   r?   r   r   getattr__all__r=   r7   r5   <module>r     s(  
 
  



       7 7 7 7 7 7 7 7 , , , , , , , , , , B B B B B B B B                        C B B B B B B B ' ' ' ' ' ' ' ' 9 9 9 9 9 9 9 9 + + + + + + + + +U*+IJJ
,u,-CDD------ CC    
CCC
,,,,,, CC    
CCC
 g% ........N 8    #< #< #< #< #< #< #< #<L [+'899|5 |5 |5 |5 |5*,F |5 |5 :9|5~	F@ F@ F@ F@ F@ F@ F@ F@R ]N# N# N# N# N#)C N# N# N#b ?coO$k222 JO$l4HIII 7O$o666
768T"".O$n555
s6   B BBB$ $B.-B.C C&%C&