HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/twisted/internet/__pycache__/asyncioreactor.cpython-38.pyc
U

��W[9)�@s�dZddlmZmZddlZddlmZddlmZddl	m
Z
ddlmZm
Z
mZddlmZdd	lmZzdd
lmZWnek
r�ed��YnXddlmZmZGd
d�de�Zee�Gdd�de��Zddd�ZdS)z'
asyncio-based reactor implementation.
�)�absolute_import�divisionN)�implementer)�Logger)�DelayedCall)�PosixReactorBase�_NO_FILEDESC�_ContinuousPolling)�callWithLogger)�
IReactorFDSet)�get_event_loopzRequires asyncio.)�PermissionError�BrokenPipeErrorc@s eZdZdZdd�Zdd�ZdS)�	_DCHandlez�
    Wraps ephemeral L{asyncio.Handle} instances.  Callbacks can close
    over this and use it as a mutable reference to asyncio C{Handles}.

    @ivar handle: The current L{asyncio.Handle}
    cCs
||_dS�N)�handle)�selfr�r�A/usr/lib/python3/dist-packages/twisted/internet/asyncioreactor.py�__init__'sz_DCHandle.__init__cCs|j��dS)z5
        Cancel the inner L{asyncio.Handle}.
        N)r�cancel�rrrrr+sz_DCHandle.cancelN)�__name__�
__module__�__qualname__�__doc__rrrrrrr srcs�eZdZdZdZe�Zd)�fdd�	Zdd�Zdd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd*dd�Z�fdd �Z�fd!d"�Zd#d$�Zd%d&�Zd'd(�Z�ZS)+�AsyncioSelectorReactorzA
    Reactor running on top of L{asyncio.SelectorEventLoop}.
    FNcs@|dkrt�}||_i|_i|_t�|_t|�|_t��	�dSr)
r�_asyncioEventloop�_writers�_readers�set�
_delayedCallsr	�_continuousPolling�superr)r�	eventloop��	__class__rrr;s
zAsyncioSelectorReactor.__init__cCs$z|jj�|�WnYnXdS)am	
        Compensate for a bug in asyncio where it will not unregister a FD that
        it cannot handle in the epoll loop. It touches internal asyncio code.

        A description of the bug by markrwilliams:

        The C{add_writer} method of asyncio event loops isn't atomic because
        all the Selector classes in the selector module internally record a
        file object before passing it to the platform's selector
        implementation. If the platform's selector decides the file object
        isn't acceptable, the resulting exception doesn't cause the Selector to
        un-track the file object.

        The failing/hanging stdio test goes through the following sequence of
        events (roughly):

        * The first C{connection.write(intToByte(value))} call hits the asyncio
        reactor's C{addWriter} method.

        * C{addWriter} calls the asyncio loop's C{add_writer} method, which
        happens to live on C{_BaseSelectorEventLoop}.

        * The asyncio loop's C{add_writer} method checks if the file object has
        been registered before via the selector's C{get_key} method.

        * It hasn't, so the KeyError block runs and calls the selector's
        register method

        * Code examples that follow use EpollSelector, but the code flow holds
        true for any other selector implementation. The selector's register
        method first calls through to the next register method in the MRO

        * That next method is always C{_BaseSelectorImpl.register} which
        creates a C{SelectorKey} instance for the file object, stores it under
        the file object's file descriptor, and then returns it.

        * Control returns to the concrete selector implementation, which asks
        the operating system to track the file descriptor using the right API.

        * The operating system refuses! An exception is raised that, in this
        case, the asyncio reactor handles by creating a C{_ContinuousPolling}
        object to watch the file descriptor.

        * The second C{connection.write(intToByte(value))} call hits the
        asyncio reactor's C{addWriter} method, which hits the C{add_writer}
        method. But the loop's selector's get_key method now returns a
        C{SelectorKey}! Now the asyncio reactor's C{addWriter} method thinks
        the asyncio loop will watch the file descriptor, even though it won't.
        N)rZ	_selectorZ
