%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__/win32eventreactor.cpython-312.pyc

�

Ϫ�fa;���dZddlZddlZddlmZddlmZddlmZddl	m
Z
mZmZm
Z
mZ	ddl	mZddlZddlmZmZmZmZmZddlmZdd
lmZmZddl m!Z!ddl"m#Z#m$Z$m%Z%eee�Gd�dejL��Z'Gd�d�Z(ee�Gd�d��Z)d�Z*ddgZ+y#e$rddlZej$de�	�d
�ZY��wxYw)a�
A win32event based implementation of the Twisted main loop.

This requires pywin32 (formerly win32all) or ActivePython to be installed.

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

    from twisted.internet import win32eventreactor
    win32eventreactor.install()

LIMITATIONS:
 1. WaitForMultipleObjects and thus the event loop can only handle 64 objects.
 2. Process running has some problems (see L{twisted.internet.process} docstring).


TODO:
 1. Event loop handling of writes is *very* problematic (this is causing failed tests).
    Switch to doing it the correct way, whatever that means (see below).
 2. Replace icky socket loopback waker with event based waker (use dummyEvent object)
 3. Switch everyone to using Free Software so we don't have to deal with proprietary APIs.


ALTERNATIVE SOLUTIONS:
 - IIRC, sockets can only be registered once. So we switch to a structure
   like the poll() reactor, thus allowing us to deal with write events in
   a decent fashion. This should allow us to pass tests, but we're still
   limited to 64 events.

Or:

 - Instead of doing a reactor, we make this an addon to the select reactor.
   The WFMO event loop runs in a separate thread. This means no need to maintain
   separate code for networking, 64 event limit doesn't apply to sockets,
   we can run processes and other win32 stuff in default event loop. The
   only problem is that we're stuck with the icky socket based waker.
   Another benefit is that this could be extended to support >64 events
   in a simpler manner than the previous solution.

The 2nd solution is probably what will get implemented.
�N)�Thread)�WeakKeyDictionary)�implementer)�	FD_ACCEPT�FD_CLOSE�
FD_CONNECT�FD_READ�WSAEventSelect)�WSAEnumNetworkEventszAReliable disconnection notification requires pywin32 215 or later)�categoryc��thS�N)r	)�fd�events  �D/usr/lib/python3/dist-packages/twisted/internet/win32eventreactor.pyrrFs
���y��)�QS_ALLINPUT�
WAIT_OBJECT_0�WAIT_TIMEOUT�CreateEvent�MsgWaitForMultipleObjects)�	posixbase)�
IReactorFDSet�IReactorWin32Events)�blockingCallFromThread)�failure�log�
threadablec�~�eZdZdZedddd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
d�Zd
�Zd�Zd�Zd�Zd�ZeZy)�Win32Reactora�
    Reactor that uses Win32 event APIs.

    @ivar _reads: A dictionary mapping L{FileDescriptor} instances to a
        win32 event object used to check for read events for that descriptor.

    @ivar _writes: A dictionary mapping L{FileDescriptor} instances to a
        arbitrary value.  Keys in this dictionary will be given a chance to
        write out their data.

    @ivar _events: A dictionary mapping win32 event object to tuples of
        L{FileDescriptor} instances and event masks.

    @ivar _closedAndReading: Along with C{_closedAndNotReading}, keeps track of
        descriptors which have had close notification delivered from the OS but
        which we have not finished reading data from.  MsgWaitForMultipleObjects
        will only deliver close notification to us once, so we remember it in
        these two dictionaries until we're ready to act on it.  The OS has
        delivered close notification for each descriptor in this dictionary, and
        the descriptors are marked as allowed to handle read events in the
        reactor, so they can be processed.  When a descriptor is marked as not
        allowed to handle read events in the reactor (ie, it is passed to
        L{IReactorFDSet.removeReader}), it is moved out of this dictionary and
        into C{_closedAndNotReading}.  The descriptors are keys in this
        dictionary.  The values are arbitrary.
    @type _closedAndReading: C{dict}

    @ivar _closedAndNotReading: These descriptors have had close notification
        delivered from the OS, but are not marked as allowed to handle read
        events in the reactor.  They are saved here to record their closed
        state, but not processed at all.  When one of these descriptors is
        passed to L{IReactorFDSet.addReader}, it is moved out of this dictionary
        and into C{_closedAndReading}.  The descriptors are keys in this
        dictionary.  The values are arbitrary.  This is a weak key dictionary so
        that if an application tells the reactor to stop reading from a
        descriptor and then forgets about that descriptor itself, the reactor
        will also forget about it.
    @type _closedAndNotReading: C{WeakKeyDictionary}
    Nrc��i|_i|_i|_i|_t	�|_tjj|�yr)	�_reads�_writes�_events�_closedAndReadingr�_closedAndNotReadingr�PosixReactorBase�__init__��selfs rr(zWin32Reactor.__init__�s>�����������!#���$5�$7��!��"�"�+�+�D�1rc�^�tdddd�}t|||�||f|j|<|S)z9
        Make a win32 event object for a socket.
        Nr)rr
