
    h	Z                    $   S r SSKJr  SS/rSSKrSSKJr  SSKJrJ	r	J
r
JrJrJrJrJrJrJrJr  SSKJrJrJrJrJrJrJrJrJrJrJr  SS	KJ r   SS
K!J"r"J#r#  SSK$J%r%  SSK&J'r'  Sr(Sr)Sr* " S S\'5      r+\" \ 5       " S S\#5      5       r,SS jr-g)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                  8   ^  \ rS rSrSrU 4S jrU 4S jrSrU =r$ )
_WakerPlus=   a  
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                .   > [         TU ]  5         Xl        g N)super__init__reactor)selfr+   	__class__s     a/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/internet/cfreactor.pyr*   _WakerPlus.__init__N   s        c                X   > [         TU ]  5       nU R                  R                  5         U$ )z_
Wake up the loop and force C{runUntilCurrent} to run immediately in the
next timed iteration.
)r)   doReadr+   _scheduleSimulate)r,   resultr-   s     r.   r2   _WakerPlus.doReadR   s&    
 !&&(r0   )r+   )	__name__
__module____qualname____firstlineno____doc__r*   r2   __static_attributes____classcell__r-   s   @r.   r%   r%   =   s      r0   r%   c                     ^  \ rS rSr% SrSS jrSS jrS rS rS r	S	 r
S
 rS rS rS rS rS rS rS rS S!U 4S jjjrSrS"S jrSrS\S'   S"S jrS"S jrS rS"S jrS rS#S jrSrU =r$ )$r   \   a  
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                    0 U l         0 U l        Uc  [        nX l        Uc
  [	        5       nXl        [        R                  " U 5        g r(   )_fdmap_idmapr   _runnerr   
_cfrunloopr   r*   )r,   runLooprunners      r.   r*   CFReactor.__init__   s?    >!F?)+G!!!$'r0   c                    [        U 5      $ r(   )r%   r,   s    r.   _wakerFactoryCFReactor._wakerFactory   s    $r0   c                   ^ ^^^ Uu  pgUT R                   ;  a  [        T R                  U[        5        gT R                   U   u  pmmUUUU 4S jn
[        R
                  " TU
5        g)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                   > S n Sn TR                  5       S:X  a  [        n OMT[        :H  nU(       a  T[           (       a  TR	                  5       n OT[
           (       a  TR                  5       n U (       a  TR                  TX5        g g ! [         a0    [        R                  " 5       S   n [        R                  " 5          NTf = f)NFr!   )filenor   r   _READr2   _WRITEdoWriteBaseExceptionsysexc_infor    err_disconnectSelectable)whyisReadcallbackTypereadWriteDescriptorrwr,   s     r.   _drdw(CFReactor._socketCallback.<locals>._drdw   s    CF&--/25&C)-BBF e9"5"<"<">Cf:"5"="="?C **+>L  ! llnQ'	s   B
 .B
 B
 
7CC)rA   r   rD   r   r    callWithLogger)r,   cfSocketrZ   ignoredAddressignoredDatacontextfd
smugglesrcsrcsktr]   r[   r\   s   ` `        @@r.   _socketCallbackCFReactor._socketCallback   sc    . #T[[  "$//:?TU,0KKO)%r	M 	M> 	.6r0   c                t   US:X  a  [        S5      eXR                  ;   a  U R                  U   u  pEpgO/ nUR                  U5        [        [        U[
        [        -  [        -  U R                  U5      n[        U[        [        -  [        -  5        [        [        US5      nUR                  U5        [        U R                  U[         5        [#        U[
        [        -  [        -  5        SS/nXR$                  ['        U5      '   XEX'4U R                  U'   SXpR)                  U5      '   [+        XS5        g)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}
rN   zInvalid file descriptor.r   FTN)RuntimeErrorrA   appendr   r   r   r   r   rh   r   r   r   _preserveSOErrorr   r   rD   r   r	   rB   id	_flag2idxr
   )	r,   rd   descrflagrf   cfsgotdescrr\   ctxs	            r.   _watchFDCFReactor._watchFD   s+    89::%)[[_"Ch CJJrN*#%()*+ $$C #:=> !! ..A3JCJJsOt5JK$%()*+ B%'KK5	"!1DKKO#'>>$ *r0   c                4    [         [        [        [        0U   $ )z
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}
)r   rP   r   rQ   )r,   rq   s     r.   ro   CFReactor._flag2idx  s     &u.DfMdSSr0   c                   [        U5      U R                  ;  a  gUS:X  a  U R                  [        U5         nOUnU R                  U   u  pVp'[        Xc5        SXpR	                  U5      '   U[
           (       dY  U[           (       dJ  U R                  [        U5      	 U R                  U	 [        U R                  U[        5        [        U5        ggg)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}
NrN   F)rn   rB   rA   r	   ro   rP   rQ   r   rD   r   r   )r,   rd   rp   rq   realfdrf   rr   r\   s           r.   
_unwatchFDCFReactor._unwatchFD  s     e9DKK'8[[E+FF"kk&1% +#(>>$ %yFBuI&F#!$//38MNs#	 ",yr0   c                N    U R                  UR                  5       U[        5        g)z'
Implement L{IReactorFDSet.addReader}.
N)ru   rO   r   r,   readers     r.   	addReaderCFReactor.addReader:  s     	fmmov/DEr0   c                N    U R                  UR                  5       U[        5        g)z'
Implement L{IReactorFDSet.addWriter}.
N)ru   rO   r   r,   writers     r.   	addWriterCFReactor.addWriter@  s     	fmmov/EFr0   c                N    U R                  UR                  5       U[        5        g)z*
Implement L{IReactorFDSet.removeReader}.
N)r{   rO   r   r~   s     r.   removeReaderCFReactor.removeReaderF  s     	1FGr0   c                N    U R                  UR                  5       U[        5        g)z*
Implement L{IReactorFDSet.removeWriter}.
N)r{   rO   r   r   s     r.   removeWriterCFReactor.removeWriterL  s     	1GHr0   c           
        U R                   R                  5        VVVVs1 s H  u  pp4UiM
     nnnnnU[        U R                  5      -  nU H%  nU R	                  U5        U R                  U5        M'     [        U5      $ s  snnnnf )z'
Implement L{IReactorFDSet.removeAll}.
)rA   valuesset_internalReadersr   r   list)r,   rf   rr   rp   r\   allDescdescs          r.   	removeAllCFReactor.removeAllR  sy     59KK4F4F4HI4H0Su54HI3t,,--Dd#d#  G} Js   B
c           
         U R                   R                  5        VVVVs/ s H  u  pp4U[           (       d  M  UPM     snnnn$ s  snnnnf )z(
Implement L{IReactorFDSet.getReaders}.
)rA   r   rP   r,   rf   rr   rp   r\   s        r.   
getReadersCFReactor.getReaders]  s6     261C1C1ES1E-#EE1ESSS
   A
A
c           
         U R                   R                  5        VVVVs/ s H  u  pp4U[           (       d  M  UPM     snnnn$ s  snnnnf )z(
Implement L{IReactorFDSet.getWriters}.
)rA   r   rQ   r   s        r.   
getWritersCFReactor.getWritersc  s6     261C1C1ET1E-#EF1ETTTr   c                R    [         R                  " X5      nU R                  5         U$ )aG  
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   _moveCallLaterSoonerr3   )r,   tpler4   s      r.   r   CFReactor._moveCallLaterSooneri  s%     "66tB r0   c                D   > [         TU ]  U5        U R                  5         g)zq
Start running the reactor, then kick off the timer that advances
Twisted's clock to keep pace with CFRunLoop's.
N)r)   startRunningr3   )r,   installSignalHandlersr-   s     r.   r   CFReactor.startRunningu  s!    
 	23 	 r0   Fc                  ^  T R                   (       d!  SU 4S jjnST l         T R                  SU5        Sn T R                   (       aK  U(       a  T R                  5         SnST l         T R	                  5         ST l        T R                   (       a  MK  T R                  5         g! ST l        f = f! T R                  5         f = f)zr
Run the runner (C{CFRunLoopRun} or something that calls it), which runs
the run loop until C{crash()} is called.
c                 &   > T R                  5         g r(   )crashrI   s   r.   docrash#CFReactor.mainLoop.<locals>.docrash  s    

r0   Tr   FNreturnNone)_started	callLaterr3   	_inCFLooprC   _stopSimulating)r,   r   alreadys   `  r.   mainLoopCFReactor.mainLoop  s    
 }} !DMNN1g&-	#--. **, !%+LLN%*DNU ---X   " &+DN  "s#   1B0 )B$ 9B0 $	B--B0 0Czobject | None_currentSimulatorc                X    U R                   c  g[        U R                   5        SU l         g)z^
If we have a CFRunLoopTimer registered with the CFRunLoop, invalidate
it and set it to None.
N)r   r   rI   s    r.   r   CFReactor._stopSimulating  s)    
 !!) !7!78!%r0   c           	     N  ^  T R                  5         T R                  (       d  gT R                  (       d  T R                  (       a  SOT R	                  5       nUc  g[        5       U-   nU 4S jn[        [        USSSUS5      =nT l        [        T R                  U[        5        g)zx
Schedule a call to C{self.runUntilCurrent}.  This will cancel the
currently scheduled call if it is already scheduled.
Ng        c                T   > S Tl         TR                  5         TR                  5         g r(   )r   runUntilCurrentr3   )cftimerextrar,   s     r.   simulate-CFReactor._scheduleSimulate.<locals>.simulate  s#    %)D"  """$r0   r   )r   r   _justStoppedthreadCallQueuetimeoutr   r   r   r   r   rD   r   )r,   r   fireDater   cs   `    r.   r3   CFReactor._scheduleSimulate  s    
 	}} 
 ++t/C/C#$,,.?+-7	%
 &:1aHd&
 	
D" 	$//1.CDr0   c                \    [         R                  " XU/UQ70 UD6nU R                  5         U$ )z&
Implement L{IReactorTime.callLater}.
)r   r   r3   )r,   _seconds_fargskwdelayedCalls         r.   r   CFReactor.callLater  s2     '00QdQbQ r0   c                P    [         R                  " U 5        U R                  5         g)z!
Implement L{IReactorCore.stop}.
N)r   stopr3   rI   s    r.   r   CFReactor.stop  s     	d# r0   c                ~    [         R                  " U 5        U R                  (       d  g[        U R                  5        g)z!
Implement L{IReactorCore.crash}
N)r   r   r   r   rD   rI   s    r.   r   CFReactor.crash  s(     	t$~~doo&r0   c                j    SU l         U R                  SU R                  5        U R                  5         g)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.
Tr   N)r   r   r   r   )r,   delays     r.   iterateCFReactor.iterate  s(      	q$**%r0   )rD   r   rA   rB   r   rC   r   NN)r   r%   )T)r   boolr   r   r   )r   ) r6   r7   r8   r9   r:   r*   rJ   rh   ru   ro   r{   r   r   r   r   r   r   r   r   r   r   r   r   __annotations__r   r3   r   r   r   r   r;   r<   r=   s   @r.   r   r   \   s    $L
( @7D9+v
T$<FGHI	TU
! !6 IG#R (,}+&E>!' r0   c                4    [        XS9nSSKJn  U" U5        U$ )a  
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}
)rE   rF   r   )installReactor)r   twisted.internet.mainr   )rE   rF   r+   r   s       r.   r   r   /  s    0 7G47Nr0   r   ).r:   
__future__r   __all__rT   zope.interfacer   	CFNetworkr   r   r	   r
   r   r   r   r   r   r   r   CoreFoundationr   r   r   r   r   r   r   r   r   r   r   twisted.internet.interfacesr   twisted.internet.posixbaser   r   twisted.pythonr    _signalsr"   rP   rQ   rm   r%   r   r    r0   r.   <module>r      s   
 #k
" 
 &       6 E 
 !		
  > ]O  O Odr0   