+
    Dfj8                       R t ^ RIHt ^ RIt^ RIt^ RIt^ RIHtHt ^ RI	H
t
 ^ RIHtHtHtHtHt ^ RIHtHt ^ RIHt ^ RIHtHt ^ R	IHt ^ R
IHt ^ RIHt ^ RIH t  ^ RI!H"t"H#t# Rt$]%! ]PL                  4      t']PP                  PS                  ]PT                  4      ]'R&    ! R R]P*                  4      t+ ! R R]#4      t,R# )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                  R    ] tR t^'t$ RtRtR]R&   R]R&   R R ltR	 tR
 t	R t
RtR# )StandardIOTestProcessProtocola  
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                   V ^8  d   QhRR/# )   returnNone )formats   "9/usr/lib/python3/dist-packages/twisted/test/test_stdio.py__annotate__*StandardIOTestProcessProtocol.__annotate__=   s     ) )$ )    c                	|    \         P                  ! 4       V n        \         P                  ! 4       V n        / V n        R # N)r   DeferredonConnectiononCompletiondataselfs   &r!   __init__&StandardIOTestProcessProtocol.__init__=   s'    27..2B27..2B&(	r$   c                	<    V P                   P                  R 4       R # r&   )r(   callbackr+   s   &r!   connectionMade,StandardIOTestProcessProtocol.connectionMadeB   s    ""4(r$   c                    V P                   P                  VR4      V,           V P                   V&   V P                  e'   V P                  Ruq0n        VP                  V 4       R# R# )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   r0   )r,   namebytesds   &&& r!   childDataReceived/StandardIOTestProcessProtocol.childDataReceivedE   sU    
 ))--c2U:		$*%)%8%8$"A"JJt +r$   c                	<    V P                   P                  V4       R # r&   )r)   r0   )r,   reasons   &&r!   processEnded*StandardIOTestProcessProtocol.processEndedO   s    ""6*r$   )r*   r)   r(   r   )__name__
__module____qualname____firstlineno____doc__r   __annotations__r-   r1   r8   r<   __static_attributes__r   r$   r!   r   r   '   s.    $ 37N/6  )
)+r$   r   c                     ] tR t^St]P
                  ! 4       '       d   ]! R4      f   RtR R ltR R lt	R t
RR	 R
 lltR R ltR R ltR R ltR tR tR tR tR tR tR t]! ]P
                  ! 4       R4      R 4       tRtR# )StandardInputOutputTestswin32processNzIOn windows, spawnProcess is not available in the absence of win32process.c          
     ,    V ^8  d   QhRRRRRRRRR	R
/# )r   protor   siblingzstr | bytesargsstrkwr   r   r   r   )r    s   "r!   r"   %StandardInputOutputTests.__annotate__Z   s4     
 
$
/:
CF
NQ
	
r$   c                @   \        V\        4      '       d   VP                  4       p\        P                  RRV,           \
        P                  P                  .\        V4      ,           p\        \
        4      P                  ! V\        P                  V3R\        /VB # )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)