r$)r*r�action�whyrs     r�_makeSocketEventzWin32Reactor._makeSocketEvent�s7���D�!�Q��-���r�5�#�&�!�6�l����U���rc�&�||f|j|<y)z:
        Add a new win32 event to the event loop.
        N�r$�r*rrr,s    r�addEventzWin32Reactor.addEvent�s�� "�6�l����U�rc��|j|=y)z"
        Remove an event.
        Nr0�r*rs  r�removeEventzWin32Reactor.removeEvent�s��
�L�L��rc���||jvre|j|dttztzt
z�|j|<||jvrd|j|<|j|=yyy)zY
        Add a socket FileDescriptor for notification of data available to read.
        �doReadTN)r"r.r	rrrr&r%�r*�readers  r�	addReaderzWin32Reactor.addReader�sw������$�"&�"7�"7���'�I�"5�
�"B�X�"M�#�D�K�K���
��2�2�2�15��&�&�v�.��-�-�f�5�3�
%rc�@�||jvrd|j|<yy)zZ
        Add a socket FileDescriptor for notification of data available to write.
        �N�r#�r*�writers  r�	addWriterzWin32Reactor.addWriter�s#������%�#$�D�L�L�� �&rc���||jvrS|j|j|=|j|=||jvrd|j|<|j|=yyy)z?Remove a Selectable for notification of data available to read.TN)r"r$r%r&r8s  r�removeReaderzWin32Reactor.removeReader�sg���T�[�[� ����T�[�[��0�1����F�#���/�/�/�48��)�)�&�1��*�*�6�2�0�
!rc�<�||jvr|j|=yy)z@Remove a Selectable for notification of data available to write.Nr=r>s  r�removeWriterzWin32Reactor.removeWriter�s���T�\�\�!����V�$�"rc�N�|j|j|j�S)zD
        Remove all selectables, and return a list of them.
        )�
_removeAllr"r#r)s r�	removeAllzWin32Reactor.removeAll�s�����t�{�{�D�L�L�9�9rc�H�t|jj��Sr)�listr"�keysr)s r�
getReaderszWin32Reactor.getReaders�s���D�K�K�$�$�&�'�'rc�H�t|jj��Sr)rIr#rJr)s r�
getWriterszWin32Reactor.getWriters�s���D�L�L�%�%�'�(�(rc��tjdd|��|�d}d}t|jj	��D]}d}|jd|��t|jj	��D]%}d}tj||j|��'|rd}|js"|jstj|�yt|jj	��xs
|jg}t|d	z�}t|d|t�}|t k(ry|t"t%|�zk(r4t'j(�}|r|j+d|j,�yy|t"k\r�|t"t%|�zkr�||t"z
}|j|\}}	||j.vrZ|j1�}
|
d
k(r"|j3|t4j6d�yt9|
|�}t:|vrd|j|<tj||j
|	|�yyy)N�system�	iteration)�channelr�reactor�dFTr7ri����)r�msgrIr%rJ�
_runActionr#�callWithLogger�	_runWriter$�time�sleep�
dummyEvent�intrrrr�len�win32gui�PumpWaitingMessages�	callLater�stopr"�fileno�_disconnectSelectabler�_NO_FILEDESCrr)r*�timeout�ranUserCoder9r�handles�val�exitrr,rb�eventss            r�doWaitForMultipleEventsz$Win32Reactor.doWaitForMultipleEvents�s�������T�B��?��G����4�1�1�6�6�8�9�	.�F��K��O�O�H�f�-�	.��t�|�|�(�(�*�+�	7�B��K����r�4�>�>�2�6�	7��
�G��������J�J�w����t�|�|�(�(�*�+�@����/@���g��n�%��'���G�[�I���,���
�M�C��L�0�
0��/�/�1�D�����q�$�)�)�,����M�
!�c�M�C��L�,H�&H��C�-�/�0�E����e�,�J�B���T�[�[� �������R�<��.�.�r�9�3I�3I�5�Q��.�f�e�<���v�%�15�D�*�*�2�.����r�4�?�?�F�B�?�''I�
!rc�~�d}	|j�}|rH|j
|�|j|�	|jtj|��y|�yy#t$r.tj�d}t	j
�Y��wxYw#t$rt	j
�YywxYw)Nrr<)�doWrite�
BaseException�sys�exc_infor�deferrrBrD�connectionLostr�Failure)r*r�closeds   rrXzWin32Reactor._runWrites�����	��Z�Z�\�F�
����b�!����b�!�
��!�!�'�/�/�&�"9�:��^�����	��\�\�^�A�&�F��J�J�L�	��!�
��
�
��
�s"�A"�$B�"4B�B�B<�;B<c���	t||��}|r|j
|||dk(�yy#t$r.tj�d}t	j
�Y�PwxYw)Nr<r7)�getattrrnrorprrqrc)r*r,rrts    rrVzWin32Reactor._runAction'sc��	�(�W�R��(�*�F���&�&�r�6�6�X�3E�F����	��\�\�^�A�&�F��J�J�L�	�s�-�4A$�#A$)�__name__�
__module__�__qualname__�__doc__rr[r(r.r2r5r:r@rBrDrGrKrMrkrXrV�doIteration�rrr r Zsj��&�P�T�1�a��.�J�2��+� �6�%�
3�%�
:�(�)�>@�@�$G�*�Krr c�(�eZdZdZd�Zd�Zd�Zd�Zy)�_ThreadFDWrappera7
    This wraps an event handler and translates notification in the helper
    L{Win32Reactor} thread into a notification in the primary reactor thread.

    @ivar _reactor: The primary reactor, the one to which event notification
        will be sent.

    @ivar _fd: The L{FileDescriptor} to which the event will be dispatched.

    @ivar _action: A C{str} giving the method of C{_fd} which handles the event.

    @ivar _logPrefix: The pre-fetched log prefix string for C{_fd}, so that
        C{_fd.logPrefix} does not need to be called in a non-main thread.
    c�<�||_||_||_||_yr)�_reactor�_fd�_action�
_logPrefix)r*rRrr,�	logPrefixs     rr(z_ThreadFDWrapper.__init__Cs����
�������#��rc��|jS)zc
        Return the original handler's log prefix, as it was given to
        C{__init__}.
        )r�r)s rr�z_ThreadFDWrapper.logPrefixIs��
���rc�6��t�j�fd��S)a-
        Callback fired when the associated event is set.  Run the C{action}
        callback on the wrapped descriptor in the main reactor thread and raise
        or return whatever it raises or returns to cause this event handler to
        be removed from C{self._reactor} if appropriate.
        c�N��t�j�j��Sr)rvr�r�r)s�r�<lambda>z+_ThreadFDWrapper._execute.<locals>.<lambda>Xs���#B�7�4�8�8�T�\�\�#B�#D�r)rr�r)s`r�_executez_ThreadFDWrapper._executePs���&��M�M�D�
�	
rc�d�|jj|jj|�y)z�
        Pass through to the wrapped descriptor, but in the main reactor thread
        instead of the helper C{Win32Reactor} thread.
        N)r��callFromThreadr�rr)r*�reasons  rrrz_ThreadFDWrapper.connectionLost[s"��
	
�
�
�$�$�T�X�X�%<�%<�f�ErN)rwrxryrzr(r�r�rrr|rrr~r~3s��
�$��	
�Frr~c�0�eZdZdZdZdZd�Zd�Zd�Zd�Z	y)�_ThreadedWin32EventsMixina�
    This mixin implements L{IReactorWin32Events} for another reactor by running
    a L{Win32Reactor} in a separate thread and dispatching work to it.

    @ivar _reactor: The L{Win32Reactor} running in the other thread.  This is
        L{None} until it is actually needed.

    @ivar _reactorThread: The L{threading.Thread} which is running the
        L{Win32Reactor}.  This is L{None} until it is actually needed.
    Nc�p�|jj|jj�d|_y)zP
        Stop and discard the reactor started by C{_makeHelperReactor}.
        N)r�r�rar)s r�_unmakeHelperReactorz._ThreadedWin32EventsMixin._unmakeHelperReactorss'��	
�
�
�$�$�T�]�]�%7�%7�8���
rc��t�|_d|j_t|jjd��|_|j
dd|j�|j
j�y)z�
        Create and (in a new thread) start a L{Win32Reactor} instance to use for
        the implementation of L{IReactorWin32Events}.
        F)F)�target�args�after�shutdownN)	r r��_registerAsIOThreadr�run�_reactorThread�addSystemEventTriggerr��startr)s r�_makeHelperReactorz,_ThreadedWin32EventsMixin._makeHelperReactorzs^��
%���
�-2��
�
�)�$�D�M�M�,=�,=�H�M����"�"�7�J��8Q�8Q�R����!�!�#rc���|j�|j�|jj|jj|t	||||j��d�y)�.
        @see: L{IReactorWin32Events}
        Nr�)r�r�r�r2r~r�r1s    rr2z"_ThreadedWin32EventsMixin.addEvent�sS���=�=� ��#�#�%��
�
�$�$��M�M�"�"���T�2�v�r�|�|�~�>��		
rc�d�|jj|jj|�y)r�N)r�r�r5r4s  rr5z%_ThreadedWin32EventsMixin.removeEvent�s"��	
�
�
�$�$�T�]�]�%>�%>��Fr)
rwrxryrzr�r�r�r�r2r5r|rrr�r�cs(��	��H��N��$�
�Grr�c�p�tjd�t�}ddlm}|j|�y)Nr<)�main)r�initr �r��installReactor)�rr�s  r�installr��s&���O�O�A����A������rr�),rzrorY�	threadingr�weakrefr�zope.interfacer�	win32filerrrr	r
r�ImportError�warnings�warn�UserWarningr^�
win32eventrrrrr�twisted.internetr�twisted.internet.interfacesrr�twisted.internet.threadsr�twisted.pythonrrrr'r r~r�r��__all__r|rr�<module>r�s���
(�T���%�&�O�N��.����'�J�;�3�3�
�]�/�0�U*�9�-�-�U*�1�U*�p-F�-F�`
�
 �!�4G�4G�"�4G�n��9�
%���K�	���H�M�M�K���
�	�s�B*�* C
�C


Zerion Mini Shell 1.0