
    hB*                     t   S r SSKrSSKrSSKJrJrJrJr  SSKJ	r	J
r
  SSKJrJr  SSKJrJr   \" \S5      r\" \S5      r\" \S	5      r\" \S
5      r\" \S5      r " S S\5      r\R6                  " \\5        \" \\5       " S S\
R8                  5      5       rS rSS/rg! \ a  r\" \5      eSrCff = f)am  
A kqueue()/kevent() based implementation of the Twisted main loop.

To use this reactor, start your application specifying the kqueue reactor::

   twistd --reactor kqueue ...

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

   from twisted.internet import kqreactor
   kqreactor.install()
    N)	Attribute	Interfacedeclarationsimplementer)main	posixbase)IReactorDaemonizeIReactorFDSet)failurelog	KQ_EV_ADDKQ_EV_DELETE	KQ_EV_EOFKQ_FILTER_READKQ_FILTER_WRITEc                   8    \ rS rSrSr\" S5      r\" S5      rSrg)_IKQueue*   z*
An interface for KQueue implementations.
zAn implementation of kqueue(2).zAn implementation of kevent(2). N)	__name__
__module____qualname____firstlineno____doc__r   kqueuekevent__static_attributes__r       a/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/internet/kqreactor.pyr   r   *   s     89F89Fr   r   c                   p    \ rS rSrSr\4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\rSrg)KQueueReactor6   a%  
A reactor that uses kqueue(2)/kevent(2) and relies on Python 2.6 or higher
which has built in support for kqueue in the select module.

@ivar _kq: A C{kqueue} which will be used to check for I/O readiness.

@ivar _impl: The implementation of L{_IKQueue} to use.

@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 set containing integer file descriptors.  Values in this
    set will be registered with C{_kq} for read readiness notifications
    which will be dispatched to the corresponding L{FileDescriptor}
    instances in C{_selectables}.

@ivar _writes: A set containing integer file descriptors.  Values in this
    set will be registered with C{_kq} for write readiness notifications
    which will be dispatched to the corresponding L{FileDescriptor}
    instances in C{_selectables}.
c                     Xl         U R                   R                  5       U l        [        5       U l        [        5       U l        0 U l        [        R                  R                  U 5        g)aF  
Initialize kqueue object, file descriptor tracking dictionaries, and
the base class.

See:
    - http://docs.python.org/library/select.html
    - www.freebsd.org/cgi/man.cgi?query=kqueue
    - people.freebsd.org/~jlemon/papers/kqueue.pdf

@param _kqueueImpl: The implementation of L{_IKQueue} to use. A
    hook for testing.
N)
_implr   _kqset_reads_writes_selectablesr   PosixReactorBase__init__)self_kqueueImpls     r   r+   KQueueReactor.__init__Q   sK     !
::$$&eu""++D1r   c                 t    U R                   R                  U R                  R                  XU5      /SS5        g)z
Private method for changing kqueue registration on a given FD
filtering for events given filter/op. This will never block and
returns nothing.
r   N)r%   controlr$   r   )r,   fdfilterops       r   _updateRegistration!KQueueReactor._updateRegistratione   s.     	$**++B;<aCr   c                 F    U R                   R                  5         SU l         g)z1
Implement L{IReactorDaemonize.beforeDaemonize}.
N)r%   close)r,   s    r   beforeDaemonizeKQueueReactor.beforeDaemonizem   s     	r   c                     U R                   R                  5       U l        U R                   H  nU R	                  U[
        [        5        M      U R                   H  nU R	                  U[        [        5        M      g)z0
Implement L{IReactorDaemonize.afterDaemonize}.
N)	r$   r   r%   r'   r4   r   r   r(   r   r,   r1   s     r   afterDaemonizeKQueueReactor.afterDaemonize}   sX     ::$$&++B$$RC ,,B$$R)D r   c                 J   UR                  5       nX R                  ;  aF   U R                  U[        [        5        XR                  U'   U R                  R                  U5        gg! [
         a     N7f = f! XR                  U'   U R                  R                  U5        f = f)z'
Implement L{IReactorFDSet.addReader}.
N)filenor'   r4   r   r   OSErrorr)   add)r,   readerr1   s      r   	addReaderKQueueReactor.addReader   s     ]]_[[ $((^YG )/!!"%# !   )/!!"%##   A' '
A41A7 3A44A7 7+B"c                 J   UR                  5       nX R                  ;  aF   U R                  U[        [        5        XR                  U'   U R                  R                  U5        gg! [
         a     N7f = f! XR                  U'   U R                  R                  U5        f = f)z'
Implement L{IReactorFDSet.addWriter}.
N)r?   r(   r4   r   r   r@   r)   rA   )r,   writerr1   s      r   	addWriterKQueueReactor.addWriter   s     ]]_\\!%((_iH )/!!"%  $ "   )/!!"%  $rE   c                    Sn UR                  5       nUS:X  a,  U R                  R                  5        H  u  p4XL d  M  Sn  O   gX0R                  ;   a\  U R                  R                  U5        X0R                  ;  a  U R                  U	 U(       d   U R                  U[        [        5        ggg! [         a    Sn Nf = f! [         a     gf = f)z*
Implement L{IReactorFDSet.removeReader}.
FTN)r?   BaseExceptionr)   itemsr'   remover(   r4   r   r   r@   )r,   rB   wasLostr1   fdess        r   removeReaderKQueueReactor.removeReader   s     	B 8 --335>"G 6
 KKr"%%%b),,RN 	   	B	   #   B2 C 2C C
