%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/twisted/internet/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/twisted/internet/__pycache__/_signals.cpython-312.pyc

�

Ϫ�f?8���UdZddlmZddlZddlZddlZddlZddlZddlm	Z	ddl
mZmZm
Z
ddlmZmZmZddlmZmZddl
mZmZdd	lmZdd
lmZmZmZddlmZedk(rd
dlm Z m!Z!ee"ee	gdfZ#de$d<d'd�Z%d�Z&Gd�de�Z'eGd�d��Z(eGd�d��Z)eGd�d��Z*eGd�d��Z+Gd�de�Z,ee,�Gd�d ejZ��Z.ee�Gd!�d"ejZ��Z/ee,�Gd#�d$e/��Z0edk(re0Z1ne.Z1Gd%�d&e/�Z2y)(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�2�|dk(r3tjtjtj�nKd�}tjtj|�tjtjd�tj|�S)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��y�N�)�argss �;/usr/lib/python3/dist-packages/twisted/internet/_signals.py�noopSignalHandlerz)installHandler.<locals>.noopSignalHandlerMs���F)�signal�SIGCHLD�SIG_DFL�siginterrupt�
set_wakeup_fd)�fdr s  r�installHandlerr(;s`��
�R�x��
�
�f�n�n�f�n�n�5�	�	�
�
�f�n�n�&7�8����F�N�N�E�2�����#�#r!c�j�tjtj�tjk(S)zI
    Determine whether the I{SIGCHLD} handler is the default or not.
    )r"�	getsignalr#r$rr!r�isDefaultHandlerr+Us"�����F�N�N�+�v�~�~�=�=r!c� �eZdZdZdd�Zdd�Zy)�SignalHandlingaF
    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��y)z.
        Install the signal handlers.
        Nr��selfs r�installzSignalHandling.installf��r!c��y)zB
        Restore signal handlers to their original state.
        Nrr/s r�	uninstallzSignalHandling.uninstallkr2r!N��return�None��__name__�
__module__�__qualname__�__doc__r1r4rr!rr-r-\s����
r!r-c� �eZdZdZdd�Zdd�Zy)�_WithoutSignalHandlingz�
    A L{SignalHandling} implementation that does no signal handling.

    This is the implementation of C{installSignalHandlers=False}.
    c��y)z5
        Do not install any signal handlers.
        Nrr/s rr1z_WithoutSignalHandling.installyr2r!c��y)zB
        Do nothing because L{install} installed nothing.
        Nrr/s rr4z _WithoutSignalHandling.uninstall~r2r!Nr5r8rr!rr>r>qs����
r!r>c�@�eZdZUdZded<ded<ded<d	d�Zd	d�Zy)
�_WithSignalHandlingzg
    A reactor core helper that can manage signals: it installs signal handlers
    at start time.
    r�_sigInt�	_sigBreak�_sigTermc��tjtj�tjk(r.tjtj|j�tjtj
|j�ttdd�}|�!tj||j�yy)zI
        Install the signal handlers for the Twisted event loop.
        �SIGBREAKN)	r"r*�SIGINT�default_int_handlerrC�SIGTERMrE�getattrrD)r0rGs  rr1z_WithSignalHandling.install�sz�����F�M�M�*�f�.H�.H�H��M�M�&�-�-����6��
�
�f�n�n�d�m�m�4��6�:�t�4�����M�M�(�D�N�N�3� r!c��y)zE
        At the moment, do nothing (for historical reasons).
        Nrr/s rr4z_WithSignalHandling.uninstall�r2r!Nr5)r9r:r;r<�__annotations__r1r4rr!rrBrB�s%���
������4�r!rBc�:�eZdZUdZded<dZded<d
d�Zd
d�Zy	)�_MultiSignalHandlinga
    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.
    zSequence[SignalHandling]�_signalHandlingsF�bool�
_installedc�T�|jD]}|j��d|_y)NT)rPr1rR�r0�ds  rr1z_MultiSignalHandling.install�s'���&�&�	�A�
�I�I�K�	���r!c�n�|jr)|jD]}|j��d|_yy)NF)rRrPr4rTs  rr4z_MultiSignalHandling.uninstall�s3���?�?��*�*�
�����
�
�#�D�O�r!Nr5)r9r:r;r<rMrRr1r4rr!rrOrO�s#���$/�.��J����
$r!rOc�D�eZdZUdZded<ded<dZded<d
d�Zd
d	�Zy)�_ChildSignalHandlinga	
    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�_removeInternalReaderNzOptional[_SIGCHLDWaker]�_childWakerc���|j�*t�|_|j|j�|jj�t	j
�y)z�
        Extend the basic signal handling logic to also support handling
        SIGCHLD to know when to try to reap child processes.
        N)r[�
_SIGCHLDWakerrYr1r�reapAllProcessesr/s rr1z_ChildSignalHandling.install�sN�����#�,��D���#�#�D�$4�$4�5���� � �"�	� � �"r!c��|j�J�|j|j�|jj�|jjt	j
t
d���d|_y)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�uninstalled)r[rZr4�connectionLostr�Failure�	Exceptionr/s rr4z_ChildSignalHandling.uninstall�sj�����+�+�+�
	
�"�"�4�#3�#3�4����"�"�$����'�'����	�-�8P�(Q�R� ��r!r5)r9r:r;r<rMr[r1r4rr!rrXrX�s*��
�<�;�>�>�+/�K�(�/�#�& r!rXc�8�eZdZdZed�Zdd�Zdd�Zdd�Zy)	�_IWakeraQ
    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��y)z:
        Called when the event should be wake up.
        Nrrr!r�wakeUpz_IWaker.wakeUpr2r!c��y)zC
        Read some data from my connection and discard it.
        Nrrr!r�doReadz_IWaker.doReadr2r!c��y)zB
        Called when connection was closed and the pipes.
        Nr)�reasons rraz_IWaker.connectionLostr2r!Nr5)rlzfailure.Failurer6r7)	r9r:r;r<r�disconnectedrhrjrarr!rreres!����R�=�L��
�
r!rec�.�eZdZdZdZdd�Zd�Zd�Zd�Zy)	�_SocketWakerz�
    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.
    rc��tjtjtj�}|jtjtj
d�t
jtjtjtj��5}|jd�|jd�|j|j��|j�\}}ddd�|jd�jd�||_||_|jj |_y#1swY�UxYw)�Initialize.r)z	127.0.0.1rNF)�socket�AF_INET�SOCK_STREAM�
setsockopt�IPPROTO_TCP�TCP_NODELAY�
contextlib�closing�bind�listen�connect�getsockname�accept�setblocking�r�w�fileno)r0�client�server�reader�
clientaddrs     r�__init__z_SocketWaker.__init__-s������v�~�~�v�/A�/A�B�����&�,�,�f�.@�.@�!�D�
�
�
��M�M�&�.�.�&�*<�*<�=�
�	1�
��K�K�(�)��M�M�!���N�N�6�-�-�/�0�!'�����F�J�
	1�	���5�!����5�!��������f�f�m�m���	1�	1�s
�'AE�Ec���	tj|jjd�y#t$r+}|j
dtjk7r�Yd}~yd}~wwxYw)zSend a byte to my connection.�xrN)r�untilConcludesr��send�OSErrorr�errno�WSAEWOULDBLOCK�r0�es  rrhz_SocketWaker.wakeUp?sM��	���������T�2���	��v�v�a�y�E�0�0�0��1��	�s�*-�	A!�!A�A!c�Z�	|jjd�y#t$rYywxYw)z4
        Read some data from my connection.
        i N)r��recvr�r/s rrjz_SocketWaker.doReadGs)��	��F�F�K�K�����	��	�s��	*�*c�l�|jj�|jj�yr)r��closer�)r0rls  rraz_SocketWaker.connectionLostPs�������������r!Nr5)	r9r:r;r<rmr�rhrjrarr!rroro"s"����L�$�$��r!roc�@�eZdZUdZdZded<ded<d
d�Zd
d�Zd�Zy	)�_FDWakera�
    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�T��tj�\�_�_t	j
�j�t	j�j�t	j
�j�t	j�j��fd��_y)rqc����jSr)r�r/s�r�<lambda>z#_FDWaker.__init__.<locals>.<lambda>qs���d�f�f�r!N)�os�piper�r�r�setNonBlocking�_setCloseOnExecr�r/s`rr�z_FDWaker.__init__jse������������
���T�V�V�$�
���d�f�f�%�
���T�V�V�$�
���d�f�f�%�$��r!c�N�tj|j�d��y)zA
        Read some bytes from the pipe and discard them.
        c��yrr)�datas rr�z!_FDWaker.doRead.<locals>.<lambda>wr2r!N)r�
