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   $
 