
    Ϫf1                     D   d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZ ddlmZmZ ddlmZ ddlmZ ddlmZ dd	lmZmZ d
Z eej4                        Zej8                  j;                  ej<                        ed<    G d de	j>                        Z  G d de      Z!y)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.
    N)skipIf)defererrorprotocolreactorstdio)filepathlog)requireModule)platform)ConnectionLostNotifyingProtocol)SkipTestTestCases   xyz123abc Twisted is great!
PYTHONPATHc                   ,    e Zd ZdZdZd Zd Zd Zd Zy)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.
    Nc                 v    t        j                         | _        t        j                         | _        i | _        y N)r   DeferredonConnectiononCompletiondataselfs    9/usr/lib/python3/dist-packages/twisted/test/test_stdio.py__init__z&StandardIOTestProcessProtocol.__init__8   s'    !NN,!NN,	    c                 :    | j                   j                  d        y r   )r   callbackr   s    r   connectionMadez,StandardIOTestProcessProtocol.connectionMade=   s    ""4(r   c                     | j                   j                  |d      |z   | j                   |<   | j                  &| j                  dc}| _        |j                  |        yy)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   getonDataReceivedr   )r   namebytesds       r   childDataReceivedz/StandardIOTestProcessProtocol.childDataReceived@   sW    
 ))--c2U:		$*%)%8%8$"At"JJt +r   c                 :    | j                   j                  |       y r   )r   r   )r   reasons     r   processEndedz*StandardIOTestProcessProtocol.processEndedJ   s    ""6*r   )	__name__
__module____qualname____doc__r#   r   r    r'   r*    r   r   r   r   #   s"    $ N
)+r   r   c                       e Zd Z ej                         r
 ed      dZd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Z e ej                         d      d        Zy)StandardInputOutputTestswin32processNzIOn windows, spawnProcess is not available in the absence of win32process.c                     t         j                  dd|z   t        j                  j                  gt        |      z   }t        j                  |t         j                  |fdt        i|S )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.
        s   -ms   twisted.test.env)sys
executabler   	__class__r,   listspawnProcess	properEnv)r   protosiblingargskws        r   _spawnProcessz&StandardInputOutputTests._spawnProcessU   s`    & NNw&((	

 J ##E3>>4UYURTUUr   c                 >      fd}fd}|j                  ||      S )Nc                 .    j                  d|        y )Nz%Process terminated with non-Failure: )fail)resultr   s    r   cbz4StandardInputOutputTests._requireFailure.<locals>.cbp   s    II=fZHIr   c                      |       S r   r/   )errr   s    r   ebz4StandardInputOutputTests._requireFailure.<locals>.ebs   s    C= r   )addCallbacks)r   r&   r   rD   rG   s   ` `  r   _requireFailurez(StandardInputOutputTests._requireFailureo   s!    	J	! ~~b"%%r   c                       j                         t        j                  dz          t               j                  } j                  d        fd} j                  ||      S )z
        Verify that a protocol connected to L{StandardIO} can disconnect
        itself using C{transport.loseConnection}.
        Child process logging to s   stdio_test_loseconnc                    t              5 }|D ](  }t        j                  d|j                         z          * 	 d d d        j	                  dj
                         | j                  t        j                         y # 1 sw Y   ExY w)NzChild logged:    )	openr
   msgrstripfailIfInr   trapr   ProcessDone)r)   flineerrorLogFilepr   s      r   r*   zBStandardInputOutputTests.test_loseConnection.<locals>.processEnded   so    l# >q >DGG,t{{}<=>> MM!QVV$KK))*> >s   .B  B	)mktempr
   rO   r   r   r?   rI   )r   r&   r*   rV   rW   s   `  @@r   test_loseConnectionz,StandardInputOutputTests.test_loseConnectionx   s`    
 {{}+l:;)+NN14lC	+ ##A|44r   c                 B   | j                         }t        j                  d|z          t               t	        j
                         _        fd}j                  j                  |       d }| j                  j                  |      }| j                  d|       |S )z
        When stdin is closed and the protocol connected to it implements
        L{IHalfCloseableProtocol}, the protocol's C{readConnectionLost} method
        is called.
        rK   c                 T    j                   }j                  j                          |S r   )r   	transport
closeStdin)ignoredr&   rW   s     r   cbByteszAStandardInputOutputTests.test_readConnectionLost.<locals>.cbBytes   s!    AKK""$Hr   c                 B    | j                  t        j                         y r   )rR   r   rS   )r)   s    r   r*   zFStandardInputOutputTests.test_readConnectionLost.<locals>.processEnded   s    KK))*r   s   stdio_test_halfclose)
rX   r
   rO   r   r   r   r#   addCallbackrI   r?   )r   rV   r_   r*   r&   rW   s        @r   test_readConnectionLostz0StandardInputOutputTests.test_readConnectionLost   s     {{}+l:;)+ >>+	
 	
$$W-	+   !1!1<@15|Dr   c                      t               	  j                  dt        d        fd} j                  j                  |      S # t        $ r}t	        t        |            d}~ww xY w)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                     j                  j                  d   j                  t              dj                  d       | j	                  t
        j                         y)z
            Asserts that the parent received the bytes written by the child
            immediately after the child starts.
            rM   z	Received z) from child, did not find expected bytes.N)
assertTruer   endswithUNIQUE_LAST_WRITE_STRINGrR   r   rS   r)   rW   r   s    r   r*   zEStandardInputOutputTests.test_lastWriteReceived.<locals>.processEnded   sM    
 OOq	""#;<AFF:%NO KK))*r   )r   r?   rh   
ValueErrorr   strrI   r   )r   er*   rW   s   `  @r   test_lastWriteReceivedz/StandardInputOutputTests.test_lastWriteReceived   so     *+	#*,DT  		+ ##ANNLAA  	#3q6""	#s   A	 		A+A&&A+c                      t               j                  } j                  d        fd} j                  ||      S )z
        Verify that the transport of a protocol connected to L{StandardIO}
        has C{getHost} and C{getPeer} methods.
        s   stdio_test_hostpeerc                     j                   d   j                         \  }}j                  |       j                  |       | j                  t        j
                         y )NrM   )r   
