
    h!                     @   S r SSKrSSKrSSKJr  SSKJr  SSKJrJ	r	  SSK
Jr   SS jr " S	 S
\5      r " S S\	R                  5      rS0 SSS4S jr " S S\	R                  5      rS0 SS4S jr " S S\	R                  5      rS0 SSS4S jrS rS rS r/ SQrg)z
Utility methods.
    Nwraps)BytesIO)deferprotocol)failure c                     Uc  SSK Jn  [        R                  " 5       nU " U/UQ76 nUR	                  XU4[        U5      -   X45        U$ )Nr   )reactor)twisted.internetr   r   DeferredspawnProcesstuple)	r   
executableargsenvpathr   	protoArgsdps	            ]/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/internet/utils.py_callProtocolWithDeferredr      sI     ,AYAd(CSOH    c                       \ rS rSrSrS rSrg)_UnexpectedErrorOutput   a]  
Standard error data was received where it was not expected.  This is a
subclass of L{IOError} to preserve backward compatibility with the previous
error behavior of L{getProcessOutput}.

@ivar processEnded: A L{Deferred} which will fire when the process which
    produced the data on stderr has ended (exited and all file descriptors
    closed).
c                 D    [         R                  U SU< 35        X l        g )Nzgot stderr: )IOError__init__processEnded)selftextr    s      r   r   _UnexpectedErrorOutput.__init__*   s    dX67(r   )r    N)__name__
__module____qualname____firstlineno____doc__r   __static_attributes__r	   r   r   r   r      s    )r   r   c                   :    \ rS rSrSrS
S jrS rS rS rS r	Sr
g	)
_BackRelay/   a  
Trivial protocol for communicating with a process and turning its output
into the result of a L{Deferred}.

@ivar deferred: A L{Deferred} which will be called back with all of stdout
    and, if C{errortoo} is true, all of stderr as well (mixed together in
    one string).  If C{errortoo} is false and any bytes are received over
    stderr, this will fire with an L{_UnexpectedErrorOutput} instance and
    the attribute will be set to L{None}.

@ivar onProcessEnded: If C{errortoo} is false and bytes are received over
    stderr, this attribute will refer to a L{Deferred} which will be called
    back when the process ends.  This C{Deferred} is also associated with
    the L{_UnexpectedErrorOutput} which C{deferred} fires with earlier in
    this case so that users can determine when the process has actually
    ended, in addition to knowing when bytes have been received via stderr.
c                     Xl         [        5       U l        U(       a  U R                  U l        g U R
                  U l        g N)deferredr   serrReceivedIsGooderrReceivederrReceivedIsBad)r!   r/   errortoos      r   r   _BackRelay.__init__B   s/     #55D#44Dr   c                    U R                   b  [        R                  " 5       U l        [	        XR                  5      nU R                   R                  [        R                  " U5      5        S U l         U R                  R                  5         g g r.   )
r/   r   r   onProcessEndedr   errbackr   Failure	transportloseConnection)r!   r"   errs      r   r3   _BackRelay.errReceivedIsBadJ   sb    ==$"'.."2D(/B/BCCMM!!'//#"67 DMNN))+ %r   c                 :    U R                   R                  U5        g r.   r0   writer!   r"   s     r   r1   _BackRelay.errReceivedIsGoodR       Tr   c                 :    U R                   R                  U5        g r.   r?   rA   s     r   outReceived_BackRelay.outReceivedU   rC   r   c                     U R                   b4  U R                   R                  U R                  R                  5       5        g U R                  b  U R                  R                  U5        g g r.   )r/   callbackr0   getvaluer7   r8   r!   reasons     r   r    _BackRelay.processEndedX   sN    ==$MM""466??#45  ,''/ -r   )r/   r2   r7   r0   N)r   )r$   r%   r&   r'   r(   r   r3   r1   rE   r    r)   r	   r   r   r+   r+   /   s     $5,0r   r+   c                 (   ^ [        U4S jXX#U5      $ )a  
Spawn a process and return its output as a deferred returning a L{bytes}.

@param executable: The file name to run and get the output of - the
                   full path should be used.

@param args: the command line arguments to pass to the process; a
             sequence of strings. The first string should B{NOT} be the
             executable's name.

@param env: the environment variables to pass to the process; a
            dictionary of strings.

@param path: the path to run the subprocess in - defaults to the
             current directory.

@param reactor: the reactor to use - defaults to the default reactor

@param errortoo: If true, include stderr in the result.  If false, if
    stderr is received the returned L{Deferred} will errback with an
    L{IOError} instance with a C{processEnded} attribute.  The
    C{processEnded} attribute refers to a L{Deferred} which fires when the
    executed process ends.
