
    hJ8                       % S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKJ	r	  SSK
JrJrJr  SSKJrJrJr  SSKJrJr  SSKJrJr  SS	KJr  SS
KJrJrJr  SSKJr  \S:X  a  SSK J!r!J"r"  \\#\\	   /S4   r$S\%S'   S'S jr&S r' " S S\5      r(\ " S S5      5       r)\ " S S5      5       r*\ " S S5      5       r+\ " S S5      5       r, " S S\5      r-\" \-5       " S S \R\                  5      5       r/\" \5       " S! S"\R\                  5      5       r0\" \-5       " S# S$\05      5       r1\S:X  a  \1r2O\/r2 " S% S&\05      r3g)(a  
This module is used to integrate child process termination into a
reactor event loop.  This is a challenging feature to provide because
most platforms indicate process termination via SIGCHLD and do not
provide a way to wait for that signal and arbitrary I/O events at the
same time.  The naive implementation involves installing a Python
SIGCHLD handler; unfortunately this leads to other syscalls being
interrupted (whenever SIGCHLD is received) and failing with EINTR
(which almost no one is prepared to handle).  This interruption can be
disabled via siginterrupt(2) (or one of the equivalent mechanisms);
however, if the SIGCHLD is delivered by the platform to a non-main
thread (not a common occurrence, but difficult to prove impossible),
the main thread (waiting on select() or another event notification
API) may not wake up leading to an arbitrary delay before the child
termination is noticed.

The basic solution to all these issues involves enabling SA_RESTART (ie,
disabling system call interruption) and registering a C signal handler which
writes a byte to a pipe.  The other end of the pipe is registered with the
event loop, allowing it to wake up shortly after SIGCHLD is received.  See
L{_SIGCHLDWaker} for the implementation of the event loop side of this
solution.  The use of a pipe this way is known as the U{self-pipe
trick<http://cr.yp.to/docs/selfpipe.html>}.

From Python version 2.6, C{signal.siginterrupt} and C{signal.set_wakeup_fd}
provide the necessary C signal handler which writes to the pipe to be
registered with C{SA_RESTART}.
    )annotationsN)	FrameType)CallableOptionalSequence)	Attribute	Interfaceimplementer)definefrozen)Protocol	TypeAlias)IReadDescriptor)failurelogutil)platformTypeposix   )fdescprocessr   SignalHandlerc                <   U S:X  a4  [         R                   " [         R                  [         R                  5        OMS n[         R                   " [         R                  U5        [         R                  " [         R                  S5        [         R                  " U 5      $ )a  
Install a signal handler which will write a byte to C{fd} when
I{SIGCHLD} is received.

This is implemented by installing a SIGCHLD handler that does nothing,
setting the I{SIGCHLD} handler as not allowed to interrupt system calls,
and using L{signal.set_wakeup_fd} to do the actual writing.

@param fd: The file descriptor to which to write when I{SIGCHLD} is
    received.

@return: The file descriptor previously configured for this use.
c                     g N )argss    `/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/internet/_signals.pynoopSignalHandler)installHandler.<locals>.noopSignalHandlerM   s        F)signalSIGCHLDSIG_DFLsiginterruptset_wakeup_fd)fdr    s     r   installHandlerr)   ;   s`     
Rxfnnfnn5	 	fnn&78FNNE2##r"   c                 l    [         R                  " [         R                  5      [         R                  :H  $ )zA
Determine whether the I{SIGCHLD} handler is the default or not.
)r#   	getsignalr$   r%   r   r"   r   isDefaultHandlerr,   U   s"     FNN+v~~==r"   c                  ,    \ rS rSrSrSS jrSS jrSrg)SignalHandling\   a.  
The L{SignalHandling} protocol enables customizable signal-handling
behaviors for reactors.

A value that conforms to L{SignalHandling} has install and uninstall hooks
that are called by a reactor at the correct times to have the (typically)
process-global effects necessary for dealing with signals.
c                    g)z
Install the signal handlers.
Nr   selfs    r   installSignalHandling.installf       r"   c                    g)z2
Restore signal handlers to their original state.
Nr   r1   s    r   	uninstallSignalHandling.uninstallk   r5   r"   r   NreturnNone__name__
__module____qualname____firstlineno____doc__r3   r7   __static_attributes__r   r"   r   r.   r.   \   s    
r"   r.   c                  ,    \ rS rSrSrSS jrSS jrSrg)_WithoutSignalHandlingq   z
A L{SignalHandling} implementation that does no signal handling.

