
    hO                       S r SSKJr  SSKJrJrJr  SSK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Jr  SSKJr  S	 rS
 r " S S5      r\" \R2                  5       " S S5      5       r\" \R6                  \R8                  \R:                  \R<                  \R>                  5       " S S\\5      5       r \4SS jjr!SS jr"/ SQr#g)z+
Support for generic select()able objects.
    )annotations)AF_INETAF_INET6	inet_pton)IterableListOptionalUnion)implementer)
interfacesmain)failurereflect)lazyByteSlicec                D    [        U [        5      (       d  [        S5      eg )NzData must be bytes)
isinstancebytes	TypeError)objs    `/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/internet/abstract.py_dataMustBeBytesr      s     c5!!,-- "    c                D    SR                  [        U 5      US  /U-   5      $ )Nr   )join
memoryview)bObjoffsetbArrays      r   _concatenater      s%    88Z%fg./&899r   c                  6    \ rS rSrSrSrSrSrS rS r	S r
Srg)	_ConsumerMixin"   a  
L{IConsumer} implementations can mix this in to get C{registerProducer} and
C{unregisterProducer} methods which take care of keeping track of a
producer's state.

Subclasses must provide three attributes which L{_ConsumerMixin} will read
but not write:

  - connected: A C{bool} which is C{True} as long as the consumer has
    someplace to send bytes (for example, a TCP connection), and then
    C{False} when it no longer does.

  - disconnecting: A C{bool} which is C{False} until something like
    L{ITransport.loseConnection} is called, indicating that the send buffer
    should be flushed and the connection lost afterwards.  Afterwards,
    C{True}.

  - disconnected: A C{bool} which is C{False} until the consumer no longer
    has a place to send bytes, then C{True}.

Subclasses must also override the C{startWriting} method.

@ivar producer: L{None} if no producer is registered, otherwise the
    registered producer.

@ivar producerPaused: A flag indicating whether the producer is currently
    paused.
@type producerPaused: L{bool}

@ivar streamingProducer: A flag indicating whether the producer was
    registered as a streaming (ie push) producer or not (ie a pull
    producer).  This will determine whether the consumer may ever need to
    pause and resume it, or if it can merely call C{resumeProducing} on it
    when buffer space is available.
@ivar streamingProducer: C{bool} or C{int}

NFc                    [        S5      e)z
Override in a subclass to cause the reactor to monitor this selectable
for write events.  This will be called once in C{unregisterProducer} if
C{loseConnection} has previously been called, so that the connection can
actually close.
z!%r did not implement startWritingNotImplementedErrorselfs    r   startWriting_ConsumerMixin.startWritingM   s     ""EFFr   c                    U R                   b  [        SU< SU R                   < S35      eU R                  (       a  UR                  5         gXl         X l        U(       d  UR                  5         gg)a  
Register to receive data from a producer.

This sets this selectable to be a consumer for a producer.  When this
selectable runs out of data on a write() call, it will ask the producer
to resumeProducing(). When the FileDescriptor's internal data buffer is
filled, it will ask the producer to pauseProducing(). If the connection
is lost, FileDescriptor calls producer's stopProducing() method.

If streaming is true, the producer should provide the IPushProducer
interface. Otherwise, it is assumed that producer provides the
IPullProducer interface. In this case, the producer won't be asked to
pauseProducing(), but it has to be careful to write() data only when its
resumeProducing() method is called.
NzCannot register producer z, because producer z was never unregistered.)producerRuntimeErrordisconnectedstopProducingstreamingProducerresumeProducing)r'   r+   	streamings      r   registerProducer_ConsumerMixin.registerProducerV   s_      ==$#+T]]<  ""$$M%."((* r   c                z    SU l         U R                  (       a#  U R                  (       a  U R                  5         ggg)z=
Stop consuming data from a producer, without disconnecting.
N)r+   	connecteddisconnectingr(   r&   s    r   unregisterProducer!_ConsumerMixin.unregisterProducers   s-     >>d00 1>r   )r+   r/   )__name__
__module____qualname____firstlineno____doc__r+   producerPausedr/   r(   r2   r7   __static_attributes__ r   r   r!   r!   "   s*    $L HNG+: r   r!   c                  (    \ rS rSrSrSS jrS rSrg)	_LogOwner|   z
Mixin to help implement L{interfaces.ILoggingContext} for transports which
have a protocol, the log prefix of which should also appear in the
transport's log prefix.
c                    [         R                  R                  U5      (       a  UR                  5       $ UR                  R
                  $ )z
Determine the log prefix to use for messages related to
C{applicationObject}, which may or may not be an
L{interfaces.ILoggingContext} provider.

@return: A C{str} giving the log prefix to use.
)r   ILoggingContext
providedBy	logPrefix	__class__r9   )r'   applicationObjects     r   _getLogPrefix_LogOwner._getLogPrefix   s=     %%001BCC$..00 **333r   c                    g)z
Override this method to insert custom logging behavior.  Its
return value will be inserted in front of every line.  It may
be called more times than the number of output lines.
-r@   r&   s    r   rG   _LogOwner.logPrefix   s     r   r@   N)rI   objectreturnstr)r9   r:   r;   r<   r=   rJ   rG   r?   r@   r   r   rB   rB   |   s    
4r   rB   c                      \ 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%S jjrS	 rS&S
 jrS rS rS rS rS rS'S jrS 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" r(S#r)g)*FileDescriptor   z
An object which can be operated on by select().

This is an abstract superclass of all objects which may be notified when
they are readable or writable; e.g. they have a file-descriptor that is
valid to be passed to select(2).
r   Fr   i   Nc                J    U(       d  SSK Jn  UnXl        / U l        SU l        g)z
@param reactor: An L{IReactorFDSet} provider which this descriptor will
    use to get readable and writeable event notifications.  If no value
    is given, the global reactor will be used.
r   )reactorN)twisted.internetrV   _tempDataBuffer_tempDataLen)r'   rV   _reactors      r   __init__FileDescriptor.__init__   s$     <G,.r   c                    SU l         SU l        U R                  b!  U R                  R                  5         SU l        U R	                  5         U R                  5         g)aJ  The connection was lost.

This is called when the connection on a selectable object has been
lost.  It will be called whether the connection was closed explicitly,
an exception occurred in an event handler, or the other end of the
connection closed it first.

Clean up state here, but make sure to call back up to FileDescriptor.
   r   N)r-   r5   r+   r.   stopReadingstopWritingr'   reasons     r   connectionLostFileDescriptor.connectionLost   sL     ==$MM'') DMr   c                Z    [        S[        R                  " U R                  5      -  5      e)a]  
Write as much as possible of the given data, immediately.

This is called to invoke the lower-level writing functionality, such
as a socket's send() method, or a file's write(); this method
returns an integer or an exception.  If an integer, it is the number
of bytes written (possibly zero); if an exception, it indicates the
connection was lost.
z#%s does not implement writeSomeDatar%   r   qualrH   r'   datas     r   writeSomeDataFileDescriptor.writeSomeData   s&     "1GLL4PP
 	
r   c                Z    [        S[        R                  " U R                  5      -  5      e)z
Called when data is available for reading.

Subclasses must override this method. The result will be interpreted
in the same way as a result of doWrite().
z%s does not implement doReadrf   r&   s    r   doReadFileDescriptor.doRead   s&     "*W\\$..-II
 	
r   c                6   [        U R                  5      U R                  -
  nXR                  :  al  US:  a1  [	        U R                  U R                  U R
                  5      U l        O SR                  U R
                  5      U l        SU l        / U l        SU l        U R                  (       a0  U R                  [        U R                  U R                  5      5      nOU R                  U R                  5      n[        U[        5      (       d  US:  a  U$ U =R                  U-  sl        U R                  [        U R                  5      :X  a  U R                  (       d  SU l        SU l        U R                  5         U R                  bD  U R                  (       a  U R                  (       a"  SU l        U R                  R!                  5         gU R"                  (       a  U R%                  5       $ U R&                  (       a  SU l        U R+                  5       nU$ g)z
Called when data can be written.

@return: L{None} on success, an exception or a negative integer on
    failure.

@see: L{twisted.internet.interfaces.IWriteDescriptor.doWrite}.
r   r   NFT)len
dataBufferr   
SEND_LIMITr   rX   r   rY   rj   r   r   	Exceptionr`   r+   r/   r>   r0   r6   _postLoseConnection_writeDisconnecting_writeDisconnected_closeWriteConnection)r'   	remaininglresults       r   doWriteFileDescriptor.doWrite   s    (4;;6	&1} #/OOT[[$2F2F# #&((4+?+?"@DK#%D  !D ;;""=$++#NOA ""4??3A a##q1uHq;;#doo..t7H7H!DODK}}(++0C0C ',#--/  ## //11))
 +/'335r   c                "    [         R                  $ )zuCalled after a loseConnection(), when all data has been written.

