o
    >h8                     @  s  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 dejZ+G d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                   @  sH   e Zd ZU dZdZded< ded< dd	d
Zdd Zdd Zd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                 C  s   t  | _t  | _i | _d S N)r   DeferredonConnectiononCompletiondataself r#   w/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/test/test_stdio.py__init__=   s   


z&StandardIOTestProcessProtocol.__init__c                 C  s   | j d  d S r   )r   callbackr!   r#   r#   r$   connectionMadeB      z,StandardIOTestProcessProtocol.connectionMadec                 C  sB   | j |d| | j |< | jdur| jd}| _||  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}.
            N)r    getr   r&   )r"   namebytesdr#   r#   r$   childDataReceivedE   s
   
z/StandardIOTestProcessProtocol.childDataReceivedc                 C  s   | j | d S r   )r   r&   )r"   reasonr#   r#   r$   processEndedO   r(   z*StandardIOTestProcessProtocol.processEnded)r   r   )
__name__
__module____qualname____doc__r   __annotations__r%   r'   r.   r0   r#   r#   r#   r$   r   '   s   
 

r   c                   @  s   e Zd Ze reddu rdZd5ddZd6ddZdd Z		d7d8ddZ
d9ddZd9d d!Zd9d"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zee d2d3d4 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                 O  sP   t |tr	| }tjdd| tjjgt| }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__r2   listr   spawnProcess	properEnv)r"   r8   r9   r;   r=   procargsr#   r#   r$   _spawnProcessZ   s    
z&StandardInputOutputTests._spawnProcessr-   defer.Deferred[None]r&   Callable[[Failure], object]c                   s$   fdd} fdd}| ||S )Nc                   s     d|  d S )Nz%Process terminated with non-Failure: )fail)resultr!   r#   r$   cb}   s   z4StandardInputOutputTests._requireFailure.<locals>.cbc                   s    | S r   r#   )err)r&   r#   r$   eb   s   z4StandardInputOutputTests._requireFailure.<locals>.eb)addCallbacks)r"   r-   r&   rM   rO   r#   )r&   r"   r$   _requireFailurez   s   z(StandardInputOutputTests._requireFailurec                   sL      td   t j}d   fd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                   sb   t  }|D ]}td|   qW d    n1 sw   Y  dj | tj d S )NzChild logged:    )	openr   msgrstripfailIfInr    trapr   ProcessDone)r/   flineerrorLogFilepr"   r#   r$   r0      s   
zBStandardInputOutputTests.test_loseConnection.<locals>.processEnded)mktempr   rT   r   r   rH   rQ   r"   r-   r0   r#   r[   r$   test_loseConnection   s   	z,StandardInputOutputTests.test_loseConnectionFexampleoutboolc                   s\   |   }t t _d	 fdd}j| dd }| j|}| || |S )
Nignoredr   r   rI   c                   s&   j } rj  |S j  |S r   )r   r   closeStdout
closeStdin)rd   r-   rb   r]   r#   r$   cbBytes   s   

zGStandardInputOutputTests.exampleOutputsAndZeroExitCode.<locals>.cbBytesc                 S  s   |  tj d S r   )rW   r   rX   r/   r#   r#   r$   r0      r(   zLStandardInputOutputTests.exampleOutputsAndZeroExitCode.<locals>.processEnded)rd   r   r   rI   )r^   r   r   r   r   addCallbackrQ   rH   )r"   ra   rb   r\   rh   r0   r-   r#   rg   r$   exampleOutputsAndZeroExitCode   s   
z6StandardInputOutputTests.exampleOutputsAndZeroExitCodec                 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rk   r!   r#   r#   r$   test_readConnectionLost      
z0StandardInputOutputTests.test_readConnectionLostc                 C  rl   )
        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_buggyrn   r!   r#   r#   r$   test_buggyReadConnectionLost   rp   z5StandardInputOutputTests.test_buggyReadConnectionLostc                 C  s   | j dddS )rq    stdio_test_halfclose_buggy_writeT)rb   rn   r!   r#   r#   r$   test_buggyWriteConnectionLost   s   z6StandardInputOutputTests.test_buggyWriteConnectionLostc              
     s^   t   zj dtdd W n ty  } ztt|d}~ww  fd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                   s2     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.
            rR   z	Received z) from child, did not find expected bytes.N)
assertTruer    endswithUNIQUE_LAST_WRITE_STRINGrW   r   rX   ri   r]   r"   r#   r$   r0      s
   zEStandardInputOutputTests.test_lastWriteReceived.<locals>.processEnded)r   rH   ry   
