
    h8                       S r SSKJr  SSKrSSKrSSKrSSKJrJr  SSK	J
r
  SSKJrJrJrJrJr  SSK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SKJr  SSKJ r   SSK!J"r"J#r#  Sr$\%" \RL                  5      r'\RP                  RS                  \RT                  5      \'S'    " S S\R*                  5      r+ " S S\#5      r,g)z
Tests for L{twisted.internet.stdio}.

@var properEnv: A copy of L{os.environ} which has L{bytes} keys/values on POSIX
    platforms and native L{str} keys/values on Windows.
    )annotationsN)AnyCallable)skipIf)defererrorprotocolreactorstdio)IProcessTransportIReactorProcess)ProcessProtocol)filepathlog)Failure)requireModule)platform)ConnectionLostNotifyingProtocol)SkipTestTestCases   xyz123abc Twisted is great!
PYTHONPATHc                  N    \ rS rSr% SrSrS\S'   S\S'   SS jrS	 rS
 r	S r
Srg)StandardIOTestProcessProtocol'   a  
Test helper for collecting output from a child process and notifying
something when it exits.

@ivar onConnection: A L{defer.Deferred} which will be called back with
L{None} when the connection to the child process is established.

@ivar onCompletion: A L{defer.Deferred} which will be errbacked with the
failure associated with the child process exiting when it exits.

@ivar onDataReceived: A L{defer.Deferred} which will be called back with
this instance whenever C{childDataReceived} is called, or L{None} to
suppress these callbacks.

@ivar data: A C{dict} mapping file descriptors to strings containing all
bytes received from the child process on each file descriptor.
Nzdefer.Deferred[None] | NoneonDataReceivedr   	transportc                z    [         R                  " 5       U l        [         R                  " 5       U l        0 U l        g N)r   DeferredonConnectiononCompletiondataselfs    ^/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/test/test_stdio.py__init__&StandardIOTestProcessProtocol.__init__=   s'    27..2B27..2B&(	    c                :    U R                   R                  S 5        g r   )r    callbackr#   s    r%   connectionMade,StandardIOTestProcessProtocol.connectionMadeB   s    ""4(r(   c                    U R                   R                  US5      U-   U R                   U'   U R                  b%  U R                  Sso0l        UR                  U 5        gg)z{
Record all bytes received from the child process in the C{data}
dictionary.  Fire C{onDataReceived} if it is not L{None}.
r(   N)r"   getr   r*   )r$   namebytesds       r%   childDataReceived/StandardIOTestProcessProtocol.childDataReceivedE   sU    
 ))--c2U:		$*%)%8%8$"A"JJt +r(   c                :    U R                   R                  U5        g r   )r!   r*   )r$   reasons     r%   processEnded*StandardIOTestProcessProtocol.processEndedO   s    ""6*r(   )r"   r!   r    r   )returnNone)__name__
