o
    bV                     @   s   d Z ddlZddlmZmZmZmZmZmZ	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZ eeG dd	 d	ejejZd
d Zd	dgZdS )z
A poll() based implementation of the twisted main loop.

To install the event loop (and you should do this before any connections,
listeners or connectors are added)::

    from twisted.internet import pollreactor
    pollreactor.install()
    N)POLLERRPOLLHUPPOLLINPOLLNVALPOLLOUTerrorpoll)implementer)	posixbase)IReactorFDSet)logc                   @   s   e Zd ZdZeeB eB ZeZ	e
Zdd Zdd Zdd Zdd	 Zd
d Zdd Zdd Zdd Zdd ZeZdd Zdd ZdS )PollReactora^  
    A reactor that uses poll(2).

    @ivar _poller: A L{select.poll} which will be used to check for I/O
        readiness.

    @ivar _selectables: A dictionary mapping integer file descriptors to
        instances of L{FileDescriptor} which have been registered with the
        reactor.  All L{FileDescriptor}s which are currently receiving read or
        write readiness notifications will be present as values in this
        dictionary.

    @ivar _reads: A dictionary mapping integer file descriptors to arbitrary
        values (this is essentially a set).  Keys in this dictionary will be
        registered with C{_poller} for read readiness notifications which will
        be dispatched to the corresponding L{FileDescriptor} instances in
        C{_selectables}.

    @ivar _writes: A dictionary mapping integer file descriptors to arbitrary
        values (this is essentially a set).  Keys in this dictionary will be
        registered with C{_poller} for write readiness notifications which will
        be dispatched to the corresponding L{FileDescriptor} instances in
        C{_selectables}.
    c                 C   s*   t  | _i | _i | _i | _tj|  dS )zo
        Initialize polling object, file descriptor tracking dictionaries, and
        the base class.
        N)r   _poller_selectables_reads_writesr
   PosixReactorBase__init__self r   >/usr/lib/python3/dist-packages/twisted/internet/pollreactor.pyr   C   s
   zPollReactor.__init__c                 C   s   z| j | W n	 ty   Y nw d}|| jv r|tB }|| jv r&|tB }|dkr3| j || dS || jv r>| j|= dS dS )z*Register/unregister an fd with the poller.r   N)	r   
unregisterKeyErrorr   r   r   r   registerr   )r   fdmaskr   r   r   _updateRegistrationN   s   


zPollReactor._updateRegistrationc                 C   sn   z
|  }||  W n ty&   | j D ]
\}}||u r  nqY d S Y nw ||v r5||= | | d S d S )N)filenoBaseExceptionr   itemsr   )r   
selectablemdictr   fdesr   r   r   _dictRemove`   s   
zPollReactor._dictRemovec                 C   8   |  }|| jvr|| j|< d| j|< | | dS dS )z@Add a FileDescriptor for notification of data available to read.   N)r   r   r   r   )r   readerr   r   r   r   	addReaderu      


zPollReactor.addReaderc                 C   r%   )zAAdd a FileDescriptor for notification of data available to write.r&   N)r   r   r   r   )r   writerr   r   r   r   	addWriter}   r)   zPollReactor.addWriterc                 C      |  || jS )z?Remove a Selectable for notification of data available to read.)r$   r   )r   r'   r   r   r   removeReader      zPollReactor.removeReaderc                 C   r,   )z@Remove a Selectable for notification of data available to write.)r$   r   )r   r*   r   r   r   removeWriter   r.   zPollReactor.removeWriterc                    s,      fdd jD  fdd jD S )zD
        Remove all selectables, and return a list of them.
        c                       g | ]} j | qS r   r   .0r   r   r   r   
<listcomp>       z)PollReactor.removeAll.<locals>.<listcomp>c                    r0   r   r1   r2   r   r   r   r4      r5   )
_removeAllr   r   r   r   r   r   	removeAll   s   zPollReactor.removeAllc              
   C   s   |dur
t |d }z| j|}W n ty. } z|jd tjkr)W Y d}~dS  d}~ww | j}|D ]\}}z| j| }W n	 t	yH   Y q4w t
||||| q4dS )zPoll the poller for new events.Ni  r   )intr   r   SelectErrorargserrnoEINTR_doReadOrWriter   r   r   callWithLogger)r   timeoutle_drdwr   eventr!   r   r   r   doPoll   s&   zPollReactor.doPollc                        fdd j D S )Nc                    r0   r   r1   r2   r   r   r   r4      r5   z*PollReactor.getReaders.<locals>.<listcomp>)r   r   r   r   r   
getReaders      zPollReactor.getReadersc                    rE   )Nc                    r0   r   r1   r2   r   r   r   r4      r5   z*PollReactor.getWriters.<locals>.<listcomp>)r   r   r   r   r   
getWriters   rG   zPollReactor.getWritersN)__name__
__module____qualname____doc__r   r   r   _POLL_DISCONNECTEDr   _POLL_INr   	_POLL_OUTr   r   r$   r(   r+   r-   r/   r7   rD   doIterationrF   rH   r   r   r   r   r   $   s"    	r   c                  C   s   t  } ddlm} ||  dS )zInstall the poll() reactor.r   )installReactorN)r   twisted.internet.mainrQ   )prQ   r   r   r   install   s   rT   )rL   r;   selectr   r   r   r   r   r   r9   r   zope.interfacer	   twisted.internetr
   twisted.internet.interfacesr   twisted.pythonr   r   _PollLikeMixinr   rT   __all__r   r   r   r   <module>   s   $
 