
    Yf8                    |   d Z ddlmZ ddlZddlZddlZddlmZm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mZ dd
l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j&                  Z'ej(        )                    ej*                  e'd<    G d dej                  Z+ G d de#          Z,dS )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                  F    e Zd ZU dZdZded<   ded<   dd	Zd
 Zd Zd Z	dS )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   	transportreturnNonec                t    t          j                    | _        t          j                    | _        i | _        d S N)r   DeferredonConnectiononCompletiondataselfs    I/var/www/html/env/lib/python3.11/site-packages/twisted/test/test_stdio.py__init__z&StandardIOTestProcessProtocol.__init__=   s-    27.2B2B27.2B2B&(			    c                :    | j                             d            d S r   )r!   callbackr$   s    r&   connectionMadez,StandardIOTestProcessProtocol.connectionMadeB   s    ""4(((((r(   c                    | j                             |d          |z   | j         |<   | j        &| j        dc}| _        |                    |            dS dS )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&   childDataReceivedz/StandardIOTestProcessProtocol.childDataReceivedE   s_    
 )--c22U:	$*%)%8$"At"JJt +*r(   c                :    | j                             |           d S r   )r"   r*   )r%   reasons     r&   processEndedz*StandardIOTestProcessProtocol.processEndedO   s    ""6*****r(   )r   r   )
__name__
__module____qualname____doc__r   __annotations__r'   r+   r1   r4    r(   r&   r   r   '   s          $ 37N6666    ) ) ) )
) ) )  + + + + +r(   r   c                      e Zd Z ej                    r ed          dZd&dZd'dZd Z		 d(d)dZ
d*dZd*dZd*dZd Zd Zd Zd  Zd! Zd" Zd# Z e ej                    d$          d%             ZdS )+StandardInputOutputTestswin32processNzIOn windows, spawnProcess is not available in the absence of win32process.protor   siblingstr | bytesargsstrkwr   r   r   c                   t          |t                    r|                                }t          j        dd|z   t
          j        j        gt          |          z   } t          t
                    j
        |t          j        |fdt          i|S )ak  
        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)
isinstancer/   decodesys
executabler
   	__class__r6   listr   spawnProcess	properEnv)r%   r>   r?   rA   rC   procargss         r&   _spawnProcessz&StandardInputOutputTests._spawnProcessZ   s    ( gu%% 	'nn&&GNg%(	

 JJ 5w''43>8
 
1:
>@
 
 	
r(   r0   defer.Deferred[None]r*   Callable[[Failure], object]c                F      fd}fd}|                     ||          S )Nc                8                         d|            d S )Nz%Process terminated with non-Failure: )fail)resultr%   s    r&   cbz4StandardInputOutputTests._requireFailure.<locals>.cb}   s$    IIHfHHIIIIIr(   c                     |           S r   r:   )errr*   s    r&   ebz4StandardInputOutputTests._requireFailure.<locals>.eb   s    8C== r(   )addCallbacks)r%   r0   r*   rV   rY   s   ` `  r&   _requireFailurez(StandardInputOutputTests._requireFailurez   sQ    	J 	J 	J 	J 	J	! 	! 	! 	! 	! ~~b"%%%r(   c                                                       t          j        dz              t                      j        }                     d            fd}                     ||          S )z
        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 }|D ]+}t          j        d|                                z              ,	 d d d            n# 1 swxY w Y                       dj                   |                     t          j                   d S )NzChild logged:    )	openr   msgrstripfailIfInr#   trapr   ProcessDone)r3   flineerrorLogFilepr%   s      r&   r4   zBStandardInputOutputTests.test_loseConnection.<locals>.processEnded   s    l## >q > >DG,t{{}}<====>> > > > > > > > > > > > > > > MM!QV$$$KK)*****s   /AAA)mktempr   r`   r   r"   rO   r[   )r%   r0   r4   rg   rh   s   `  @@r&   test_loseConnectionz,StandardInputOutputTests.test_loseConnection   s    
 {{}}+l:;;;)++N14lCCC	+ 	+ 	+ 	+ 	+ 	+ 	+ ##A|444r(   Fexampleoutboolc                *   |                                  }t                      t          j                    _        dfd}j                            |           d }|                     j        |          }|                     ||           |S )Nignoredr   r   rP   c                    j         }rj                                         nj                                         |S r   )r"   r   closeStdout
