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

�

Ϫ�f[��"�dZddlmZddgZddlZddlmZddlmZm	Z	m
Z
mZmZm
Z
mZmZmZmZmZddlmZmZmZmZmZmZmZmZmZmZmZdd	lm Z dd
l!m"Z"m#Z#ddl$m%Z%dd
l&m'Z'dZ(dZ)dZ*Gd�de'�Z+ee �Gd�de#��Z,dd�Z-y)z�
A reactor for integrating with U{CFRunLoop<http://bit.ly/cfrunloop>}, the
CoreFoundation main loop used by macOS.

This is useful for integrating Twisted with U{PyObjC<http://pyobjc.sf.net/>}
applications.
�)�annotations�install�	CFReactorN)�implementer)�CFSocketCreateRunLoopSource�CFSocketCreateWithNative�CFSocketDisableCallBacks�CFSocketEnableCallBacks�CFSocketInvalidate�CFSocketSetSocketFlags�*kCFSocketAutomaticallyReenableReadCallBack�+kCFSocketAutomaticallyReenableWriteCallBack�kCFSocketConnectCallBack�kCFSocketReadCallBack�kCFSocketWriteCallBack)�CFAbsoluteTimeGetCurrent�CFRunLoopAddSource�CFRunLoopAddTimer�CFRunLoopGetCurrent�CFRunLoopRemoveSource�CFRunLoopRun�
CFRunLoopStop�CFRunLoopTimerCreate�CFRunLoopTimerInvalidate�kCFAllocatorDefault�kCFRunLoopCommonModes)�
IReactorFDSet)�_NO_FILEDESC�PosixReactorBase)�log�)�
_UnixWaker�@c�,��eZdZdZ�fd�Z�fd�Z�xZS)�
_WakerPlusa�
    The normal Twisted waker will simply wake up the main loop, which causes an
    iteration to run, which in turn causes L{ReactorBase.runUntilCurrent}
    to get invoked.

    L{CFReactor} has a slightly different model of iteration, though: rather
    than have each iteration process the thread queue, then timed calls, then
    file descriptors, each callback is run as it is dispatched by the CFRunLoop
    observer which triggered it.

    So this waker needs to not only unblock the loop, but also make sure the
    work gets done; so, it reschedules the invocation of C{runUntilCurrent} to
    be immediate (0 seconds from now) even if there is no timed call work to
    do.
    c�0��t�|��||_y�N)�super�__init__�reactor)�selfr*�	__class__s  ��</usr/lib/python3/dist-packages/twisted/internet/cfreactor.pyr)z_WakerPlus.__init__Ns���
�������c�Z��t�|��}|jjd�|S)zw
        Wake up the loop and force C{runUntilCurrent} to run immediately in the
        next timed iteration.
        T)r(�doReadr*�_scheduleSimulate)r+�resultr,s  �r-r0z_WakerPlus.doReadRs(���
���!�����&�&�t�,��
r.)�__name__�
__module__�__qualname__�__doc__r)r0�
__classcell__�r,s@r-r%r%=s���� ��r.r%c����eZdZUdZdd�Zdd�Zd�Zd�Zd�Zd�Z	d	�Z
d
�Zd�Zd�Z
d
�Zd�Zd�Zd�Zdd�fd�
ZdZd d�ZdZded<d d�Zd!d"d�Zd�Zd�Zd�Zd#d�Z�xZS)$ra&
    The CoreFoundation reactor.

    You probably want to use this via the L{install} API.

    @ivar _fdmap: a dictionary, mapping an integer (a file descriptor) to a
        4-tuple of:

            - source: a C{CFRunLoopSource}; the source associated with this
              socket.
            - socket: a C{CFSocket} wrapping the file descriptor.
            - descriptor: an L{IReadDescriptor} and/or L{IWriteDescriptor}
              provider.
            - read-write: a 2-C{list} of booleans: respectively, whether this
              descriptor is currently registered for reading or registered for
              writing.

    @ivar _idmap: a dictionary, mapping the id() of an L{IReadDescriptor} or
        L{IWriteDescriptor} to a C{fd} in L{_fdmap}.  Implemented in this
        manner so that we don't have to rely (even more) on the hashability of
        L{IReadDescriptor} providers, and we know that they won't be collected
        since these are kept in sync with C{_fdmap}.  Necessary because the
        .fileno() of a file descriptor may change at will, so we need to be
        able to look up what its file descriptor I{used} to be, so that we can
        look it up in C{_fdmap}

    @ivar _cfrunloop: the C{CFRunLoop} pyobjc object wrapped
        by this reactor.

    @ivar _inCFLoop: Is C{CFRunLoopRun} currently running?

    @type _inCFLoop: L{bool}

    @ivar _currentSimulator: if a CFTimer is currently scheduled with the CF
        run loop to run Twisted callLater calls, this is a reference to it.
        Otherwise, it is L{None}
    Nc��i|_i|_|�t}||_|�
t	�}||_t
j|�yr')�_fdmap�_idmapr�_runnerr�
_cfrunlooprr))r+�runLoop�runners   r-r)zCFReactor.__init__�sC���������>�!�F�����?�)�+�G�!����!�!�$�'r.c��t|�Sr')r%�r+s r-�
_wakerFactoryzCFReactor._wakerFactory�s
���$��r.c�������|\}}|�jvrt�j|t�y�j|\}}	������fd�}
t	j
�|
�y)a�
        The socket callback issued by CFRunLoop.  This will issue C{doRead} or
        C{doWrite} calls to the L{IReadDescriptor} and L{IWriteDescriptor}
        registered with the file descriptor that we are being notified of.

        @param cfSocket: The C{CFSocket} which has got some activity.

        @param callbackType: The type of activity that we are being notified
            of.  Either C{kCFSocketReadCallBack} or C{kCFSocketWriteCallBack}.

        @param ignoredAddress: Unused, because this is not used for either of
            the callback types we register for.

        @param ignoredData: Unused, because this is not used for either of the
            callback types we register for.

        @param context: The data associated with this callback by
            C{CFSocketCreateWithNative} (in C{CFReactor._watchFD}).  A 2-tuple
            of C{(int, CFRunLoopSource)}.
        Nc�`��d}d}	�j�dk(rt}n>�tk(}|r�tr*�j	�}n�t
r�j
�}|r�j�||�yy#t$r.tj�d}tj�Y�MwxYw)NF���r!)
�filenorr�_READr0�_WRITE�doWrite�
BaseException�sys�exc_infor �err�_disconnectSelectable)�why�isRead�callbackType�readWriteDescriptor�rwr+s  ����r-�_drdwz(CFReactor._socketCallback.<locals>._drdw�s�����C��F�
�&�-�-�/�2�5�&�C�)�-B�B�F���e�9�"5�"<�"<�">�C��f�:�"5�"=�"=�"?�C���*�*�+>��V�L���!�
��l�l�n�Q�'�����	�
�s�AA6�64B-�,B-)r;rr>rr �callWithLogger)
r+�cfSocketrR�ignoredAddress�ignoredData�context�fd�
smugglesrc�src�sktrUrSrTs
` `        @@r-�_socketCallbackzCFReactor._socketCallback�sa���.#���Z�
�T�[�[� �
"�$�/�/�:�?T�U��,0�K�K��O�)��S�%�r�	M�>	���.��6r.c�~�|dk(rtd��||jvr|j|\}}}}n�g}|j|�tt|t
tztz|j|�}t|ttztz�tt|d�}|j|�t|j|t �t#|t
tztz�ddg}||j$t'|�<||||f|j|<d||j)|�<t+||�y)a�
        Register a file descriptor with the C{CFRunLoop}, or modify its state
        so that it's listening for both notifications (read and write) rather
        than just one; used to implement C{addReader} and C{addWriter}.

        @param fd: The file descriptor.

        @type fd: L{int}

        @param descr: the L{IReadDescriptor} or L{IWriteDescriptor}

        @param flag: the flag to register for callbacks on, either
            C{kCFSocketReadCallBack} or C{kCFSocketWriteCallBack}
        rFzInvalid file descriptor.rFTN)�RuntimeErrorr;�appendrrrrrr_rr
r�_preserveSOErrorrrr>rr	r<�id�	_flag2idxr
)	r+r[�descr�flagr]�cfs�gotdescrrT�ctxs	         r-�_watchFDzCFReactor._watchFD�s9����8��9�:�:�
�����%)�[�[��_�"�C��h���C��J�J�r�N�*�#��%�(�)�*�+��$�$���C�
#��:�=�>�!�!�
�.�.A�3��J�C��J�J�s�O��t����5J�K�$��%�(�)�*�+�
����B�%'�D�K�K��5�	�"�!�3��r�1�D�K�K��O�#'��4�>�>�$�� ���T�*r.c�4�tttti|S)a-
        Convert a C{kCFSocket...} constant to an index into the read/write
        state list (C{_READ} or C{_WRITE}) (the 4th element of the value of
        C{self._fdmap}).

        @param flag: C{kCFSocketReadCallBack} or C{kCFSocketWriteCallBack}

        @return: C{_READ} or C{_WRITE}
        )rrHrrI)r+rgs  r-rezCFReactor._flag2idxs��&�u�.D�f�M�d�S�Sr.c��t|�|jvry|dk(r|jt|�}n|}|j|\}}}}t||�d||j	|�<|t
sT|tsJ|jt|�=|j|=t|j|t�t|�yyy)a�
        Unregister a file descriptor with the C{CFRunLoop}, or modify its state
        so that it's listening for only one notification (read or write) as
        opposed to both; used to implement C{removeReader} and C{removeWriter}.

        @param fd: a file descriptor

        @type fd: C{int}

        @param descr: an L{IReadDescriptor} or L{IWriteDescriptor}

        @param flag: C{kCFSocketWriteCallBack} C{kCFSocketReadCallBack}
        NrFF)rdr<r;r	rerHrIrr>rr)r+r[rfrg�realfdr]rhrTs        r-�
_unwatchFDzCFReactor._unwatchFDs����e�9�D�K�K�'��
��8��[�[��E��+�F��F�"�k�k�&�1���S�%�� ��d�+�#(��4�>�>�$�� ��%�y��F�����B�u�I�&����F�#�!�$�/�/�3�8M�N��s�#�	",�yr.c�N�|j|j�|t�y)z7
        Implement L{IReactorFDSet.addReader}.
        N)rkrGr�r+�readers  r-�	addReaderzCFReactor.addReader:s��	
�
�
�f�m�m�o�v�/D�Er.c�N�|j|j�|t�y)z7
        Implement L{IReactorFDSet.addWriter}.
        N)rkrGr�r+�writers  r-�	addWriterzCFReactor.addWriter@s��	
�
�
�f�m�m�o�v�/E�Fr.c�N�|j|j�|t�y)z:
        Implement L{IReactorFDSet.removeReader}.
        N)rorGrrqs  r-�removeReaderzCFReactor.removeReaderFs��	
����
�
���1F�Gr.c�N�|j|j�|t�y)z:
        Implement L{IReactorFDSet.removeWriter}.
        N)rorGrrus  r-�removeWriterzCFReactor.removeWriterLs��	
����
�
���1G�Hr.c
��|jj�D����chc]	\}}}}|��}}}}}|t|j�z}|D]$}|j	|�|j|��&t
|�Scc}}}}w)z7
        Implement L{IReactorFDSet.removeAll}.
        )r;�values�set�_internalReadersryr{�list)r+r]rhrfrT�allDesc�descs       r-�	removeAllzCFReactor.removeAllRs���59�K�K�4F�4F�4H�I�I�0�S�#�u�b�5�I��I��3�t�,�,�-�-���	$�D����d�#����d�#�	$��G�}���Js�A?
c
��|jj�D����cgc]\}}}}|ts�|��c}}}}Scc}}}}w)z8
        Implement L{IReactorFDSet.getReaders}.
        )r;r}rH�r+r]rhrfrTs     r-�
getReaderszCFReactor.getReaders]s7��26���1C�1C�1E�S�S�-�#�s�E�2��E���S�S��S��>
�>
c
��|jj�D����cgc]\}}}}|ts�|��c}}}}Scc}}}}w)z8
        Implement L{IReactorFDSet.getWriters}.
        )r;r}rIr�s     r-�
getWriterszCFReactor.getWriterscs7��26���1C�1C�1E�T�T�-�#�s�E�2��F���T�T��Tr�c�R�tj||�}|j�|S)aw
        Override L{PosixReactorBase}'s implementation of L{IDelayedCall.reset}
        so that it will immediately reschedule.  Normally
        C{_moveCallLaterSooner} depends on the fact that C{runUntilCurrent} is
        always run before the mainloop goes back to sleep, so this forces it to
        immediately recompute how long the loop needs to stay asleep.
        )r�_moveCallLaterSoonerr1)r+�tpler2s   r-r�zCFReactor._moveCallLaterSooneris'��"�6�6�t�T�B����� ��
r.c�H��t�|�|�|jd��y)z�
        Start running the reactor, then kick off the timer that advances
        Twisted's clock to keep pace with CFRunLoop's.
        T)�forceN)r(�startRunningr1)r+�installSignalHandlersr,s  �r-r�zCFReactor.startRunningus&���
	���2�3�	
���T��*r.Fc�\���jsd�fd�}d�_�jd|�d}	�jr@|r�j�d}d�_	�j	�d�_�jr�@�j�y#d�_wxYw#�j�wxYw)z�
        Run the runner (C{CFRunLoopRun} or something that calls it), which runs
        the run loop until C{crash()} is called.
        c�&���j�yr')�crashrBs�r-�docrashz#CFReactor.mainLoop.<locals>.docrash�s����
�
�r.TrFN��return�None)�_started�	callLaterr1�	_inCFLoopr=�_stopSimulating)r+r��alreadys`  r-�mainLoopzCFReactor.mainLoop�s����
�}�}�
�!�D�M��N�N�1�g�&���-	#��-�-��.�*�*�,���!%���+��L�L�N�%*�D�N�U�-�-�X
� � �"��&+�D�N��� � �"�s#�'B�B
�(B�
	B�B�B+z
object | None�_currentSimulatorc�V�|j�yt|j�d|_y)zv
        If we have a CFRunLoopTimer registered with the CFRunLoop, invalidate
        it and set it to None.
        N)r�rrBs r-r�zCFReactor._stopSimulating�s)��
�!�!�)�� ��!7�!7�8�!%��r.c	����j��jsy|rdn�j�}|�yt�|z}�fd�}t	t
|ddd|d�x}�_t�j|t�y)aV
        Schedule a call to C{self.runUntilCurrent}.  This will cancel the
        currently scheduled call if it is already scheduled.

        @param force: Even if there are no timed calls, make sure that
            C{runUntilCurrent} runs immediately (in a 0-seconds-from-now
            C{CFRunLoopTimer}).  This is necessary for calls which need to
            trigger behavior of C{runUntilCurrent} other than running timed
            calls, such as draining the thread call queue or calling C{crash()}
            when the appropriate flags are set.

        @type force: C{bool}
        Ngc�T��d�_�j��j�yr')r��runUntilCurrentr1)�cftimer�extrar+s  �r-�simulatez-CFReactor._scheduleSimulate.<locals>.simulates#���%)�D�"�� � �"��"�"�$r.r)
r�r��timeoutrrrr�rr>r)r+r�r��fireDater��cs`     r-r1zCFReactor._scheduleSimulate�s~���	
�����}�}�
��#�D�L�L�N���?��+�-��7��	%�
&:���1�a��H�d�&
�	
��D�"�	�$�/�/�1�.C�Dr.c�\�tj|||g|��i|��}|j�|S)z6
        Implement L{IReactorTime.callLater}.
        )rr�r1)r+�_seconds�_f�args�kw�delayedCalls      r-r�zCFReactor.callLaters4��'�0�0��x��Q�d�Q�b�Q����� ��r.c�P�tj|�|jd�y)z1
        Implement L{IReactorCore.stop}.
        TN)r�stopr1rBs r-r�zCFReactor.stops ��	���d�#����t�$r.c�r�tj|�|jsyt|j�y)z1
        Implement L{IReactorCore.crash}
        N)rr�r�rr>rBs r-r�zCFReactor.crashs(��	���t�$��~�~���d�o�o�&r.c�j�d|_|jd|j�|j�y)z�
        Emulate the behavior of C{iterate()} for things that want to call it,
        by letting the loop run for a little while and then scheduling a timed
        call to exit it.
        TrN)r�r�r�r�)r+�delays  r-�iteratezCFReactor.iterate$s(����
�	
���q�$�*�*�%��
�
�r.�NN)r�r%)T)r��boolr�r�r�)F)r�r�r�r�)r)r3r4r5r6r)rCr_rkrerorsrwryr{r�r�r�r�r�r�r�r��__annotations__r�r1r�r�r�r�r7r8s@r-rr\s����$�L
(� �@7�D9+�v
T�$�<F�G�H�I�	�T�U�
�+�6�I�G#�R(,��}�+�&�#E�J�%�'�r.c�<�t||��}ddlm}||�|S)a6
    Configure the twisted mainloop to be run inside CFRunLoop.

    @param runLoop: the run loop to use.

    @param runner: the function to call in order to actually invoke the main
        loop.  This will default to C{CFRunLoopRun} if not specified.  However,
        this is not an appropriate choice for GUI applications, as you need to
        run NSApplicationMain (or something like it).  For example, to run the
        Twisted mainloop in a PyObjC application, your C{main.py} should look
        something like this::

            from PyObjCTools import AppHelper
            from twisted.internet.cfreactor import install
            install(runner=AppHelper.runEventLoop)
            # initialize your application
            reactor.run()

    @return: The installed reactor.

    @rtype: C{CFReactor}
    )r?r@r)�installReactor)r�twisted.internet.mainr�)r?r@r*r�s    r-rr5s ��0���7�G�4��7���Nr.r�).r6�
__future__r�__all__rL�zope.interfacer�	CFNetworkrrr	r
rrr
rrrr�CoreFoundationrrrrrrrrrrr�twisted.internet.interfacesr�twisted.internet.posixbaserr�twisted.pythonr �_signalsr"rHrIrcr%rr�r.r-�<module>r�s���
�#��k�
"��
�&���������6�E��
!�	��	
�������>
�]��U� �U��U�pr.

Zerion Mini Shell 1.0