Whatever this returns is then returned by doWrite.
)r   CONNECTION_DONEr&   s    r   rt   "FileDescriptor._postLoseConnection9  s     ###r   c                    g Nr@   r&   s    r   rw   $FileDescriptor._closeWriteConnectionA  s    r   c                &    U R                  U5        g r   rc   ra   s     r   writeConnectionLost"FileDescriptor.writeConnectionLostE      F#r   c                &    U R                  U5        g r   r   ra   s     r   readConnectionLost!FileDescriptor.readConnectionLostI  r   r   c                    [        5       er   r$   r&   s    r   getHostFileDescriptor.getHostM      !##r   c                    [        5       er   r$   r&   s    r   getPeerFileDescriptor.getPeerQ  r   r   c                `    [        U R                  5      U R                  -   U R                  :  $ )aA  
Determine whether the user-space send buffer for this transport is full
or not.

When the buffer contains more than C{self.bufferSize} bytes, it is
considered full.  This might be improved by considering the size of the
kernel send buffer and how much of it is free.

@return: C{True} if it is full, C{False} otherwise.
)rp   rq   rY   
bufferSizer&   s    r   _isSendBufferFull FileDescriptor._isSendBufferFullU  s'     4??#d&7&77$//IIr   c                    U R                   bJ  U R                  (       a8  U R                  5       (       a"  SU l        U R                   R	                  5         gggg)zI
Possibly pause a producer, if there is one and the send buffer is full.
NT)r+   r/   r   r>   pauseProducingr&   s    r   _maybePauseProducer"FileDescriptor._maybePauseProducerb  sK    
 ==$)?)?%%''&*#,,. ( *@$r   c                "   [        U5        U R                  (       a  U R                  (       a  gU(       aZ  U R                  R	                  U5        U =R
                  [        U5      -  sl        U R                  5         U R                  5         gg)a  Reliably write some data.