CCc                    Sn UR                  5       nUS:X  a,  U R                  R                  5        H  u  p4XL d  M  Sn  O   gX0R                  ;   a\  U R                  R                  U5        X0R                  ;  a  U R                  U	 U(       d   U R                  U[        [        5        ggg! [         a    Sn Nf = f! [         a     gf = f)z*
Implement L{IReactorFDSet.removeWriter}.
FrK   TN)r?   rL   r)   rM   r(   rN   r'   r4   r   r   r@   )r,   rG   rO   r1   rP   s        r   removeWriterKQueueReactor.removeWriter   s     	B 8 --335>"G 6
 LL#$%%b),,R,O 	   	B	   rS   c                     U R                  U R                   Vs/ s H  oR                  U   PM     snU R                   Vs/ s H  oR                  U   PM     sn5      $ s  snf s  snf )z'
Implement L{IReactorFDSet.removeAll}.
)
_removeAllr'   r)   r(   r;   s     r   	removeAllKQueueReactor.removeAll   sY     -1[[9[rr"[9-1\\:\rr"\:
 	
9:s   A!A&
c                 ^    U R                    Vs/ s H  oR                  U   PM     sn$ s  snf )z(
Implement L{IReactorFDSet.getReaders}.
)r'   r)   r;   s     r   
getReadersKQueueReactor.getReaders   s)     15<"!!"%<<<   *c                 ^    U R                    Vs/ s H  oR                  U   PM     sn$ s  snf )z(
Implement L{IReactorFDSet.getWriters}.
)r(   r)   r;   s     r   
getWritersKQueueReactor.getWriters   s)     15="!!"%===r^   c                    Uc  Sn U R                   R                  / [        U R                  5      U5      nU R                  nU H7  nUR                  n U R                  U   n[        R                  " XtXvU5        M9     g! [         a)  nUR
                  [
        R                  :X  a   SnAge SnAff = f! [         a     M  f = f)z!
Poll the kqueue for new events.
N   )r%   r0   lenr)   r@   errnoEINTR_doWriteOrReadidentr   callWithLoggerKeyError)r,   timeouteventse_drdweventr1   
selectables           r   doKEventKQueueReactor.doKEvent   s     ?G		XX%%b#d.?.?*@'JF ##EBM!..r2
 "":jeL   	 ww%++%	   s/   0B B7
B4B/.B//B47
CCc                 4   SnSnUR                   UR                  UR                  UR                  4u  pgpU[        -  (       a  U(       a  U	(       a  [
        R                  nO` UR                  5       S:X  a  Sn[        R                  nO8U[        :X  a  SnUR                  5       nU[        :X  a  SnUR                  5       nU(       a  U R'                  XU5        gg! [         a/    [        R                   " 5       n["        R$                  " US5         NSf = f)zz
Private method called when a FD is ready for reading, writing or was
lost. Do the work and raise errors where necessary.
NFrK   TzSAn exception was raised from application code while processing a reactor selectable)r2   flagsdatafflagsr   r   CONNECTION_LOSTr?   r   _NO_FILEDESCr   doReadr   doWriterL   r   Failurer   err_disconnectSelectable)
r,   rp   r1   ro   whyinReadr2   rt   ru   rv   s
             r   rg   KQueueReactor._doWriteOrRead  s    
 LLKKJJLL	)
% 9&&&C$$&","F#00C/!%(//10!&(002 &&z?  !  oo'=	s   $&C 8C 6DD)r$   r%   r'   r)   r(   N)r   r   r   r   r   selectr+   r4   r8   r<   rC   rH   rQ   rU   rY   r\   r`   rq   rg   doIterationr   r   r   r   r!   r!   6   sY    2 $* 2(D E$%44
=>M<'@R Kr   r!   c                  4    [        5       n SSKJn  U" U 5        g)z
Install the kqueue() reactor.
r   )installReactorN)r!   twisted.internet.mainr   )pr   s     r   installr   :  s     	A41r   r   ) r   re   r   zope.interfacer   r   r   r   twisted.internetr   r   twisted.internet.interfacesr	   r
   twisted.pythonr   r   getattrr   r   r   r   r   AttributeErrorrm   ImportErrorr   directlyProvidesr*   r!   r   __all__r   r   r   <module>r      s   
   J J , H '
 ,I6>2L,IV%56Nf&78O
:y :   fh / ]-.@I.. @ /@F I
&}  
a.s   -B$ $B7*B22B7