splitlinesrf   rR   r   rS   )r)   hostpeerrW   r   s      r   r*   z?StandardInputOutputTests.test_hostAndPeer.<locals>.processEnded   sG    --/JD$OOD!OOD!KK))*r   r   r   r?   rI   r   r&   r*   rW   s   `  @r   test_hostAndPeerz)StandardInputOutputTests.test_hostAndPeer   sA    
 *+NN145	+ ##A|44r   c                      t               j                  } j                  d        fd} j                  ||      S )z
        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                     j                  j                  d   d       | j                  t        j                         y NrM   s   ok!assertEqualr   rR   r   rS   ri   s    r   r*   z9StandardInputOutputTests.test_write.<locals>.processEnded   -    QVVAY/KK))*r   rs   rt   s   `  @r   
test_writez#StandardInputOutputTests.test_write   sA    
 *+NN112	+ ##A|44r   c                      t               j                  } j                  d        fd} j                  ||      S )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                     j                  j                  d   d       | j                  t        j                         y rx   ry   ri   s    r   r*   zAStandardInputOutputTests.test_writeSequence.<locals>.processEnded   r{   r   rs   rt   s   `  @r   test_writeSequencez+StandardInputOutputTests.test_writeSequence   sA    
 *+NN145	+ ##A|44r   c                     | j                         }t        |d      5 }t        d      D ]  }|j                  d|fz          	 d d d        |S # 1 sw Y   |S xY w)Nwbi      %d
)rX   rN   rangewrite)r   junkPathjunkFileis       r   	_junkPathz"StandardInputOutputTests._junkPath   s\    ;;=(D! 	/X4[ /w!~./	/ 	/ s   &AAc                     t               j                  }g t        t        d            fd j	                  d      j
                  j                          fd} j                  ||      S )z
        Verify that the transport of a protocol connected to L{StandardIO}
        is a working L{IProducer} provider.
        d   c                     rOj                  dj                         fz         j                  d          t        j                  dd        y y )Nr   g{Gz?)appendpopr   r   	callLater)ignr    proctoWritewrittens    r   r    z>StandardInputOutputTests.test_producer.<locals>.connectionMade  sG    w'++-)99:

72;'!!$= r   s   stdio_test_producerc                     j                  j                  d   dj                               j                  dt	              fz         | j                  t        j                         y )NrM   r   z*Connection lost with %d writes left to go.)rz   r   joinassertFalselenrR   r   rS   )r)   rW   r   r   r   s    r   r*   z<StandardInputOutputTests.test_producer.<locals>.processEnded  sW    QVVAY(9:EWW KK))*r   )r   r   r8   r   r?   r   ra   rI   )r   r&   r*   r    rW   r   r   r   s   `  @@@@@r   test_producerz&StandardInputOutputTests.test_producer  so    
 *+NNuSz"	> !!!%;<	"">2	+ ##A|44r   c                      t               j                  } j                          j                  d        fd} j	                  ||      S )z
        Verify that the transport of a protocol connected to L{StandardIO}
        is a working L{IConsumer} provider.
        s   stdio_test_consumerc                     t        d      5 }j                  j                  d   |j                                d d d        | j	                  t
        j                         y # 1 sw Y   )xY w)NrbrM   )rN   rz   r   readrR   r   rS   )r)   rT   r   rW   r   s     r   r*   z<StandardInputOutputTests.test_consumer.<locals>.processEnded1  sT    h% 6  AFFH56KK))*6 6s   .A$$A-)r   r   r   r?   rI   )r   r&   r*   r   rW   s   `  @@r   test_consumerz&StandardInputOutputTests.test_consumer%  sO    
 *+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                    	
 t        j                         }t        |      }t        j                   j                               j                  d      x _        } j                  |j                         t        |j                               }t        j                         s\t        j                         \  }} j                  t        j                  |        j                  t        j                  |       ||d<   t!        j"                  |fi |d
t%        j&                         		
fdt)        j*                  d       	
 fd}|j-                  |       |S )aE  
        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                      D ]/  } | k(  rj                           y j                  d| fz          n t        j                  d       y )N   %dr   )loseConnectionr   r   r   )value
connectioncounthowManyspins    r   r   zAStandardInputOutputTests.test_normalFileStandardOut.<locals>.spin\  sS     G#--/  %!12 a&r   r   c           	          j                  t              dz          j                  j                         dj                  d t	              D                     y )NrM   r   c              3   (   K   | ]
  }d |fz    yw)r   Nr/   ).0r   s     r   	<genexpr>zVStandardInputOutputTests.test_normalFileStandardOut.<locals>.cbLost.<locals>.<genexpr>l  s     +QQEQDL+Qs   )rz   next
getContentr   r   )r)   r   r   pathr   s    r   cbLostzCStandardInputOutputTests.test_normalFileStandardOut.<locals>.cbLosti  sH    T%['A+6!388+Q%.+Q#Qr   )r   r   r   r	   FilePathrX   rN   normal
addCleanupclosedictfilenor   	isWindowsospiper   
StandardIO	itertoolsr   r   r   ra   )r   
onConnLostr;   r   kwargsrwr   r   r   r   r   r   s   `       @@@@@r   test_normalFileStandardOutz3StandardInputOutputTests.test_normalFileStandardOut8  s	    ^^%
/
;  /#yy.f%V]]_-!!# 779DAqOOBHHa(OOBHHa(F7O%%e6v6
 !	' 	!T"	 	v&r   )r+   r,   r-   r   r   r   skipr?   rI   rY   rb   rm   ru   r|   r   r   r   r   r   r   r/   r   r   r1   r1   N   s    xn = E' 	
V4&5,4%BN5"5 5 5<5& 	=
3
3r   r1   )"r.   r   r   r5   unittestr   twisted.internetr   r   r   r   r   twisted.pythonr	   r
   twisted.python.reflectr   twisted.python.runtimer   twisted.test.test_tcpr   twisted.trial.unittestr   r   rh   r   environr:   pathsepr   r   ProcessProtocolr   r1   r/   r   r   <module>r      s     	 
  C C ( 0 + A 5 : 	**//#((3	, (+H$<$< (+Vbx br   