The data is buffered until the underlying file descriptor is ready
for writing. If there is more than C{self.bufferSize} data in the
buffer and this descriptor has a registered streaming producer, its
C{pauseProducing()} method will be called.
N)	r   r5   rv   rX   appendrY   rp   r   r(   rh   s     r   writeFileDescriptor.writen  sg     	~~!8!8  ''-T*$$&	 r   c                D   U H  n[        U5        M     U R                  (       a  U(       a  U R                  (       a  gU R                  R	                  U5        U H!  nU =R
                  [        U5      -  sl        M#     U R                  5         U R                  5         g)a  
Reliably write a sequence of data.

Currently, this is a convenience method roughly equivalent to::

    for chunk in iovec:
        fd.write(chunk)

It may have a more efficient implementation at a later time or in a
different reactor.

As with the C{write()} method, if a buffer size limit is reached and a
streaming producer is registered, it will be paused until the buffered
data is written to the underlying file descriptor.
N)	r   r5   rv   rX   extendrY   rp   r   r(   )r'   iovecis      r   writeSequenceFileDescriptor.writeSequence  su      AQ ~~Ud.E.E##E*AQ'   "r   c                d   U R                   (       a  U R                  (       d  U R                  (       aT  U R                  5         U R	                  5         U R                  [        R                  " [        R                  5      5        gU R                  5         U R                  5         SU l        ggg)a  Close the connection at the next available opportunity.

Call this to cause this FileDescriptor to lose its connection.  It will
first write any data that it has buffered.

If there is data buffered yet to be written, this method will cause the
transport to lose its connection as soon as it's done flushing its
write buffer.  If you have a producer registered, the connection won't
be closed until the producer is finished. Therefore, make sure you
unregister your producer when it's finished, or the connection will
never close.
r^   N)r5   r6   rv   r_   r`   rc   r   Failurer   r~   r(   r&   s    r   loseConnectionFileDescriptor.loseConnection  sx     >>$"4"4&&  "  "##GOOD4H4H$IJ  "!!#%&" #5>r   c                2    SU l         U R                  5         g )NT)ru   r(   r&   s    r   loseWriteConnection"FileDescriptor.loseWriteConnection  s    #' r   c                :    U R                   R                  U 5        g)z{Stop waiting for read availability.

Call this to remove this selectable from being notified when it is
ready for reading.
N)rV   removeReaderr&   s    r   r_   FileDescriptor.stopReading       	!!$'r   c                :    U R                   R                  U 5        g)z|Stop waiting for write availability.

Call this to remove this selectable from being notified when it is ready
for writing.
N)rV   removeWriterr&   s    r   r`   FileDescriptor.stopWriting  r   r   c                :    U R                   R                  U 5        g)z$Start waiting for read availability.N)rV   	addReaderr&   s    r   startReadingFileDescriptor.startReading  s    t$r   c                :    U R                   R                  U 5        g)z{Start waiting for write availability.

Call this to have this FileDescriptor be notified whenever it is ready for
writing.
N)rV   	addWriterr&   s    r   r(   FileDescriptor.startWriting  s     	t$r   i   c                D    U R                  5         U R                  5         g)zStop consuming data.

This is called when a producer has lost its connection, to tell the
consumer to go lose its connection (and break potential circular
references).
N)r7   r   r&   s    r   stopConsumingFileDescriptor.stopConsuming  s     	!r   c                l    U R                   (       a#  U R                  (       d  U R                  5         g g g r   )r5   r6   r   r&   s    r   r0   FileDescriptor.resumeProducing  s$    >>$"4"4 #5>r   c                $    U R                  5         g r   )r_   r&   s    r   r   FileDescriptor.pauseProducing  s    r   c                $    U R                  5         g r   )r   r&   s    r   r.   FileDescriptor.stopProducing  s    r   c                    g)zFile Descriptor number for select().

This method must be overridden or assigned in subclasses to
indicate a valid file descriptor for the operating system.
r@   r&   s    r   filenoFileDescriptor.fileno  s     r   )rX   rY   rv   ru   r5   rq   r-   r6   r   r+   r>   rV   r   )rV   z"Optional[interfaces.IReactorFDSet])ri   r   rP   zUnion[int, BaseException])rb   zfailure.FailurerP   None)ri   r   rP   r   )r   zIterable[bytes]rP   r   )*r9   r:   r;   r<   r=   r5   r-   r6   ru   rv   rq   r   rr   r[   rc   rj   rm   r{   rt   rw   r   r   r   r   r   r   r   r   r   r   r_   r`   r   r(   r+   r   r   r0   r   r.   r   r?   r@   r   r   rS   rS      s     ILMJFJ$
	