c                    > [        U TS9$ )N)r4   )r+   )r   r4   s    r   <lambda>"getProcessOutput.<locals>.<lambda>y   s    *Q2r   )r   )r   r   r   r   r   r4   s        `r   getProcessOutputrQ   _   s    2 %2JcQX r   c                        \ rS rSrS rS rSrg)_ValueGetter}   c                     Xl         g r.   r/   )r!   r/   s     r   r   _ValueGetter.__init__~   s     r   c                 b    U R                   R                  UR                  R                  5        g r.   )r/   rH   valueexitCoderJ   s     r   r    _ValueGetter.processEnded   s    v||445r   rV   N)r$   r%   r&   r'   r   r    r)   r	   r   r   rS   rS   }   s    !6r   rS   c                 &    [        [        XX#U5      $ )z7Spawn a process and return its exit code as a Deferred.)r   rS   )r   r   r   r   r   s        r   getProcessValuer]      s    $\:SPWXXr   c                   *    \ rS rSrSS jrS rS rSrg)_EverythingGetter   Nc                     Xl         [        5       U l        [        5       U l        U R                  R                  U l        U R                  R                  U l        X l        g r.   )r/   r   outBuferrBufr@   rE   r2   
stdinBytes)r!   r/   rd   s      r   r   _EverythingGetter.__init__   sB     ii;;,,;;,,$r   c                     U R                   bA  U R                  R                  SU R                   5        U R                  R                  5         g g )Nr   )rd   r:   writeToChild
closeStdin)r!   s    r   connectionMade _EverythingGetter.connectionMade   s;    ??&NN''4??; NN%%'	 'r   c                 H   U R                   R                  5       nU R                  R                  5       nUR                  nUR                  nUR
                  (       a(  U R                  R                  X#UR
                  45        g U R                  R                  X#U45        g r.   )	rb   rI   rc   rY   rZ   signalr/   r8   rH   )r!   rK   outr<   ecodes         r   r    _EverythingGetter.processEnded   sq    kk""$kk""$LLzz88MM!!3QXX"67MM""Cd#34r   )r/   rc   r2   rb   rE   rd   r.   )r$   r%   r&   r'   r   ri   r    r)   r	   r   r   r_   r_      s    %(5r   r_   c           
      *    [        [        U UUUUU4S9$ )a  Spawn a process and returns a Deferred that will be called back with
its output (from stdout and stderr) and it's exit code as (out, err, code)
If a signal is raised, the Deferred will errback with the stdout and
stderr up to that point, along with the signal, as (out, err, signalNum)
)r   )r   r_   )r   r   r   r   r   rd   s         r   getProcessOutputAndValuerr      s(     %- r   c                 z    U H#  n [         R                  R                  U5        M%     U $ ! [         a     M6  f = fr.   )warningsfiltersremove
ValueError)passthroughaddedFiltersfs      r   _resetWarningFiltersr{      sA    	##A& 
   		s   ,
::c                    U  H  u  pE[         R                  " U0 UD6  M     [         R                  S[        U 5       n U" U0 UD6n[	        U[
        R                  5      (       a  UR                  [        U5        U$ [        SU5        U$ ! [         a9    [        R                  " 5       n[        SU5        US   R                  US   5      ef = f)a=  
Run the function I{f}, but with some warnings suppressed.

This calls L{warnings.filterwarnings} to add warning filters before
invoking I{f}. If I{f} returns a L{Deferred} then the added filters are
removed once the deferred fires. Otherwise they are removed immediately.

Note that the list of warning filters is a process-wide resource, so
calling this function will affect all threads.

@param suppressedWarnings:
    A list of arguments to pass to L{warnings.filterwarnings}, a sequence
    of (args, kwargs) 2-tuples.

@param f: A callable, which may return a L{Deferred}.

@param a: Positional arguments passed to I{f}

@param kw: Keyword arguments passed to I{f}

@return: The result of C{f(*a, **kw)}

@seealso: L{twisted.python.util.runWithWarningsSuppressed}
    functions similarly, but doesn't handled L{Deferred}s.
N      )rt   filterwarningsru   len
isinstancer   r   addBothr{   BaseExceptionsysexc_infowith_traceback)	suppressedWarningsrz   akwr   kwargsry   resultr   s	            r   runWithWarningsSuppressedr      s    4 +00 +##$=c*<&=>LA fenn--NN/>  !|4  6<<>T<0qk((!556s   B ACc                 4   ^ ^ [        T 5      U U4S j5       nU$ )z
Wrap C{f} in a callable which suppresses the indicated warnings before
invoking C{f} and unsuppresses them afterwards.  If f returns a Deferred,
warnings will remain suppressed until the Deferred fires.
c                  $   > [        TT/U Q70 UD6$ r.   )r   )r   r   rz   r   s     r   warningSuppressingWrapper3suppressWarnings.<locals>.warningSuppressingWrapper   s    ();QIIbIIr   r   )rz   r   r   s   `` r   suppressWarningsr      s%     1XJ J %$r   )r   r   rQ   r]   rr   )Nr	   )r(   r   rt   	functoolsr   ior   r   r   r   twisted.pythonr   r   r   r   ProcessProtocolr+   rQ   rS   r]   r_   rr   r{   r   r   __all__r	   r   r   <module>r      s   

     , " DF	)W ) -0)) -0` ')btTTU <68++ 6 &(RdD Y
500 58 RdDT&(V%r   