ValueErrorr   r<   rQ   r   )r"   er0   r#   rz   r$   test_lastWriteReceived   s   
z/StandardInputOutputTests.test_lastWriteReceivedc                   2   t    j} d  fd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                   s6    j d  \}}| | | tj d S )NrR   )r    
splitlinesrw   rW   r   rX   )r/   hostpeerrz   r#   r$   r0      s   

z?StandardInputOutputTests.test_hostAndPeer.<locals>.processEndedr   r   rH   rQ   r_   r#   rz   r$   test_hostAndPeer   s
   z)StandardInputOutputTests.test_hostAndPeerc                   r~   )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 NrR   s   ok!assertEqualr    rW   r   rX   ri   rz   r#   r$   r0        z9StandardInputOutputTests.test_write.<locals>.processEndedr   r_   r#   rz   r$   
test_write  
   z#StandardInputOutputTests.test_writec                   r~   )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                   r   r   r   ri   rz   r#   r$   r0      r   zAStandardInputOutputTests.test_writeSequence.<locals>.processEndedr   r_   r#   rz   r$   test_writeSequence  r   z+StandardInputOutputTests.test_writeSequencec                 C  sV   |   }t|d}tdD ]
}|d|f  qW d    |S 1 s$w   Y  |S )Nwbi      %d
)r^   rS   rangewrite)r"   junkPathjunkFileir#   r#   r$   	_junkPath&  s   
z"StandardInputOutputTests._junkPathc                   sd   t  j}g ttd fdd dj  fdd}||S )z
        Verify that the transport of a protocol connected to L{StandardIO}
        is a working L{IProducer} provider.
        d   c                   s<   r d f  d  td d  d S d S )Nr   g{Gz?)appendpopr   r	   	callLater)ign)r'   proctoWritewrittenr#   r$   r'   8  s
   z>StandardInputOutputTests.test_producer.<locals>.connectionMades   stdio_test_producerc                   s>     jd d dtf  | tj d S )NrR   r)   z*Connection lost with %d writes left to go.)r   r    joinassertFalselenrW   r   rX   ri   )r]   r"   r   r   r#   r$   r0   B  s
   z<StandardInputOutputTests.test_producer.<locals>.processEnded)r   r   rD   r   rH   r   rj   rQ   r_   r#   )r'   r]   r   r"   r   r   r$   test_producer-  s   z&StandardInputOutputTests.test_producerc                   s>   t  j}  d   fdd}||S )z
        Verify that the transport of a protocol connected to L{StandardIO}
        is a working L{IConsumer} provider.
        s   stdio_test_consumerc                   sP   t  d}jd |  W d    n1 sw   Y  | tj d S )NrbrR   )rS   r   r    readrW   r   rX   )r/   rY   r   r]   r"   r#   r$   r0   W  s   z<StandardInputOutputTests.test_consumer.<locals>.processEnded)r   r   r   rH   rQ   r_   r#   r   r$   test_consumerK  s   z&StandardInputOutputTests.test_consumerzpStandardIO does not accept stdout as an argument to Windows.  Testing redirection to a file is therefore harder.c                   s   t  }t|}t d _}|j	 t
| d}t s@t \}}tj	| tj	| ||d< tj|fi | dt  fddtd fd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                    s@   D ]} | kr     d S  d| f   td d S )N   %dr   )loseConnectionr   r	   r   )value)
connectioncounthowManyspinr#   r$   r     s   zAStandardInputOutputTests.test_normalFileStandardOut.<locals>.spinr   c                   s<    t d    ddd tD  d S )NrR   r)   c                 s  s    | ]}d |f V  qdS )r   Nr#   ).0r   r#   r#   r$   	<genexpr>  s    zVStandardInputOutputTests.test_normalFileStandardOut.<locals>.cbLost.<locals>.<genexpr>)r   next
getContentr   r   ri   )r   r   pathr"   r#   r$   cbLost  s   zCStandardInputOutputTests.test_normalFileStandardOut.<locals>.cbLost)r   r   r   r   FilePathr^   rS   normal
addCleanupclosedictfilenor   	isWindowsospiper
   
StandardIO	itertoolsr   r	   r   rj   )r"   
onConnLostr8   r   kwargsrwr   r#   )r   r   r   r   r"   r   r$   test_normalFileStandardOut^  s&   	
z3StandardInputOutputTests.test_normalFileStandardOut)
r8   r   r9   r:   r;   r<   r=   r   r   r   )r-   rI   r&   rJ   r   rI   )F)ra   r<   rb   rc   r   rI   )r   rI   )r1   r2   r3   r   r   r   skiprH   rQ   r`   rk   ro   rs   ru   r}   r   r   r   r   r   r   r   r   r#   r#   r#   r$   r6   S   s0    

 



'r6   )-r4   
__future__r   r   r   rA   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   ry   r   environrF   pathsepr   r   r   r6   r#   r#   r#   r$   <module>   s*   
,