Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib64/python3.7/site-packages/twisted/test/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib64/python3.7/site-packages/twisted/test/__pycache__/test_stdio.cpython-37.pyc

B

;��]d3�@s$dZddlmZmZddlZddlZddlZddlmZddl	m
Z
mZddlm
Z
ddlmZddlmZmZmZdd	lmZmZmZmZmZdd
lmZdZdZe��r�e
d�dkr�d
Zeej �Z!ej"�#ej$�e!d<n"e�Z!ej"�#ej$��%e�&��e!d<Gdd�dej'�Z(Gdd�dej)�Z*dS)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.
�)�absolute_import�divisionN)�unittest)�filepath�log)�
requireModule)�platform)�range�
intToBytes�bytesEnviron)�error�defer�protocol�stdio�reactor)�ConnectionLostNotifyingProtocolsxyz123abc Twisted is great!Zwin32processzIOn windows, spawnProcess is not available in the absence of win32process.�
PYTHONPATHs
PYTHONPATHc@s4eZdZdZdZdd�Zdd�Zdd�Zd	d
�ZdS)�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.
    NcCst��|_t��|_i|_dS)N)r
�Deferred�onConnection�onCompletion�data)�self�r�J/opt/alt/python37/lib64/python3.7/site-packages/twisted/test/test_stdio.py�__init__Bs