isinstancer6   decodesys
executabler	   	__class__r?   listr   spawnProcess	properEnv)r,   rI   rJ   rK   rM   procargss   &&&*, r!   _spawnProcess&StandardInputOutputTests._spawnProcessZ   s    ( gu%%nn&GNNg%((	

 J w'443>>8
1:
>@
 	
r$   c               $    V ^8  d   QhRRRRRR/# )r   r7   defer.Deferred[None]r0   zCallable[[Failure], object]r   r   )r    s   "r!   r"   rN   z   s$     	& 	&%	&1L	&		&r$   c                	@   a a V 3R  lpV3R lpVP                  W44      # )c                2   < SP                  R V : 24       R# )z%Process terminated with non-Failure: N)fail)resultr,   s   &r!   cb4StandardInputOutputTests._requireFailure.<locals>.cb}   s    II=fZHIr$   c                   < S! V 4      # r&   r   )errr0   s   &r!   eb4StandardInputOutputTests._requireFailure.<locals>.eb   s    C= r$   )addCallbacks)r,   r7   r0   rb   rf   s   f&f  r!   _requireFailure(StandardInputOutputTests._requireFailurez   s    	J	! ~~b%%r$   c                   a aa S P                  4       o\        P                  ! RS,           4       \        4       oSP                  pS P                  SRS4       VVV 3R lpS P                  W4      # )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                D  < \        S4      ;_uu_ 4       pV F.  p\        P                  ! R VP                  4       ,           4       K0  	  RRR4       SP	                  ^SP
                  4       V P                  \        P                  4       R#   + '       g   i     LM; i)zChild logged: N)	openr   msgrstripfailIfInr*   trapr   ProcessDone)r;   flineerrorLogFilepr,   s   &  r!   r<   BStandardInputOutputTests.test_loseConnection.<locals>.processEnded   sf    l##qDGG,t{{}<=  $ MM!QVV$KK))* $#s   5BB	)mktempr   rn   r   r)   rZ   ri   )r,   r7   r<   ru   rv   s   f  @@r!   test_loseConnection,StandardInputOutputTests.test_loseConnection   s^    
 {{}+l:;)+NN14lC	+ ##A44r$   c               $    V ^8  d   QhRRRRRR/# )r   examplerL   outboolr   r]   r   )r    s   "r!   r"   rN      s$      !%	r$   c                	  aa V P                  4       p\        4       o\        P                  ! 4       Sn        R  VV3R llpSP                  P                  V4       R pV P                  SP                  V4      pV P                  SW4       V# )c                    V ^8  d   QhRRRR/# )r   ignoredr   r   r]   r   )r    s   "r!   r"   LStandardInputOutputTests.exampleOutputsAndZeroExitCode.<locals>.__annotate__   s     	 	T 	&: 	r$   c                   < SP                   pS'       d   SP                  P                  4        V# SP                  P                  4        V# r&   )r)   r   closeStdout
closeStdin)r   r7   r}   rv   s   & r!   cbBytesGStandardInputOutputTests.exampleOutputsAndZeroExitCode.<locals>.cbBytes   s=    A'') H &&(Hr$   c                D    V P                  \        P                  4       R # r&   )rq   r   rr   )r;   s   &r!   r<   LStandardInputOutputTests.exampleOutputsAndZeroExitCode.<locals>.processEnded   s    KK))*r$   )rx   r   r   r'   r   addCallbackri   rZ   )r,   r|   r}   ru   r   r<   r7   rv   s   &&f    @r!   exampleOutputsAndZeroExitCode6StandardInputOutputTests.exampleOutputsAndZeroExitCode   s{     {{})+ >>+	 	 	
$$W-	+   !1!1<@1g4r$   c                   V ^8  d   QhRR/# r   r   r]   r   )r    s   "r!   r"   rN      s     J J)= Jr$   c                $    V P                  R4      # )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                   V ^8  d   QhRR/# r   r   )r    s   "r!   r"   rN      s     P P.B Pr$   c                $    V P                  R4      # )
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                   V ^8  d   QhRR/# r   r   )r    s   "r!   r"   rN      s     
 
/C 
r$   c                (    V P                  RRR7      # )r    stdio_test_halfclose_buggy_writeT)r}   r   r+   s   &r!   test_buggyWriteConnectionLost6StandardInputOutputTests.test_buggyWriteConnectionLost   s!     11.D 2 
 	
r$   c                   a a \        4       o S P                  SR\        RR7       TT 3R lpS P                  SP                  T4      #   \         d   p\	        \        T4      4      hRp?ii ; i)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                   < SP                  SP                  ^,          P                  \        4      RSP                  : R24       V P	                  \
        P                  4       R# )ze
Asserts that the parent received the bytes written by the child
immediately after the child starts.
z	Received z) from child, did not find expected bytes.N)
assertTruer*   endswithUNIQUE_LAST_WRITE_STRINGrq   r   rr   r;   rv   r,   s   &r!   r<   EStandardInputOutputTests.test_lastWriteReceived.<locals>.processEnded   sM    
 OOq	""#;<AFF:%NO KK))*r$   )r   rZ   r   