readFromFDr�r/s rrjz_FDWaker.doReadss��	�������(9�:r!c��t|d�sy|j|jfD]}	tj|��|`|`y#t
$rY�+wxYw)zClose both ends of my pipe.r�N)�hasattrr�r�r�r�r�)r0rlr's   rraz_FDWaker.connectionLostysY���t�S�!���&�&�$�&�&�.�	�B�
������	�

�F�D�F���
��
�s�A�	A�ANr5)	r9r:r;r<rmrMr�rjrarr!rr�r�Us'����L�
�F�
�F�%�;�	r!r�c��eZdZdZd�Zy)�
_UnixWakerz�
    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���|j�1	tjtj|jd�yy#t
$r(}|jtjk7r�Yd}~yd}~wwxYw)z)Write one byte to the pipe, and flush it.Nr�)r�rr�r��writer�r��EAGAINr�s  rrhz_UnixWaker.wakeUp�s^���6�6��
��#�#�B�H�H�d�f�f�d�;����
��7�7�e�l�l�*��+��
�s�/?�	A0�A+�+A0N)r9r:r;r<rhrr!rr�r��s���r!r�c�4��eZdZdZdd�Zdd�Zd�fd�Z�xZS)r]zQ
    L{_SIGCHLDWaker} can wake up a reactor whenever C{SIGCHLD} is received.
    c�.�t|j�y)zJ
        Install the handler necessary to make this waker active.
        N)r(r�r/s rr1z_SIGCHLDWaker.install�s��	�t�v�v�r!c��td�y)zC
        Remove the handler which makes this waker active.
        rN)r(r/s rr4z_SIGCHLDWaker.uninstall�s��	�r�r!c�J��t�|��tj�y)a
        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)�superrjrr^)r0�	__class__s �rrjz_SIGCHLDWaker.doRead�s���	����� � �"r!r5)r9r:r;r<r1r4rj�
__classcell__)r�s@rr]r]�s������
#�
#r!r])r'r�r6r�)3r<�
__future__rrxr�r�r"rr�typesr�typingrrr�zope.interfacerr	r
�attrsrrr
r�twisted.internet.interfacesr�twisted.pythonrrr�twisted.python.runtimerrfrrr�rrMr(r+r-r>rBrOrXre�Loggerror�r��_Wakerr]rr!r�<module>r�s}��
�:#���	�
�
��/�/�<�<� �&�7�-�-�/��7�� �#�S�(�9�*=�$>��$D�E�
�y�E�$�4>��X��*�����$�����@�$�$��$�D�9 �9 ��9 �x�i��:
�W��/�3�:�:�/��/�d
�_��,�s�z�z�,��,�^
�W�������*�7��
�F��F�#�H�#r!

Zerion Mini Shell 1.0