z&StandardIOTestProcessProtocol.__init__cCs|j�d�dS)N)r�callback)rrrr�connectionMadeHsz,StandardIOTestProcessProtocol.connectionMadecCs>|j�|d�||j|<|jdk	r:|jd}|_|�|�dS)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�get�onDataReceivedr)r�name�bytes�drrr�childDataReceivedLs
z/StandardIOTestProcessProtocol.childDataReceivedcCs|j�|�dS)N)rr)r�reasonrrr�processEndedWsz*StandardIOTestProcessProtocol.processEnded)	�__name__�
__module__�__qualname__�__doc__r rrr$r&rrrrr.src@s~eZdZeZdd�Zdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�Zdd�Zdd�Ze��rzde_dS)�StandardInputOutputTestscOs:tjdd|tjjgt|�}tj|tj|fdti|��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(�listZspawnProcess�	properEnv)r�protoZsibling�args�kwrrr�
_spawnProcess`sz&StandardInputOutputTests._spawnProcesscs$�fdd�}�fdd�}|�||�S)Ncs��d|f�dS)Nz'Process terminated with non-Failure: %r)Zfail)�result)rrr�cb~sz4StandardInputOutputTests._requireFailure.<locals>.cbcs�|�S)Nr)�err)rrr�eb�sz4StandardInputOutputTests._requireFailure.<locals>.eb)ZaddCallbacks)rr#rr7r9r)rrr�_requireFailure}sz(StandardInputOutputTests._requireFailurecsL����t�d��t���j}���d�����fdd�}��||�S)z�
        Verify that a protocol connected to L{StandardIO} can disconnect
        itself using C{transport.loseConnection}.
        zChild process logging to sstdio_test_loseconnc	sTt�d��&}x|D]}t�d|���qWWdQRX��d�j�|�tj�dS)N�rzChild logged: �)	�openr�msg�rstripZfailIfInr�trapr�ProcessDone)r%�f�line)�errorLogFile�prrrr&�s

 zBStandardInputOutputTests.test_loseConnection.<locals>.processEnded)�mktemprr>rrr5r:)rr#r&r)rDrErr�test_loseConnection�sz,StandardInputOutputTests.test_loseConnectioncsf|��}t�d|�t��t���_�fdd�}�j�|�dd�}|��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.
        zChild process logging to cs�j}�j��|S)N)rZ	transportZ
closeStdin)Zignoredr#)rErr�cbBytes�s
zAStandardInputOutputTests.test_readConnectionLost.<locals>.cbBytescSs|�tj�dS)N)r@rrA)r%rrrr&�szFStandardInputOutputTests.test_readConnectionLost.<locals>.processEndedsstdio_test_halfclose)
rFrr>rr
rr �addCallbackr:r5)rrDrHr&r#r)rEr�test_readConnectionLost�s

z0StandardInputOutputTests.test_readConnectionLostc
sjt��y�j�dtdd�Wn0tk
rL}zt�t|���Wdd}~XYnX��fdd�}���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.
        sstdio_test_lastwriteT)ZusePTYNcs0���jd�t�d�jf�|�tj�dS)z�
            Asserts that the parent received the bytes written by the child
            immediately after the child starts.
            r<z4Received %r from child, did not find expected bytes.N)�
assertTruer�endswith�UNIQUE_LAST_WRITE_STRINGr@rrA)r%)rErrrr&�s
zEStandardInputOutputTests.test_lastWriteReceived.<locals>.processEnded)	rr5rM�
ValueErrorrZSkipTest�strr:r)r�er&r)rErr�test_lastWriteReceived�s 
z/StandardInputOutputTests.test_lastWriteReceivedcs2t���j}���d���fdd�}��||�S)z�
        Verify that the transport of a protocol connected to L{StandardIO}
        has C{getHost} and C{getPeer} methods.
        sstdio_test_hostpeercs6�jd��\}}��|���|�|�tj�dS)Nr<)r�
splitlinesrKr@rrA)r%�hostZpeer)rErrrr&�s

z?StandardInputOutputTests.test_hostAndPeer.<locals>.processEnded)rrr5r:)rr#r&r)rErr�test_hostAndPeer�s
z)StandardInputOutputTests.test_hostAndPeercs2t���j}���d���fdd�}��||�S)z�
        Verify that the C{write} method of the transport of a protocol
        connected to L{StandardIO} sends bytes to standard out.
        sstdio_test_writecs"���jdd�|�tj�dS)Nr<sok!)�assertEqualrr@rrA)r%)rErrrr&�sz9StandardInputOutputTests.test_write.<locals>.processEnded)rrr5r:)rr#r&r)rErr�
test_write�s
z#StandardInputOutputTests.test_writecs2t���j}���d���fdd�}��||�S)z�
        Verify that the C{writeSequence} method of the transport of a
        protocol connected to L{StandardIO} sends bytes to standard out.
        sstdio_test_writeseqcs"���jdd�|�tj�dS)Nr<sok!)rUrr@rrA)r%)rErrrr&szAStandardInputOutputTests.test_writeSequence.<locals>.processEnded)rrr5r:)rr#r&r)rErr�test_writeSequence�s
z+StandardInputOutputTests.test_writeSequencec	CsF|��}t|d��*}x"td�D]}|�t|�d�qWWdQRX|S)N�wbi�
)rFr=r	�writer
)r�junkPathZjunkFile�irrr�	_junkPath
s
 z"StandardInputOutputTests._junkPathcsdt���j}g�ttd�������fdd�����d���j�������fdd�}��||�S)z�
        Verify that the transport of a protocol connected to L{StandardIO}
        is a working L{IProducer} provider.
        �dcs:�r6��t����d����d�t�d�d�dS)NrY���g{�G�z�?)�appendr
�poprZr�	callLater)Zign)r�proc�toWrite�writtenrrr sz>StandardInputOutputTests.test_producer.<locals>.connectionMadesstdio_test_producercs>���jdd�������dt��f�|�tj�dS)Nr<rz*Connection lost with %d writes left to go.)rUr�joinZassertFalse�lenr@rrA)r%)rErrdrerrr&*s
z<StandardInputOutputTests.test_producer.<locals>.processEnded)rrr0r	r5rrIr:)rr#r&r)rrErcrrdrer�
test_producersz&StandardInputOutputTests.test_producercs>t���j}�������d�����fdd�}��||�S)z�
        Verify that the transport of a protocol connected to L{StandardIO}
        is a working L{IConsumer} provider.
        sstdio_test_consumerc	s<t�d��}���jd|���WdQRX|�tj�dS)N�rbr<)r=rUr�readr@rrA)r%rB)r[rErrrr&?s z<StandardInputOutputTests.test_consumer.<locals>.processEnded)rrr]r5r:)rr#r&r)r[rErr�
test_consumer3sz&StandardInputOutputTests.test_consumercs�t��}t|�}t��������d��_}��|j	�t
|��d�}t�
�s�t��\}}��tj	|���tj	|�||d<tj|f|��d�t�������fdd��t�d������fdd	�}|�|�|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.
        rX)�stdout�stdin�csBx0�D](}|�kr���dS��t|��PqWt�d��dS)Nr)ZloseConnectionrZr
rrb)�value)�
connection�count�howMany�spinrrrses
zAStandardInputOutputTests.test_normalFileStandardOut.<locals>.spinrc	s8��t���d������d�ttt�����dS)Nr<r)rU�nextZ
getContentrf�mapr
r	)r%)rqrr�pathrrr�cbLostqszCStandardInputOutputTests.test_normalFileStandardOut.<locals>.cbLost)r
rrrZFilePathrFr=�normalZ
addCleanup�close�dict�filenor�	isWindows�os�piperZ
StandardIO�	itertoolsrqrrbrI)rZ
onConnLostr2rx�kwargsr;�wrwr)rprqrrrvrrsr�test_normalFileStandardOutFs&
z3StandardInputOutputTests.test_normalFileStandardOutzpStandardIO does not accept stdout as an argument to Windows.  Testing redirection to a file is therefore harder.N)r'r(r)�skipWindowsNopywin32�skipr5r:rGrJrQrTrVrWr]rhrkr�rr|rrrrr+\s'3r+)+r*�
__future__rrr}r-rZ
twisted.trialrZtwisted.pythonrrZtwisted.python.reflectrZtwisted.python.runtimerZtwisted.python.compatr	r
rZtwisted.internetrr
rrrZtwisted.test.test_tcprrMr�r|rz�environr1�pathseprfrv�encode�getfilesystemencodingZProcessProtocolrZTestCaser+rrrr�<module>	s.
.