ValueErrorr   rL   ri   r)   )r,   er<   rv   s   f  @r!   test_lastWriteReceived/StandardInputOutputTests.test_lastWriteReceived   so     *+	#*,DT  		+ ##ANNLAA  	#3q6""	#s   A
 
A.A))A.c                   a a \        4       oSP                  pS P                  SR4       VV 3R lpS P                  W4      # )zk
Verify that the transport of a protocol connected to L{StandardIO}
has C{getHost} and C{getPeer} methods.
s   stdio_test_hostpeerc                   < SP                   ^,          P                  4       w  rSP                  V4       SP                  V4       V P                  \        P
                  4       R# )   N)r*   
splitlinesr   rq   r   rr   )r;   hostpeerrv   r,   s   &  r!   r<   ?StandardInputOutputTests.test_hostAndPeer.<locals>.processEnded   sE    --/JDOOD!OOD!KK))*r$   r   r)   rZ   ri   r,   r7   r<   rv   s   f  @r!   test_hostAndPeer)StandardInputOutputTests.test_hostAndPeer   s?    
 *+NN145	+ ##A44r$   c                   a a \        4       oSP                  pS P                  SR4       VV 3R lpS P                  W4      # )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                   < SP                  SP                  ^,          R4       V P                  \        P                  4       R# r   s   ok!NassertEqualr*   rq   r   rr   r   s   &r!   r<   9StandardInputOutputTests.test_write.<locals>.processEnded  -    QVVAY/KK))*r$   r   r   s   f  @r!   
test_write#StandardInputOutputTests.test_write  s?    
 *+NN112	+ ##A44r$   c                   a a \        4       oSP                  pS P                  SR4       VV 3R lpS P                  W4      # )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                   < SP                  SP                  ^,          R4       V P                  \        P                  4       R# r   r   r   s   &r!   r<   AStandardInputOutputTests.test_writeSequence.<locals>.processEnded   r   r$   r   r   s   f  @r!   test_writeSequence+StandardInputOutputTests.test_writeSequence  s?    
 *+NN145	+ ##A44r$   c                	    V P                  4       p\        VR 4      ;_uu_ 4       p\        R4       F  pVP                  RV3,          4       K  	  RRR4       V#   + '       g   i     T# ; i)wbi      %d
N)rx   rm   rangewrite)r,   junkPathjunkFileis   &   r!   	_junkPath"StandardInputOutputTests._junkPath&  sX    ;;=(D!!X4[w!~. ! "  "! s   ,AA,	c                  a aaaaa \        4       oSP                  p. o\        \        ^d4      4      oVVVV3R loS P	                  SR4      oSP
                  P                  S4       VV VV3R lpS P                  W4      # )zh
Verify that the transport of a protocol connected to L{StandardIO}
is a working L{IProducer} provider.
c                   < S'       dZ   SP                  R SP                  4       3,          4       SP                  SR,          4       \        P                  ! RSR4       R# R# )r   g{Gz?N)appendpopr   r	   	callLater)ignr1   proctoWritewrittens   &r!   r1   >StandardInputOutputTests.test_producer.<locals>.connectionMade8  sG    w'++-)99:

72;'!!$= r$   s   stdio_test_producerc                   < SP                  SP                  ^,          RP                  S4      4       SP                  SR\	        S4      3,          4       V P                  \        P                  4       R# )r   r$   z*Connection lost with %d writes left to go.N)r   r*   joinassertFalselenrq   r   rr   )r;   rv   r,   r   r   s   &r!   r<   <StandardInputOutputTests.test_producer.<locals>.processEndedB  sW    QVVAY(9:EWW KK))*r$   )r   r)   rV   r   rZ   r(   r   ri   )r,   r7   r<   r1   rv   r   r   r   s   f  @@@@@r!   test_producer&StandardInputOutputTests.test_producer-  sw    
 *+NNuSz"	> 	> !!!%;<	"">2	+ 	+ ##A44r$   c                   a aa \        4       oSP                  pS P                  4       oS P                  SRS4       VVV 3R lpS P	                  W4      # )zh
Verify that the transport of a protocol connected to L{StandardIO}
is a working L{IConsumer} provider.
s   stdio_test_consumerc                  < \        SR 4      ;_uu_ 4       pSP                  SP                  ^,          VP                  4       4       RRR4       V P	                  \
        P                  4       R#   + '       g   i     L1; i)rbN)rm   r   r*   readrq   r   rr   )r;   rs   r   rv   r,   s   & r!   r<   <StandardInputOutputTests.test_consumer.<locals>.processEndedW  sP    h%%  AFFH5 &KK))* &%s   2A11B	)r   r)   r   rZ   ri   )r,   r7   r<   r   rv   s   f  @@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                  a aa	a
aa \         P                  ! 4       p\        V4      p\        P                  ! S P                  4       4      oSP                  R4      ;S n        pS P                  VP                  4       \        VP                  4       R7      p\        P                  ! 4       '       g\   \        P                  ! 4       w  rVS P                  \        P                  V4       S P                  \        P                  V4       WTR&   \         P"                  ! V3/ VB o^o
\$        P&                  ! 4       o	VV	V
V3R lo\(        P*                  ! ^ S4       V	V
VV 3R lpVP-                  V4       V# )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stdinc                    < S F6  p V S8X  d   SP                  4         R # SP                  RV 3,          4        M	  \        P                  ! ^ S4       R # )N   %d)loseConnectionr   r	   r   )value
connectioncounthowManyspins    r!   r   AStandardInputOutputTests.test_normalFileStandardOut.<locals>.spin  sN    G#--/  %!12  a&r$   c           	        < SP                  \        S4      S^,           4       SP                  SP                  4       RP                  R \	        S4       4       4      4       R# )r   r$   c              3  6   "   T F  pR V3,          x  K  	  R# 5i)r   Nr   ).0r   s   & r!   	<genexpr>VStandardInputOutputTests.test_normalFileStandardOut.<locals>.cbLost.<locals>.<genexpr>  s     +Q.QEQDLL.s   N)r   next
getContentr   r   )r;   r   r   pathr,   s   &r!   cbLostCStandardInputOutputTests.test_normalFileStandardOut.<locals>.cbLost  sH    T%['A+6!388+Q%.+Q#Qr$   )r   r'   r   r   FilePathrx   rm   normal
addCleanupclosedictfilenor   	isWindowsospiper
   
StandardIO	itertoolsr   r	   r   r   )r,   
onConnLostrI   r   kwargsrwr   r   r   r   r   r   s   f       @@@@@r!   test_normalFileStandardOut3StandardInputOutputTests.test_normalFileStandardOut^  s    ^^%
/
;  /#yy.f%V]]_-!!## 779DAOOBHHa(OOBHHa(7O%%e6v6
 !	' 	' 	!T"	 	 	v&r$   )r   )F)r>   r?   r@   rA   r   r  r   skiprZ   ri   ry   r   r   r   r   r   r   r   r   r   r   r   r   r  rD   r   r$   r!   rF   rF   S   s    n = E' 	

@	&5,2JP
%BN5"5 5 5<5& 	=
3
3r$   rF   )-rB   
__future__r   r  r  rS   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  environrX   pathsepr   r   r   rF   r   r$   r!   <module>r     s    #  	 
    C C J 5 ( * 0 + A 5 : 	**//#((3	, )+H$<$< )+XCx Cr$   