This is the implementation of C{installSignalHandlers=False}.
c                    g)z%
Do not install any signal handlers.
Nr   r1   s    r   r3   _WithoutSignalHandling.instally   r5   r"   c                    g)z2
Do nothing because L{install} installed nothing.
Nr   r1   s    r   r7    _WithoutSignalHandling.uninstall~   r5   r"   r   Nr9   r<   r   r"   r   rD   rD   q   s    
r"   rD   c                  L    \ rS rSr% SrS\S'   S\S'   S\S'   SS jrSS jrS	rg
)_WithSignalHandling   z[
A reactor core helper that can manage signals: it installs signal handlers
at start time.
r   _sigInt	_sigBreak_sigTermc                   [         R                  " [         R                  5      [         R                  :X  a/  [         R                   " [         R                  U R                  5        [         R                   " [         R
                  U R                  5        [        [         SS5      nUb!  [         R                   " XR                  5        gg)z9
Install the signal handlers for the Twisted event loop.
SIGBREAKN)	r#   r+   SIGINTdefault_int_handlerrM   SIGTERMrO   getattrrN   )r2   rQ   s     r   r3   _WithSignalHandling.install   sx     FMM*f.H.HHMM&--6fnndmm4 6:t4MM(NN3  r"   c                    g)z5
At the moment, do nothing (for historical reasons).
Nr   r1   s    r   r7   _WithSignalHandling.uninstall   r5   r"   r   Nr9   )	r=   r>   r?   r@   rA   __annotations__r3   r7   rB   r   r"   r   rK   rK      s%    
 4r"   rK   c                  F    \ rS rSr% SrS\S'   SrS\S'   SS jrSS	 jrS
r	g)_MultiSignalHandling   a  
An implementation of L{SignalHandling} which propagates protocol
method calls to a number of other implementations.

This supports composition of multiple signal handling implementations into
a single object so the reactor doesn't have to be concerned with how those
implementations are factored.

@ivar _signalHandlings: The other C{SignalHandling} implementations to
    which to propagate calls.

@ivar _installed: If L{install} has been called but L{uninstall} has not.
    This is used to avoid double cleanup which otherwise results (at least
    during test suite runs) because twisted.internet.reactormixins doesn't
    keep track of whether a reactor has run or not but always invokes its
    cleanup logic.
zSequence[SignalHandling]_signalHandlingsFbool
_installedc                X    U R                    H  nUR                  5         M     SU l        g )NT)r]   r3   r_   r2   ds     r   r3   _MultiSignalHandling.install   s"    &&AIIK 'r"   c                |    U R                   (       a+  U R                   H  nUR                  5         M     SU l         g g )NF)r_   r]   r7   ra   s     r   r7   _MultiSignalHandling.uninstall   s.    ??** +#DO r"   )r_   Nr9   )
r=   r>   r?   r@   rA   rY   r_   r3   r7   rB   r   r"   r   r[   r[      s#    $ /.J
$r"   r[   c                  P    \ rS rSr% SrS\S'   S\S'   SrS\S'   SS	 jrSS
 jrSr	g)_ChildSignalHandling   a  
Signal handling behavior which supports I{SIGCHLD} for notification about
changes to child process state.

@ivar _childWaker: L{None} or a reference to the L{_SIGCHLDWaker} which is
    used to properly notice child process termination.  This is L{None}
    when this handling behavior is not installed and non-C{None}
    otherwise.  This is mostly an unfortunate implementation detail due to
    L{_SIGCHLDWaker} allocating file descriptors as a side-effect of its
    initializer.
z#Callable[[IReadDescriptor], object]_addInternalReader_removeInternalReaderNzOptional[_SIGCHLDWaker]_childWakerc                    U R                   c*  [        5       U l         U R                  U R                   5        U R                   R                  5         [        R
                  " 5         g)zv