closeStdin)ro   r0   rl   rh   s     r&   cbByteszGStandardInputOutputTests.exampleOutputsAndZeroExitCode.<locals>.cbBytes   sB    A )''))))&&(((Hr(   c                D    |                      t          j                   d S r   )rc   r   rd   )r3   s    r&   r4   zLStandardInputOutputTests.exampleOutputsAndZeroExitCode.<locals>.processEnded   s    KK)*****r(   )ro   r   r   rP   )ri   r   r   r    r   addCallbackr[   rO   )r%   rk   rl   rg   rs   r4   r0   rh   s     `    @r&   exampleOutputsAndZeroExitCodez6StandardInputOutputTests.exampleOutputsAndZeroExitCode   s     {{}})++ >++	 	 	 	 	 	 	 	
$$W---	+ 	+ 	+   !1<@@1g|444r(   c                ,    |                      d          S )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rv   r$   s    r&   test_readConnectionLostz0StandardInputOutputTests.test_readConnectionLost   s     112HIIIr(   c                ,    |                      d          S )
        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_buggyry   r$   s    r&   test_buggyReadConnectionLostz5StandardInputOutputTests.test_buggyReadConnectionLost   s     112NOOOr(   c                0    |                      dd          S )r|    stdio_test_halfclose_buggy_writeT)rl   ry   r$   s    r&   test_buggyWriteConnectionLostz6StandardInputOutputTests.test_buggyWriteConnectionLost   s&     11.D 2 
 
 	
r(   c                     t                      	                      dt          d           n.# t          $ r!}t	          t          |                    d}~ww xY w fd}                     j        |          S )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        d                             t                    dj        d           |                     t
          j                   dS )z
            Asserts that the parent received the bytes written by the child
            immediately after the child starts.
            r^   z	Received z) from child, did not find expected bytes.N)
assertTruer#   endswithUNIQUE_LAST_WRITE_STRINGrc   r   rd   r3   rh   r%   s    r&   r4   zEStandardInputOutputTests.test_lastWriteReceived.<locals>.processEnded   s`    
 OOq	""#;<<OAFOOO   KK)*****r(   )r   rO   r   
ValueErrorr   rB   r[   r"   )r%   er4   rh   s   `  @r&   test_lastWriteReceivedz/StandardInputOutputTests.test_lastWriteReceived   s     *++	#*,DT       	# 	# 	#3q66"""	#		+ 		+ 		+ 		+ 		+ 		+ ##ANLAAAs   1 
AAAc                     t                      j        }                     d            fd}                     ||          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                                         \  }}                    |                               |           |                     t          j                   d S )Nr^   )r#   
splitlinesr   rc   r   rd   )r3   hostpeerrh   r%   s      r&   r4   z?StandardInputOutputTests.test_hostAndPeer.<locals>.processEnded   s]    --//JD$OOD!!!OOD!!!KK)*****r(   r   r"   rO   r[   r%   r0   r4   rh   s   `  @r&   test_hostAndPeerz)StandardInputOutputTests.test_hostAndPeer   se    
 *++N14555	+ 	+ 	+ 	+ 	+ 	+ ##A|444r(   c                     t                      j        }                     d            fd}                     ||          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        d         d           |                     t          j                   d S Nr^   s   ok!assertEqualr#   rc   r   rd   r   s    r&   r4   z9StandardInputOutputTests.test_write.<locals>.processEnded  :    QVAY///KK)*****r(   r   r   s   `  @r&   
test_writez#StandardInputOutputTests.test_write  se    
 *++N11222	+ 	+ 	+ 	+ 	+ 	+ ##A|444r(   c                     t                      j        }                     d            fd}                     ||          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        d         d           |                     t          j                   d S r   r   r   s    r&   r4   zAStandardInputOutputTests.test_writeSequence.<locals>.processEnded   r   r(   r   r   s   `  @r&   test_writeSequencez+StandardInputOutputTests.test_writeSequence  se    
 *++N14555	+ 	+ 	+ 	+ 	+ 	+ ##A|444r(   c                    |                                  }t          |d          5 }t          d          D ]}|                    d|fz             	 d d d            n# 1 swxY w Y   |S )Nwbi      %d
)ri   r_   rangewrite)r%   junkPathjunkFileis       r&   	_junkPathz"StandardInputOutputTests._junkPath&  s    ;;==(D!! 	/X4[[ / /w!~..../	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ s   ,AA"%A"c                     t                      j        }g t          t          d                    fd                     d          j                                        fd}                     ||          S )z
        Verify that the transport of a protocol connected to L{StandardIO}
        is a working L{IProducer} provider.
        d   c                    r^                     d                                fz                                 d                    t          j        dd            d S d S )Nr   g{Gz?)appendpopr   r
   	callLater)ignr+   proctoWritewrittens    r&   r+   z>StandardInputOutputTests.test_producer.<locals>.connectionMade8  sh     >w'++--)99:::