DL$$$$$J
/ "4'2((%% HJ r   rS   c                V   [        U [        5      (       a   U R                  S5      n U[        :X  a  U R                  SS5      S   n O0U[        :X  a  U R                  S5      S:w  a  gO[        SU< 35      e [        X5        g	! [         a     gf = f! [        [        4 a     gf = f)
a  
Determine whether the given string represents an IP address of the given
family; by default, an IPv4 address.

@param addr: A string which may or may not be the decimal dotted
    representation of an IPv4 address.
@param family: The address family to test for; one of the C{AF_*} constants
    from the L{socket} module.  (This parameter has only been available
    since Twisted 17.1.0; previously L{isIPAddress} could only test for IPv4
    addresses.)

@return: C{True} if C{addr} represents an IPv4 address, C{False} otherwise.
asciiF%r^   r   .   zunknown address family T)r   r   decodeUnicodeDecodeErrorr   splitr   count
ValueErrorr   OSError)addrfamilys     r   isIPAddressr     s     $	;;w'D  zz#q!!$	7	 ::c?a   26*=>> 	& ) " 		$   s#   B 9B 
BBB('B(c                "    [        U [        5      $ )a  
Determine whether the given string represents an IPv6 address.

@param addr: A string which may or may not be the hex
    representation of an IPv6 address.
@type addr: C{str}

@return: C{True} if C{addr} represents an IPv6 address, C{False}
    otherwise.
@rtype: C{bool}
)r   r   )r   s    r   isIPv6Addressr   !  s     tX&&r   )rS   r   r   N)r   rQ   r   intrP   bool)r   rQ   rP   r   )$r=   
__future__r   socketr   r   r   typingr   r   r	   r
   zope.interfacer   rW   r   r   twisted.pythonr   r   twisted.python.compatr   r   r   r!   rE   rB   IPushProducerIReadWriteDescriptor	IConsumer
ITransportIHalfCloseableDescriptorrS   r   r   __all__r@   r   r   <module>r      s   
 # / / 2 2 & - + 0.:W  W t Z''(  )8 ##''V^Y VVr
 *1 %P' =r   