Extend the basic signal handling logic to also support handling
SIGCHLD to know when to try to reap child processes.
N)rk   _SIGCHLDWakerri   r3   r   reapAllProcessesr1   s    r   r3   _ChildSignalHandling.install   sN     #,D##D$4$45  " 	  "r"   c                
   U R                   c   eU R                  U R                   5        U R                   R                  5         U R                   R                  [        R
                  " [        S5      5      5        SU l         g)aj  
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uninstalled)rk   rj   r7   connectionLostr   Failure	Exceptionr1   s    r   r7   _ChildSignalHandling.uninstall   sj     +++
 	""4#3#34""$''	-8P(QR  r"   )rk   r9   )
r=   r>   r?   r@   rA   rY   rk   r3   r7   rB   r   r"   r   rg   rg      s*    
 <;>>+/K(/#& r"   rg   c                  F    \ rS rSrSr\" S5      rS	S jrS	S jrS
S jr	Sr
g)_IWakeri  a9  
Interface to wake up the event loop based on the self-pipe trick.

The U{I{self-pipe trick}<http://cr.yp.to/docs/selfpipe.html>}, used to wake
up the main loop from another thread or a signal handler.
This is why we have wakeUp together with doRead

This is used by threads or signals to wake up the event loop.
 c                     g)z*
Called when the event should be wake up.
Nr   r   r"   r   wakeUp_IWaker.wakeUp  r5   r"   c                     g)z3
Read some data from my connection and discard it.
Nr   r   r"   r   doRead_IWaker.doRead  r5   r"   c                    g)z2
Called when connection was closed and the pipes.
Nr   )reasons    r   rr   _IWaker.connectionLost  r5   r"   r   Nr9   )r   zfailure.Failurer:   r;   )r=   r>   r?   r@   rA   r   disconnectedrz   r}   rr   rB   r   r"   r   rw   rw     s!     R=L

r"   rw   c                  8    \ rS rSrSrSrS
S jrS rS rS r	Sr
g	)_SocketWakeri"  z
The I{self-pipe trick<http://cr.yp.to/docs/selfpipe.html>}, implemented
using a pair of sockets rather than pipes (due to the lack of support in
select() on Windows for pipes), used to wake up the main loop from
another thread.
r   c                   [         R                   " [         R                  [         R                  5      nUR                  [         R                  [         R
                  S5        [        R                  " [         R                   " [         R                  [         R                  5      5       nUR                  S5        UR                  S5        UR                  UR                  5       5        UR                  5       u  p4SSS5        UR                  S5        WR                  S5        X0l        Xl        U R                  R                   U l        g! , (       d  f       NX= f)Initialize.r   )z	127.0.0.1r   NF)socketAF_INETSOCK_STREAM
setsockoptIPPROTO_TCPTCP_NODELAY
contextlibclosingbindlistenconnectgetsocknameacceptsetblockingrwfileno)r2   clientserverreader
clientaddrs        r   __init___SocketWaker.__init__-  s     v~~v/A/AB&,,f.@.@!DMM&..&*<*<=
KK()MM!NN6--/0!'F
 	5!5!ffmm
 
s   *AE
Ec                     [         R                  " U R                  R                  S5        g! [         a,  nUR
                  S   [        R                  :w  a  e  SnAgSnAff = f)zSend a byte to my connection.   xr   N)r   untilConcludesr   sendOSErrorr   errnoWSAEWOULDBLOCKr2   es     r   rz   _SocketWaker.wakeUp?  sM    	T2 	vvayE000 1	s   +. 
A$"AA$c                \     U R                   R                  S5        g! [         a     gf = f)z$
Read some data from my connection.
i    N)r   recvr   r1   s    r   r}   _SocketWaker.doReadG  s)    	FFKK 		s    
++c                l    U R                   R                  5         U R                  R                  5         g r   )r   closer   )r2   r   s     r   rr   _SocketWaker.connectionLostP  s    r"   )r   r   r   Nr9   )r=   r>   r?   r@   rA   r   r   rz   r}   rr   rB   r   r"   r   r   r   "  s"     L$$r"   r   c                  L    \ rS rSr% SrSrS\S'   S\S'   SS jrSS jrS	 r	S
r
g)_FDWakeriU  a  
The I{self-pipe trick<http://cr.yp.to/docs/selfpipe.html>}, used to wake
up the main loop from another thread or a signal handler.

L{_FDWaker} is a base class for waker implementations based on
writing to a pipe being monitored by the reactor.

@ivar o: The file descriptor for the end of the pipe which can be
    written to wake up a reactor monitoring this waker.

@ivar i: The file descriptor which should be monitored in order to
    be awoken by this waker.
r   intioc                `  ^  [         R                  " 5       u  T l        T l        [        R
                  " T R                  5        [        R                  " T R                  5        [        R
                  " T R                  5        [        R                  " T R                  5        U 4S jT l        g)r   c                    > T R                   $ r   )r   r1   s   r   <lambda>#_FDWaker.__init__.<locals>.<lambda>q  s	    dffr"   N)ospiper   r   r   setNonBlocking_setCloseOnExecr   r1   s   `r   r   _FDWaker.__init__j  se    TVV$dff%TVV$dff%$r"   c                P    [         R                  " U R                  5       S 5        g)z1