unregister)r�fdrrr�_unregisterFDInAsyncioHs2z-AsyncioSelectorReactor._unregisterFDInAsyncioc
Cs�|r
|jn|j}|��dkr.|�|t|�dSz
|�}Wn2tk
rj}z|}|j�d�W5d}~XYnX|r~|�|||�dS�N���)ZdoReadZdoWrite�filenoZ_disconnectSelectabler�	Exception�_logZfailure)rZ
selectable�read�methodZwhy�errr�_readOrWrite�s
z#AsyncioSelectorReactor._readOrWritec
Cs�||j��ks||jjkrdS|��}z&|j�|t||j|d�||j|<WnHtk
r�}z*|�	|�|j
t
jkr�|j�|�n�W5d}~XYnXdS�NT)
r�keysr"r+rZ
add_readerr
r1�IOErrorr(�errnoZEPERM�	addReader)r�readerr'r0rrrr6�s"
��
z AsyncioSelectorReactor.addReadercCs�||j��ks||jjkrdS|��}z&|j�|t||j|d�||j|<Wn\tk
rv|�	|�|j�
|�Yn4tk
r�|�	|�Yn|�	|��YnXdS)NF)rr3r"r+rZ
add_writerr
r1r
r(�	addWriterr�r�writerr'rrrr8�s(
��

z AsyncioSelectorReactor.addWritercCst||j��ks|j�|�sdS|j�|�r:|j�|�dS|��}|dkrX|j�|�}n|j�|�|j�|�dSr))	rr3r"Z	isReading�removeReaderr+�poprZ
remove_reader)rr7r'rrrr;�s
�z#AsyncioSelectorReactor.removeReadercCst||j��ks|j�|�sdS|j�|�r:|j�|�dS|��}|dkrX|j�|�}n|j�|�|j�|�dSr))	rr3r"Z	isWriting�removeWriterr+r<rZ
remove_writerr9rrrr=�s
�z#AsyncioSelectorReactor.removeWritercCs"|�|j��|j���|j��Sr)Z
_removeAllrr3rr"�	removeAllrrrrr>�s�z AsyncioSelectorReactor.removeAllcCst|j���|j��Sr)�listrr3r"�
getReadersrrrrr@�s�z!AsyncioSelectorReactor.getReaderscCst|j���|j��Sr)r?rr3r"�
getWritersrrrrrA�s�z!AsyncioSelectorReactor.getWriterscCs
t|j�Sr)r?r!rrrr�getDelayedCalls�sz&AsyncioSelectorReactor.getDelayedCallscCs$|j�|d|jj�|j��dS)Ng{�G�z�?)r�
call_later�stop�run_forever)rZtimeoutrrr�iterates�zAsyncioSelectorReactor.iterateTcCs&|j|d�|j��|jr"d|_dS)N)�installSignalHandlersF)ZstartRunningrrEZ_justStopped)rrGrrr�run	s
zAsyncioSelectorReactor.runcst���|�d|jd�dS)NrZshutdown)r#rD�	callLaterZfireSystemEventrr%rrrDs
zAsyncioSelectorReactor.stopcst���|j��dSr)r#�crashrrDrr%rrrJs
zAsyncioSelectorReactor.crashcCs
|j��Sr)r�timerrrr�secondsszAsyncioSelectorReactor.secondsc	sx�����fdd���j�|��}t|����fdd�}���fdd�}t���|�di||�jd���j����S)	Ncs d�_�j�������dSr2)Zcalledr!�remover)�args�dc�f�kwargsrrrrHsz-AsyncioSelectorReactor.callLater.<locals>.runcs�j�|����dSr)r!rMr�rO)�dchandlerrrr&sz0AsyncioSelectorReactor.callLater.<locals>.cancelcs�j�|j���_dSr)rZcall_atrKrrR)rSrHrrr�reset*sz/AsyncioSelectorReactor.callLater.<locals>.resetr)rL)rrCrrrLr!�add)rrLrPrNrQrrrTr)rNrOrSrPrQrHrrrIs�z AsyncioSelectorReactor.callLatercs"����fdd�}�j�|�dS)Ncs�jd�f����S)Nr)rIr�rNrPrQrrr�<lambda>4�z7AsyncioSelectorReactor.callFromThread.<locals>.<lambda>)rZcall_soon_threadsafe)rrPrNrQ�grrVr�callFromThread3sz%AsyncioSelectorReactor.callFromThread)N)T)rrrrZ_asyncClosedrr-rr(r1r6r8r;r=r>r@rArBrFrHrDrJrLrIrZ�
__classcell__rrr%rr3s*
8
rcCs t|�}ddlm}||�dS)z�
    Install an asyncio-based reactor.

    @param eventloop: The asyncio eventloop to wrap. If default, the global one
        is selected.
    r)�installReactorN)rZtwisted.internet.mainr\)r$Zreactorr\rrr�install9sr])N)rZ
__future__rrr5Zzope.interfacerZtwisted.loggerrZtwisted.internet.baserZtwisted.internet.posixbaserrr	Ztwisted.python.logr
Ztwisted.internet.interfacesrZasyncior�ImportError�builtinsr
r�objectrrr]rrrr�<module>s&