__module____qualname____firstlineno____doc__r   __annotations__r&   r+   r2   r6   __static_attributes__ r(   r%   r   r   '   s.    $ 37N/6  )
)+r(   r   c                  .   \ rS rSr\R
                  " 5       (       a  \" S5      c  Sr          SS jr      SS jr	S r
 S     SS jjrSS	 jrSS
 jrSS jrS rS rS rS rS rS rS r\" \R
                  " 5       S5      S 5       rSrg)StandardInputOutputTestsS   win32processNzIOn windows, spawnProcess is not available in the absence of win32process.c                .   [        U[        5      (       a  UR                  5       n[        R                  SSU-   [
        R                  R                  /[        U5      -   n[        [
        5      R                  " U[        R                  U4S[        0UD6$ )a  
Launch a child Python process and communicate with it using the given
ProcessProtocol.

@param proto: A L{ProcessProtocol} instance which will be connected to
    the child process.

@param sibling: The basename of a file containing the Python program to
    run in the child process.

@param *args: strings which will be passed to the child process on the
    command line as C{argv[2:]}.

@param **kw: additional arguments to pass to L{reactor.spawnProcess}.

@return: The L{IProcessTransport} provider for the spawned process.
z-mztwisted.test.env)
isinstancer0   decodesys
executabler
   	__class__r;   listr   spawnProcess	properEnv)r$   protosiblingargskwprocargss         r%   _spawnProcess&StandardInputOutputTests._spawnProcessZ   s    ( gu%%nn&GNNg%((	

 J w'443>>8
1:
>@
 	
r(   c                @   ^ ^ U 4S jnU4S jnUR                  X45      $ )Nc                0   > TR                  SU < 35        g )Nz%Process terminated with non-Failure: )fail)resultr$   s    r%   cb4StandardInputOutputTests._requireFailure.<locals>.cb}   s    II=fZHIr(   c                   > T" U 5      $ r   rA   )errr*   s    r%   eb4StandardInputOutputTests._requireFailure.<locals>.eb   s    C= r(   )addCallbacks)r$   r1   r*   r[   r_   s   ` `  r%   _requireFailure(StandardInputOutputTests._requireFailurez   s    	J	! ~~b%%r(   c                   ^ ^^ T R                  5       m[        R                  " ST-   5        [        5       mTR                  nT R                  TST5        UUU 4S jnT R                  X5      $ )zl
Verify that a protocol connected to L{StandardIO} can disconnect
itself using C{transport.loseConnection}.
zChild process logging to s   stdio_test_loseconnc                &  > [        T5       nU H*  n[        R                  " SUR                  5       -   5        M,     S S S 5        TR	                  STR
                  5        U R                  [        R                  5        g ! , (       d  f       NJ= f)NzChild logged:    )	openr   msgrstripfailIfInr"   trapr   ProcessDone)r5   flineerrorLogFilepr$   s      r%   r6   BStandardInputOutputTests.test_loseConnection.<locals>.processEnded   sc    l#qDGG,t{{}<=  $ MM!QVV$KK))* $#s   1B
B)mktempr   rh   r   r!   rU   rb   )r$   r1   r6   ro   rp   s   `  @@r%   test_loseConnection,StandardInputOutputTests.test_loseConnection   s^    
 {{}+l:;)+NN14lC	+ ##A44r(   c                  ^^ U R                  5       n[        5       m[        R                  " 5       Tl        SUU4S jjnTR                  R                  U5        S nU R                  TR                  U5      nU R                  TX5        U$ )Nc                   > TR                   nT(       a  TR                  R                  5         U$ TR                  R                  5         U$ r   )r!   r   closeStdout
closeStdin)ignoredr1   outrp   s     r%   cbBytesGStandardInputOutputTests.exampleOutputsAndZeroExitCode.<locals>.cbBytes   s=    A'') H &&(Hr(   c                B    U R                  [        R                  5        g r   )rk   r   rl   )r5   s    r%   r6   LStandardInputOutputTests.exampleOutputsAndZeroExitCode.<locals>.processEnded   s    KK))*r(   )ry   r9   r8   defer.Deferred[None])rr   r   r   r   r   addCallbackrb   rU   )r$   examplerz   ro   r{   r6   r1   rp   s     `    @r%   exampleOutputsAndZeroExitCode6StandardInputOutputTests.exampleOutputsAndZeroExitCode   s{     {{})+ >>+	 	 	
$$W-	+   !1!1<@1g4r(   c                $    U R                  S5      $ )z
When stdin is closed and the protocol connected to it implements
L{IHalfCloseableProtocol}, the protocol's C{readConnectionLost} method
is called.
stdio_test_halfcloser   r#   s    r%   test_readConnectionLost0StandardInputOutputTests.test_readConnectionLost   s     112HIIr(   c                $    U R                  S5      $ )
When stdin is closed and the protocol connnected to it implements
L{IHalfCloseableProtocol} but its C{readConnectionLost} method raises
an exception its regular C{connectionLost} method will be called.
stdio_test_halfclose_buggyr   r#   s    r%   test_buggyReadConnectionLost5StandardInputOutputTests.test_buggyReadConnectionLost   s     112NOOr(   c                "    U R                  SSS9$ )r    stdio_test_halfclose_buggy_writeT)rz   r   r#   s    r%   test_buggyWriteConnectionLost6StandardInputOutputTests.test_buggyWriteConnectionLost   s!     11.D 2 
 	
r(   c                   ^ ^ [        5       m T R                  TS[        SS9  UU 4S jnT R                  TR                  U5      $ ! [         a  n[	        [        U5      5      eSnAff = f)z
Verify that a write made directly to stdout using L{os.write}
after StandardIO has finished is reliably received by the
process reading that stdout.
s   stdio_test_lastwriteT)usePTYNc                   > TR                  TR                  S   R                  [        5      STR                  < S35        U R	                  [
        R                  5        g)ze
Asserts that the parent received the bytes written by the child
immediately after the child starts.
rf   z	Received z) from child, did not find expected bytes.N)
assertTruer"   endswithUNIQUE_LAST_WRITE_STRINGrk   r   rl   r5   rp   r$   s    r%   r6   EStandardInputOutputTests.test_lastWriteReceived.<locals>.processEnded   sM    
 OOq	""#;<AFF:%NO KK))*r(   )r   rU   r   
ValueErrorr   strrb   r!   )r$   er6   rp   s   `  @r%   test_lastWriteReceived/StandardInputOutputTests.test_lastWriteReceived   so     *+	#*,DT  		+ ##ANNLAA  	#3q6""	#s   A 
A*A%%A*c                   ^ ^ [        5       mTR                  nT R                  TS5        UU 4S jnT R                  X5      $ )zk
Verify that the transport of a protocol connected to L{StandardIO}
has C{getHost} and C{getPeer} methods.
s   stdio_test_hostpeerc                   > TR                   S   R                  5       u  pTR                  U5        TR                  U5        U R                  [        R
                  5        g )Nrf   )r"   
splitlinesr   rk   r   rl   )r5   hostpeerrp   r$   s      r%   r6   ?StandardInputOutputTests.test_hostAndPeer.<locals>.processEnded   sE    --/JDOOD!OOD!KK))*r(   r   r!   rU   rb   r$   r1   r6   rp   s   `  @r%   test_hostAndPeer)StandardInputOutputTests.test_hostAndPeer   s?    
 *+NN145	+ ##A44r(   c                   ^ ^ [        5       mTR                  nT R                  TS5        UU 4S jnT R                  X5      $ )zx
Verify that the C{write} method of the transport of a protocol
connected to L{StandardIO} sends bytes to standard out.
s   stdio_test_writec                   > TR                  TR                  S   S5        U R                  [        R                  5        g Nrf   s   ok!assertEqualr"   rk   r   rl   r   s    r%   r6   9StandardInputOutputTests.test_write.<locals>.processEnded  -    QVVAY/KK))*r(   r   r   s   `  @r%   
test_write#StandardInputOutputTests.test_write  s?    
 *+NN112	+ ##A44r(   c                   ^ ^ [        5       mTR                  nT R                  TS5        UU 4S jnT R                  X5      $ )z
Verify that the C{writeSequence} method of the transport of a
protocol connected to L{StandardIO} sends bytes to standard out.
s   stdio_test_writeseqc                   > TR                  TR                  S   S5        U R                  [        R                  5        g r   r   r   s    r%   r6   AStandardInputOutputTests.test_writeSequence.<locals>.processEnded   r   r(   r   r   s   `  @r%   test_writeSequence+StandardInputOutputTests.test_writeSequence  s?    
 *+NN145	+ ##A44r(   c                    U R                  5       n[        US5       n[        S5       H  nUR                  SU4-  5        M     S S S 5        U$ ! , (       d  f       U$ = f)Nwbi      %d
)rr   rg   rangewrite)r$   junkPathjunkFileis       r%   	_junkPath"StandardInputOutputTests._junkPath&  sU    ;;=(D!X4[w!~. ! "  "! s   (A
Ac                  ^ ^^^^^ [        5       mTR                  n/ m[        [        S5      5      mUUUU4S jmT R	                  TS5      mTR
                  R                  T5        UU UU4S jnT R                  X5      $ )zh
Verify that the transport of a protocol connected to L{StandardIO}
is a working L{IProducer} provider.
d   c                   > T(       aP  TR                  STR                  5       4-  5        TR                  TS   5        [        R                  " STS 5        g g )Nr   g{Gz?)appendpopr   r
   	callLater)ignr+   proctoWritewrittens    r%   r+   >StandardInputOutputTests.test_producer.<locals>.connectionMade8  sG    w'++-)99:

72;'!!$= r(   s   stdio_test_producerc                   > TR                  TR                  S   SR                  T5      5        TR                  TS[	        T5      4-  5        U R                  [        R                  5        g )Nrf   r(   z*Connection lost with %d writes left to go.)r   r"   joinassertFalselenrk   r   rl   )r5   rp   r$   r   r   s    r%   r6   <StandardInputOutputTests.test_producer.<locals>.processEndedB  sW    QVVAY(9:EWW KK))*r(   )r   r!   rM   r   rU   r    r   rb   )r$   r1   r6   r+   rp   r   r   r   s   `  @@@@@r%   test_producer&StandardInputOutputTests.test_producer-  sw    
 *+NNuSz"	> 	> !!!%;<	"">2	+ 	+ ##A44r(   c                   ^ ^^ [        5       mTR                  nT R                  5       mT R                  TST5        UUU 4S jnT R	                  X5      $ )zh
Verify that the transport of a protocol connected to L{StandardIO}
is a working L{IConsumer} provider.
s   stdio_test_consumerc                   > [        TS5       nTR                  TR                  S   UR                  5       5        S S S 5        U R	                  [
        R                  5        g ! , (       d  f       N.= f)Nrbrf   )rg   r   r"   readrk   r   rl   )r5   rm   r   rp   r$   s     r%   r6   <StandardInputOutputTests.test_consumer.<locals>.processEndedW  sM    h%  AFFH5 &KK))* &%s   .A$$
A2)r   r!   r   rU   rb   )r$   r1   r6   r   rp   s   `  @@r%   test_consumer&StandardInputOutputTests.test_consumerK  sM    
 *+NN>>#14h?	+
 ##A44r(   zpStandardIO does not accept stdout as an argument to Windows.  Testing redirection to a file is therefore harder.c                  ^ ^^	^
^^ [         R                  " 5       n[        U5      n[        R                  " T R                  5       5      mTR                  S5      =T l        nT R                  UR                  5        [        UR                  5       S9n[        R                  " 5       (       d[  [        R                  " 5       u  pVT R                  [        R                  U5        T R                  [        R                  U5        XTS'   [         R"                  " U40 UD6mSm
[$        R&                  " 5       m	UU	U
U4S jm[(        R*                  " ST5        U	U
UU 4S jnUR-                  U5        U$ )a  
If L{StandardIO} is created with a file descriptor which refers to a
normal file (ie, a file from the filesystem), L{StandardIO.write}
writes bytes to that file.  In particular, it does not immediately
consider the file closed or call its protocol's C{connectionLost}
method.
r   )stdoutstdin   c                    > T H0  n U T:X  a  TR                  5           g TR                  SU 4-  5          O   [        R                  " ST5        g )N   %dr   )loseConnectionr   r
   r   )value
connectioncounthowManyspins    r%   r   AStandardInputOutputTests.test_normalFileStandardOut.<locals>.spin  sN    G#--/  %!12  a&r(   r   c           	        > TR                  [        T5      TS-   5        TR                  TR                  5       SR                  S [	        T5       5       5      5        g )Nrf   r(   c              3  .   #    U  H  nS U4-  v   M     g7f)r   NrA   ).0r   s     r%   	<genexpr>VStandardInputOutputTests.test_normalFileStandardOut.<locals>.cbLost.<locals>.<genexpr>  s     +Q.QEQDL.s   )r   next
getContentr   r   )r5   r   r   pathr$   s    r%   cbLostCStandardInputOutputTests.test_normalFileStandardOut.<locals>.cbLost  sH    T%['A+6!388+Q%.+Q#Qr(   )r   r   r   r   FilePathrr   rg   normal
addCleanupclosedictfilenor   	isWindowsospiper   
StandardIO	itertoolsr   r
   r   r   )r$   
onConnLostrP   r   kwargsrwr   r   r   r   r   r   s   `       @@@@@r%   test_normalFileStandardOut3StandardInputOutputTests.test_normalFileStandardOut^  s    ^^%
/
;  /#yy.f%V]]_-!!## 779DAOOBHHa(OOBHHa(7O%%e6v6
 !	' 	' 	!T"	 	 	v&r(   )r   )
rP   r   rQ   zstr | bytesrR   r   rS   r   r8   r   )r1   r   r*   zCallable[[Failure], object]r8   r   )F)r   r   rz   boolr8   r   )r8   r   )r:   r;   r<   r=   r   r   r   skiprU   rb   rs   r   r   r   r   r   r   r   r   r   r   r   r   r  r@   rA   r(   r%   rC   rC   S   s    n = E' 	

$
/:
CF
NQ
	
@	&%	&1L	&		&5. ).!%	2JP
%BN5"5 5 5<5& 	=
3
3r(   rC   )-r>   
__future__r   r   r   rJ   typingr   r   unittestr   twisted.internetr   r   r	   r
   r   twisted.internet.interfacesr   r   twisted.internet.protocolr   twisted.pythonr   r   twisted.python.failurer   twisted.python.reflectr   twisted.python.runtimer   twisted.test.test_tcpr   twisted.trial.unittestr   r   r   r   environrO   pathsepr   r   r   rC   rA   r(   r%   <module>r     s    #  	 
    C C J 5 ( * 0 + A 5 : 	**//#((3	, )+H$<$< )+XCx Cr(   