Read some bytes from the pipe and discard them.
c                    g r   r   )datas    r   r   !_FDWaker.doRead.<locals>.<lambda>w  s    Tr"   N)r   
readFromFDr   r1   s    r   r}   _FDWaker.doReads  s     	(9:r"   c                    [        U S5      (       d  gU R                  U R                  4 H  n [        R                  " U5        M     U ?U ?g! [
         a     M0  f = f)zClose both ends of my pipe.r   N)hasattrr   r   r   r   r   )r2   r   r(   s      r   rr   _FDWaker.connectionLosty  sW    tS!!&&$&&.B !
 FDF  s   A
AA)r   r   r   Nr9   )r=   r>   r?   r@   rA   r   rY   r   r}   rr   rB   r   r"   r   r   r   U  s'     L
F
F%;	r"   r   c                      \ rS rSrSrS rSrg)
_UnixWakeri  z
This class provides a simple interface to wake up the event loop.

This is used by threads or signals to wake up the event loop.
c                    U R                   b2   [        R                  " [        R                  U R                   S5        gg! [
         a)  nUR                  [        R                  :w  a  e  SnAgSnAff = f)z)Write one byte to the pipe, and flush it.Nr   )r   r   r   r   writer   r   EAGAINr   s     r   rz   _UnixWaker.wakeUp  s^     66##BHHdffd;    77ell* +s   0A 
A4A//A4r   N)r=   r>   r?   r@   rA   rz   rB   r   r"   r   r   r     s    r"   r   c                  D   ^  \ rS rSrSrSS jrSS jrSU 4S jjrSrU =r	$ )rm   i  zI
L{_SIGCHLDWaker} can wake up a reactor whenever C{SIGCHLD} is received.
c                .    [        U R                  5        g)z:
Install the handler necessary to make this waker active.
N)r)   r   r1   s    r   r3   _SIGCHLDWaker.install  s     	tvvr"   c                    [        S5        g)z3
Remove the handler which makes this waker active.
r   N)r)   r1   s    r   r7   _SIGCHLDWaker.uninstall  s     	rr"   c                L   > [         TU ]  5         [        R                  " 5         g)z
Having woken up the reactor in response to receipt of
C{SIGCHLD}, reap the process which exited.

This is called whenever the reactor notices the waker pipe is
writeable, which happens soon after any call to the C{wakeUp}
method.
N)superr}   r   rn   )r2   	__class__s    r   r}   _SIGCHLDWaker.doRead  s     	  "r"   r   r9   )
r=   r>   r?   r@   rA   r3   r7   r}   rB   __classcell__)r   s   @r   rm   rm     s    
# 
#r"   rm   )r(   r   r:   r   )4rA   
__future__r   r   r   r   r#   r   typesr   typingr   r   r   zope.interfacer   r	   r
   attrsr   r   typing_extensionsr   r   twisted.internet.interfacesr   twisted.pythonr   r   r   twisted.python.runtimer   rx   r   r   r   r   rY   r)   r,   r.   rD   rK   r[   rg   rw   Loggerr   r   r   _Wakerrm   r   r"   r   <module>r      s}  
: #   	    / / < <   1 7 - - /7 #S(9*=$>$DEy E$4>X *   $   @ $ $ $D 9  9  9 xi : W/3:: / /d _,szz , ,^ W  * 7F F#H #r"   