72;'''!$=====> >r(   s   stdio_test_producerc                                         j        d         d                                                             dt	                    fz             |                     t          j                   d S )Nr^   r(   z*Connection lost with %d writes left to go.)r   r#   joinassertFalselenrc   r   rd   )r3   rh   r%   r   r   s    r&   r4   z<StandardInputOutputTests.test_producer.<locals>.processEndedB  ss    QVAY(9(9:::EWW   KK)*****r(   )r   r"   rK   r   rO   r!   ru   r[   )r%   r0   r4   r+   rh   r   r   r   s   `  @@@@@r&   test_producerz&StandardInputOutputTests.test_producer-  s    
 *++NuSzz""	> 	> 	> 	> 	> 	> 	> 	> !!!%;<<	"">222	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ ##A|444r(   c                     t                      j        }                                                      d            fd}                     ||          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        d         |                                           d d d            n# 1 swxY w Y   |                     t
          j                   d S )Nrbr^   )r_   r   r#   readrc   r   rd   )r3   re   r   rh   r%   s     r&   r4   z<StandardInputOutputTests.test_consumer.<locals>.processEndedW  s    h%% 6  AFFHH5556 6 6 6 6 6 6 6 6 6 6 6 6 6 6KK)*****s   4AAA)r   r"   r   rO   r[   )r%   r0   r4   r   rh   s   `  @@r&   test_consumerz&StandardInputOutputTests.test_consumerK  s|    
 *++N>>##14h???	+ 	+ 	+ 	+ 	+ 	+ 	+
 ##A|4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                                                                       d          x _        }                     |j	                   t          |                                          }t          j                    s[t          j                    \  }}                     t          j	        |                                t          j	        |           ||d<   t!          j        |fi |d
t%          j                    		
fdt)          j        d           	
 fd}|                    |           |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 ]8} | k    r                                   d S                     d| fz              t          j        d           d S )N   %dr   )loseConnectionr   r
   r   )value
connectioncounthowManyspins    r&   r   zAStandardInputOutputTests.test_normalFileStandardOut.<locals>.spin  sm      G##--///FF  %!1222a&&&&&r(   r   c           	                              t                    dz                                                                   d                    d t	                    D                                  d S )Nr^   r(   c              3  "   K   | ]
}d |fz  V  dS )r   Nr:   ).0r   s     r&   	<genexpr>zVStandardInputOutputTests.test_normalFileStandardOut.<locals>.cbLost.<locals>.<genexpr>  s(      +Q+QQEQDL+Q+Q+Q+Q+Q+Qr(   )r   next
getContentr   r   )r3   r   r   pathr%   s    r&   cbLostzCStandardInputOutputTests.test_normalFileStandardOut.<locals>.cbLost  su    T%[['A+666!!388+Q+Q%..+Q+Q+Q#Q#Q    r(   )r   r    r   r   FilePathri   r_   normal
addCleanupclosedictfilenor   	isWindowsospiper   
StandardIO	itertoolsr   r
   r   ru   )r%   
onConnLostr>   r   kwargsrwr   r   r   r   r   r   s   `       @@@@@r&   test_normalFileStandardOutz3StandardInputOutputTests.test_normalFileStandardOut^  s    ^%%
/
;; //#yy.f%%%V]]__---!## 	  799DAqOOBHa(((OOBHa(((F7O%e66v66
 !!	' 	' 	' 	' 	' 	' 	' 	' 	!T"""	 	 	 	 	 	 	 	 	v&&&r(   )
r>   r   r?   r@   rA   rB   rC   r   r   r   )r0   rP   r*   rQ   r   rP   )F)rk   rB   rl   rm   r   rP   )r   rP   )r5   r6   r7   r   r   r   skiprO   r[   rj   rv   rz   r~   r   r   r   r   r   r   r   r   r   r   r:   r(   r&   r<   r<   S   s       x 
n = = E' 	

 
 
 
@	& 	& 	& 	&5 5 5. ).    2J J J JP P P P
 
 
 
%B %B %BN5 5 5"5 5 5 5 5 5   5 5 5<5 5 5& V	= 
3 3 
3 3 3r(   r<   )-r8   
__future__r   r   r   rH   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   environrM   pathsepr   r   r   r<   r:   r(   r&   <module>r      s    # " " " " "     				 



                       C C C C C C C C C C C C C C J J J J J J J J 5 5 5 5 5 5 ( ( ( ( ( ( ( ( * * * * * * 0 0 0 0 0 0 + + + + + + A A A A A A 5 5 5 5 5 5 5 5 : D	*//#(33	, )+ )+ )+ )+ )+H$< )+ )+ )+XC C C C Cx C C C C Cr(   