%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/twisted/test/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/twisted/test/__pycache__/test_process.cpython-312.pyc

�

Ϫ�f*Y��8�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
mZ	ddlZ	ddlmZddlmZmZmZeZddlmZddlmZddlmZmZmZmZmZdd	lm Z m!Z!dd
l"m#Z#ddl$m%Z%ddl&m'Z'dd
l(m	Z	e%ejR�jTZ+dZ,e-ej\�Z/ej`jcejT�e/d<Gd�dejd�Z3Gd�de	jh�Z5Gd�dejd�Z6Gd�dejd�Z7Gd�dejd�Z8Gd�dejd�Z9Gd�de7�Z:Gd�dejd�Z;Gd �d!e;�Z<Gd"�d#e;�Z=eej|ed�d$�Gd%�d&e	jh��Z?Gd'�d(ejd�Z@Gd)�d*�ZAee!j�j��d+k7d,�eej|ed�d$�Gd-�d.eAe	jh���ZDee!j�j��d/k7d0�eej|ed�d$�Gd1�d2eAe	jh���ZEGd3�d4ejd�ZFee!j�j��d/k7d0�eej|ed�d$�Gd5�d6e	jh���ZGGd7�d8ejd�ZHGd9�d:�ZIGd;�d<�ZJGd=�d>�ZKGd?�d@e�ZLGdA�dBe�ZMGdC�dDe�ZNGdE�dF�ZOGdG�dHe	jh�ZPee!j�j��d/k7d0�eej|ed�d$�GdI�dJe	jheI���ZQee!j�j��d/k7d0�eej|ed�d$�GdK�dLe	jheI���ZRGdM�dNe9�ZSee!j�j��d+k7d,�eej|ed�d$�GdO�dPe	jh���ZTee!j�j��d+k7d,�eej|ed�d$�GdQ�dRe	jh���ZUee!j�j��d+k7d,�eej|ed�d$�GdS�dTe	jh���ZVee!j�j��d+k7d,�eej|ed�d$�GdU�dVe	jh���ZWGdW�dXe	jh�ZXGdY�dZejd�ZYeej|ed�d$�Gd[�d\e	jh��ZZy#e
$rdZY���wxYw#e
$rdZeZeZeZY���wxYw)]a�
Test running processes.

@var CONCURRENT_PROCESS_TEST_COUNT: The number of concurrent processes to use
    to stress-test the spawnProcess API.  This value is tuned to a number of
    processes which has been determined to stay below various
    experimentally-determined limitations of our supported platforms.
    Particularly, Windows XP seems to have some undocumented limitations which
    cause spurious failures if this value is pushed too high.  U{Please see
    this ticket for a discussion of how we arrived at its current value.
    <http://twistedmatrix.com/trac/ticket/3404>}

@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)�SkipTest�skipIf)�process)�
ProcessReader�
ProcessWriter�
PTYProcess)�BytesIO)�verifyObject)�defer�error�
interfaces�protocol�reactor)�	procutils�runtime)�
networkString)�FilePath��msg)�unittest��
PYTHONPATHc�.�eZdZdZd�Zd�Zd�Zd�Zd�Zy)�StubProcessProtocolz�
    ProcessProtocol counter-implementation: all methods on this class raise an
    exception, so instances of this may be used to verify that only certain
    methods are called.
    c��t���N��NotImplementedError��self�datas  �;/usr/lib/python3/dist-packages/twisted/test/test_process.py�outReceivedzStubProcessProtocol.outReceivedH���!�#�#�c��t��rrrs  r"�errReceivedzStubProcessProtocol.errReceivedKr$r%c��t��rr�r s r"�inConnectionLostz$StubProcessProtocol.inConnectionLostNr$r%c��t��rrr)s r"�outConnectionLostz%StubProcessProtocol.outConnectionLostQr$r%c��t��rrr)s r"�errConnectionLostz%StubProcessProtocol.errConnectionLostTr$r%N)	�__name__�
__module__�__qualname__�__doc__r#r'r*r,r.�r%r"rrAs ���$�$�$�$�$r%rc�4�eZdZdZd�Zd�Zd�Zd�Zd�Zd�Z	y)	�ProcessProtocolTestszj
    Tests for behavior provided by the process protocol base class,
    L{protocol.ProcessProtocol}.
    c�\�ttjtj��y)zD
        L{ProcessProtocol} implements L{IProcessProtocol}.
        N)r
r
�IProcessProtocolr�ProcessProtocolr)s r"�test_interfacez#ProcessProtocolTests.test_interface^s��	�Z�0�0�(�2J�2J�2L�Mr%c���g�G�fd�dt�}d}|�}|jd|�|j�|g�y)z�
        Verify that when stdout is delivered to
        L{ProcessProtocol.childDataReceived}, it is forwarded to
        L{ProcessProtocol.outReceived}.
        c���eZdZ�fd�Zy)�:ProcessProtocolTests.test_outReceived.<locals>.OutProtocolc�(���j|�yr��append�r r!�receiveds  �r"r#zFProcessProtocolTests.test_outReceived.<locals>.OutProtocol.outReceivedm��������%r%N)r/r0r1r#�rAs�r"�OutProtocolr<l����
&r%rD�bytes�N�r�childDataReceived�assertEqual)r rD�bytesToSend�prAs    @r"�test_outReceivedz%ProcessProtocolTests.test_outReceiveddsG�����	&�-�	&����M��	���A�{�+�����K�=�1r%c���g�G�fd�dt�}d}|�}|jd|�|j�|g�y)zA
        Similar to L{test_outReceived}, but for stderr.
        c���eZdZ�fd�Zy)�:ProcessProtocolTests.test_errReceived.<locals>.ErrProtocolc�(���j|�yrr>r@s  �r"r'zFProcessProtocolTests.test_errReceived.<locals>.ErrProtocol.errReceived|rBr%N)r/r0r1r'rCs�r"�ErrProtocolrP{rEr%rRrF�NrH)r rRrKrLrAs    @r"�test_errReceivedz%ProcessProtocolTests.test_errReceivedusG�����	&�-�	&����M��	���A�{�+�����K�=�1r%c���g�G�fd�dt�}|�}|jd�|j�dg�y)z�
        Verify that when stdin close notification is delivered to
        L{ProcessProtocol.childConnectionLost}, it is forwarded to
        L{ProcessProtocol.inConnectionLost}.
        c���eZdZ�fd�Zy)�BProcessProtocolTests.test_inConnectionLost.<locals>.InLostProtocolc�(���jd�yrr>�r �losts �r"r*zSProcessProtocolTests.test_inConnectionLost.<locals>.InLostProtocol.inConnectionLost��������D�!r%N)r/r0r1r*�rZs�r"�InLostProtocolrW�����
"r%r]rN�r�childConnectionLostrJ)r r]rLrZs   @r"�test_inConnectionLostz*ProcessProtocolTests.test_inConnectionLost�sA�����	"�0�	"�
���	���a� ������v�&r%c���g�G�fd�dt�}|�}|jd�|j�dg�y)zF
        Similar to L{test_inConnectionLost}, but for stdout.
        c���eZdZ�fd�Zy)�DProcessProtocolTests.test_outConnectionLost.<locals>.OutLostProtocolc�(���jd�yrr>rYs �r"r,zVProcessProtocolTests.test_outConnectionLost.<locals>.OutLostProtocol.outConnectionLost�r[r%N)r/r0r1r,r\s�r"�OutLostProtocolrd�r^r%rfrGNr_)r rfrLrZs   @r"�test_outConnectionLostz+ProcessProtocolTests.test_outConnectionLost��A�����	"�1�	"�
���	���a� ������v�&r%c���g�G�fd�dt�}|�}|jd�|j�dg�y)zF
        Similar to L{test_inConnectionLost}, but for stderr.
        c���eZdZ�fd�Zy)�DProcessProtocolTests.test_errConnectionLost.<locals>.ErrLostProtocolc�(���jd�yrr>rYs �r"r.zVProcessProtocolTests.test_errConnectionLost.<locals>.ErrLostProtocol.errConnectionLost�r[r%N)r/r0r1r.r\s�r"�ErrLostProtocolrk�r^r%rmrSNr_)r rmrLrZs   @r"�test_errConnectionLostz+ProcessProtocolTests.test_errConnectionLost�rhr%N)
r/r0r1r2r9rMrTrargrnr3r%r"r5r5Xs&���
N�2�"
2�'� '�'r%r5c�(�eZdZdZd�Zd�Zd�Zd�Zy)�TrivialProcessProtocolz�
    Simple process protocol for tests purpose.

    @ivar outData: data received from stdin
    @ivar errData: data received from stderr
    c�.�||_g|_g|_y)zl
        Create the deferred that will be fired at the end, and initialize
        data structures.
        N)�deferred�outData�errData�r �ds  r"�__init__zTrivialProcessProtocol.__init__�s��
��
������r%c�H�||_|jjd�yr��reasonrr�callback�r rzs  r"�processEndedz#TrivialProcessProtocol.processEnded��������
�
���t�$r%c�:�|jj|�yr)rsr?rs  r"r#z"TrivialProcessProtocol.outReceived���������D�!r%c�:�|jj|�yr)rtr?rs  r"r'z"TrivialProcessProtocol.errReceived�r�r%N)r/r0r1r2rwr}r#r'r3r%r"rprp�s����%�"�"r%rpc�$�eZdZd�Zd�Zd�Zd�Zy)�TestProcessProtocolc�f�dg|_d|_d|_|jj	d�y)NrGr%�abcd)�stagesr!�err�	transport�writer)s r"�connectionMadez"TestProcessProtocol.connectionMade�s+���c�����	���������W�%r%c�p�|dk(r|xj|z
c_y|dk(r|xj|z
c_yy)z�
        Override and disable the dispatch provided by the base class to ensure
        that it is really this method which is being called, and the transport
        is not going directly to L{outReceived} or L{errReceived}.
        rGrSN)r!r��r �childFDr!s   r"rIz%TestProcessProtocol.childDataReceived�s3���a�<��I�I���I�
��\��H�H���H�r%c��|dk(r_|jjd�|jdk7rtd|j�d���|jjd�y
|dk(rz|jjd�|jdk7rtd|j�d	���|jjd�|jjd
�y
|dk(r|jjd�y
y
)z�
        Similarly to L{childDataReceived}, disable the automatic dispatch
        provided by the base implementation to verify that the transport is
        calling this method directly.
        rGrSr�z	Data was z instead of 'abcd's1234�zErr was z instead of '1234'�r�N)r�r?r!�RuntimeErrorr�r�r��r r�s  r"r`z'TestProcessProtocol.childConnectionLost�s����a�<��K�K���q�!��y�y�G�#�"�Y�t�y�y�m�;M�#N�O�O��N�N� � ��)�
��\��K�K���q�!��x�x�7�"�"�X�d�h�h�\�9K�#L�M�M��N�N� � ��)��K�K���q�!�
��\��K�K���q�!�r%c�H�||_|jjd�yrryr|s  r"r}z TestProcessProtocol.processEnded�r~r%N)r/r0r1r�rIr`r}r3r%r"r�r��s��&�	�"�(%r%r�c�:�eZdZddzZdZdZdZd�Zd�Zd�Z	d	�Z
y)
�EchoProtocols1234567���
rNc� �||_d|_y�Nr)�onEnded�count)r r�s  r"rwzEchoProtocol.__init__�s�������
r%c�H�|jdkDsJ�t|jdz
�D]'}|jj|j��)|jj|j|jg�|j|jz|_y)NrS)�n�ranger�r��s�
writeSequence�buffer)r �is  r"r�zEchoProtocol.connectionMadesx���v�v��z��z��t�v�v��z�"�	)�A��N�N� � ����(�	)�	
���$�$�d�f�f�d�f�f�%5�6��f�f�t�v�v�o��r%c�|�|j|j|jt|�z|k7r/d||jf|_|jj�y|xjt|�z
c_|jt|j�k(r|jj�yy)Nzwrong bytes received)r�r��len�failurer��
closeStdinrs  r"r#zEchoProtocol.outReceiveds����;�;�t�z�z�D�J�J��T��$:�;�t�C�2�D�$�*�*�E�D�L��N�N�%�%�'��J�J�#�d�)�#�J��z�z�S����-�-����)�)�+�.r%c��d|_|jtj�sdt	|�z|_|jj|�y)NrGz#process didn't terminate normally: )�finished�checkr�ProcessDone�strr�r�r{r|s  r"r}zEchoProtocol.processEndeds=����
��|�|�E�-�-�.�@�3�v�;�N�D�L������d�#r%)r/r0r1r�r�r�r�rwr�r#r}r3r%r"r�r��s/���T��A�
�A��H��G��&�,�$r%r�c�(�eZdZdZd�Zd�Zd�Zd�Zy)�SignalProtocola�
    A process protocol that sends a signal when data is first received.

    @ivar deferred: deferred firing on C{processEnded}.
    @type deferred: L{defer.Deferred}

    @ivar signal: the signal to send to the process.
    @type signal: C{str}

    @ivar signaled: A flag tracking whether the signal has been sent to the
        child or not yet.  C{False} until it is sent, then C{True}.
    @type signaled: C{bool}
    c�.�||_||_d|_y�NF)rr�signal�signaled)r rr�sigs   r"rwzSignalProtocol.__init__*s�� ��
������
r%c��td|�d��|js-d|_|jj|j�yy)z�
        Handle the first output from the child process (which indicates it
        is set up and ready to receive the signal) by sending the signal to
        it.  Also log all output to help with debugging.
        �	Received z from child stdoutTN)rr�r��
signalProcessr�rs  r"r#zSignalProtocol.outReceived/s?��	�i��x�1�2�3��}�}� �D�M��N�N�(�(����5�r%c�"�td|�d��y)z_
        Log all data received from the child's stderr to help with
        debugging.
        r�z from child stderrNrrs  r"r'zSignalProtocol.errReceived:s��
	�i��x�1�2�3r%c
��td|j����|jtj�s'|j
j
td|����S|j}t|jt�rttd|jz�}n|j}|j�?|j
j
td|j�d|j�d���S|j|k7rA|j
j
td|j�d|j�d|����Stj|j �|k7rQ|j
j
td|j�d	tj|j �����S|j
j#d�y)
aL
        Callback C{self.deferred} with L{None} if C{reason} is a
        L{error.ProcessTerminated} failure with C{exitCode} set to L{None},
        C{signal} set to C{self.signal}, and C{status} holding the status code
        of the exited process. Otherwise, errback with a C{ValueError}
        describing the problem.
        zChild exited: �wrong termination: �SIGNz: exitCode is z
, not Nonez: .signal was z	, wanted z: )r�getTracebackr�r�ProcessTerminatedrr�errback�
ValueError�value�
isinstancer�r��getattr�exitCode�os�WTERMSIG�statusr{)r rz�v�signalValues    r"r}zSignalProtocol.processEndedAsp��	�n�V�0�0�2�5�6�7��|�|�E�3�3�4��=�=�(�(��6I�&��4R�)S�T�T��L�L���d�k�k�3�'�!�&�%�$�+�+�*=�>�K��+�+�K��:�:�!��=�=�(�(��S����
�^�A�J�J�<�z�R�S��
�
�8�8�{�"��=�=�(�(���{�{�A�H�H�k�;���
��;�;�q�x�x� �K�/��=�=�(�(��S����
�R����A�H�H�0E�/F�G�H��
�	
�
�
���t�$r%N)r/r0r1r2rwr#r'r}r3r%r"r�r�s����
	6�4�%r%r�c��eZdZd�Zd�Zy)�TestManyProcessProtocolc�6�tj�|_yr�r�Deferredrrr)s r"rwz TestManyProcessProtocol.__init__d������(��
r%c��||_|jtj�r|jjd�y|jj
|�yr)rzr�rr�rrr{r�r|s  r"r}z$TestManyProcessProtocol.processEndedgs?������<�<��)�)�*��M�M�"�"�4�(��M�M�!�!�&�)r%N)r/r0r1rwr}r3r%r"r�r�cs��)�*r%r�c�T�eZdZUdZdZeed<ed��Zd�Z	d�Z
d�Zd�Zd	�Z
d
�Zy)�UtilityProcessProtocolz�
    Helper class for launching a Python process and getting a result from it.

    @ivar programName: The name of the program to run.
    r%�programNamec�r�|�}|j|ttdd|jg|z|��|S)a9
        Run a Python process connected to a new instance of this protocol
        class.  Return the protocol instance.

        The Python process is given C{self.program} on the command line to
        execute, in addition to anything specified by C{argv}.  C{env} is
        the complete environment.
        �-u�-m��env)�spawnProcess�pyExer�)�clsr�argvr�r s     r"�runzUtilityProcessProtocol.runxs?���u�������%�%��t�T�-=�-=�>��E�3�	
��r%c� �g|_g|_yr)�bytes�requestsr)s r"rwzUtilityProcessProtocol.__init__�s����
���
r%c��t��)zR
        Called with all bytes received on stdout when the process exits.
        r�r r�s  r"�parseChunksz"UtilityProcessProtocol.parseChunks�s
��"�#�#r%c�d�tj�}|jj|�|S)z{
        Return a Deferred which will fire with the result of L{parseChunks}
        when the child process exits.
        )rr�r�r?rus  r"�	getResultz UtilityProcessProtocol.getResult�s'��

�N�N����
�
���Q���r%c�Z�|j}d|_|D]}|j|��y)zt
        Callback all Deferreds returned up until now by L{getResult}
        with the given result object.
        N)r�r{)r �resultr�rvs    r"�_fireResultDeferredsz+UtilityProcessProtocol._fireResultDeferreds�s/��
�=�=����
��	�A�
�J�J�v��	r%c�:�|jj|�y)zE
        Accumulate output from the child process in a list.
        N)r�r?r�s  r"r#z"UtilityProcessProtocol.outReceived�s��	
�
�
���%� r%c�X�|j|j|j��y)zu
        Handle process termination by parsing all received output and firing
        any waiting Deferreds.
        N)r�r�r�r|s  r"r}z#UtilityProcessProtocol.processEnded�s!��
	
�!�!�$�"2�"2�4�:�:�">�?r%N)r/r0r1r2r�r��__annotations__�classmethodr�rwr�r�r�r#r}r3r%r"r�r�osD����K����
��
��$���!�@r%r�c��eZdZdZdZd�Zy)�GetArgumentVectorzl
    Protocol which will read a serialized argv from a process and
    expose it to interested parties.
    stwisted.test.process_getargvc�B�dj|�jd�S)z�
        Parse the output from the process to which this protocol was
        connected, which is a single unterminated line of \0-separated
        strings giving the argv of that process.  Return this as a list of
        str objects.
        r%�)�join�split)r �chunkss  r"r�zGetArgumentVector.parseChunks�s���x�x���%�%�e�,�,r%N�r/r0r1r2r�r�r3r%r"r�r��s���
2�K�-r%r�c��eZdZdZdZd�Zy)�GetEnvironmentDictionaryzx
    Protocol which will read a serialized environment dict from a process
    and expose it to interested parties.
    stwisted.test.process_getenvc��dj|�}|siSt|jd��}i}		t|�}t|�}|||<�#t$rY|SwxYw)a
        Parse the output from the process to which this protocol was
        connected, which is a single unterminated line of \0-separated
        strings giving key value pairs of the environment from that process.
        Return this as a dictionary.
        r%r�)r��iterr��next�
StopIteration)r r��environBytes�environbrv�kr�s       r"r�z$GetEnvironmentDictionary.parseChunks�sy���x�x��'����I���*�*�5�1�2�����
���N����N����!����!�
����
�s�A�	A�ANr�r3r%r"r�r��s���
1�K�r%r�z'reactor doesn't support IReactorProcessc��eZdZdZdZd�Zd�Zd�Zd�Zd�Z	e
ejjdd	�j�d
k(xrej j#�dk(d�d
��Ze
ejjdd	�j�d
k(xrej j#�dk(d�d��Zd�Zd�Zy)�ProcessTestsz!
    Test running a process.
    Fc�����d}t��tj�x}�_t	j
�ttdd|gtd�j���jjd��jjd��jjd��jj���fd	�}|j|�S)
z1
        L{twisted.internet.stdio} test.
        ztwisted.test.process_twistedr�r�N�r��path�usePTY�hello, worldsabcs123c	�����j�jj�dd�jj��d�jj��d��y)Nshello, worldabc123zOutput follows:
z-
Error message from process_twisted follows:
�
)rJ�outF�getvalue�errF��ignrLr s ��r"r}z-ProcessTests.test_stdio.<locals>.processEndedsE�����������!�%��&�&�/�/�+�Q�V�V�_�_�->�@�
r%)
�Accumulatorrr��
endedDeferredrr�r��	properEnvrr�r�r��addCallback�r �
scriptPathrvr}rLs`   @r"�
test_stdiozProcessTests.test_stdio�s����4�
��M��#�n�n�.�.��A�O����
��
�D�$�
�+����;�;�
	
�	
�����/�*�	�����&�!�	�����&�!�	����� �	��}�}�\�*�*r%c�P�ddl}|j|dd�|j�S)z�
        In some scenarious, such as Python running as part of a Windows
        Windows GUI Application with no console, L{sys.stdout} is L{None}.
        rN�stdout)�sys�patchr)r rs  r"�test_patchSysStdoutWithNonez(ProcessTests.test_patchSysStdoutWithNones$��
	��
�
�3��$�'���� � r%c�j�ddl}ddlm}|�}|j|d|�|j	�S)z�
        Some projects which use the Twisted reactor
        such as Buildbot patch L{sys.stdout} with L{io.StringIO}
        before running their tests.
        rN��StringIOr�r�iorrr)r rr�stdoutStringIOs    r"�test_patchSysStdoutWithStringIOz,ProcessTests.test_patchSysStdoutWithStringIOs.��	��!����
�
�3��.�1���� � r%c��ddl}ddlm}|j|d|��|j|d|��|j	�S)z�
        If L{sys.stdout} and L{sys.__stdout__} are patched with L{io.StringIO},
        we should get a L{ValueError}.
        rNrr�
__stdout__r)r rrs   r"�$test_patch_sys__stdout__WithStringIOz1ProcessTests.test_patch_sys__stdout__WithStringIO&s;��
	���
�
�3��(�*�-��
�
�3��h�j�1���� � r%c�2���tj�}t|�}d}tj|t
t
dd|gt����j�j���fd�}|jj�|j|�S)z�
        Test if pid is None/non-None before/after process termination.  This
        reuses process_echoer.py to get a process that blocks on stdin.
        �twisted.test.process_echoer�-u�-mr�c�<���j�j�yr��assertIsNone�pid)�ignored�	procTransr s ��r"�afterProcessEndz3ProcessTests.test_unsetPid.<locals>.afterProcessEnd?s������i�m�m�,r%)rr�rprr�r�r�
assertTruer'r�r�r
)r r�rLrr*r)s`    @r"�
test_unsetPidzProcessTests.test_unsetPid2s|���
�>�>�#��"�8�,��3�
��(�(�
�u�u�e�U�J�7�Y�
�	�	
���	�
�
�&�	-�	
����� ��#�#�O�4�4r%�CI��true�win32z/See https://twistedmatrix.com/trac/ticket/10014c�����d}tj�}t��|�_t	j
�ttdd|gt����fd�}|j|�|S)zp
        Test running a process: check its output, it exitCode, some property of
        signalProcess.
        �twisted.test.process_testerr"r#r�c�����j�jgd���j}|jtj
��j|jjd��jtj�jjd�	ddl}ddl
}|j|j�D]}tj |��y#t"$rYywxYw)N�rGrSr�r�r���INTr)rJr�rz�traprr�r�r��assertRaises�ProcessExitedAlreadyr�r��glob�process_tester�test_file_matchr��remove�
BaseException)r(�fr:r;rLr s    ��r"r�z(ProcessTests.test_process.<locals>.checkUs�������Q�X�X��7����A�
�F�F�5�*�*�+����Q�W�W�-�-�r�2�
����*�*�A�K�K�,E�,E�u�
�
��%����>�#A�#A�B�!�A��I�I�a�L�!�� �
��
�s�'=C%�%	C1�0C1)	rr�r�rrrr�r�rr
)r rrvr�rLs`   @r"�test_processzProcessTests.test_processEsZ���4�
��N�N����!����
����Q���u�e�Z�'H�i�X�	�(	
�
�
�e���r%c�Z���fd�}d}tdd|g}g}g}tt�D]Y}t�}|j	|�tj|t|t��|j	|j��[tj|d��}|j||�|S)Nc
�J��|D]�}�j|jgd�dt|j�t	|j�fz�|j
}|j
tj��j|jjd���y)Nr4z[%d] stages = %sr5)rJr��idr�r�rzr7rr�r�r�)�results�	protocolsrLr?r s    �r"�_checkz/ProcessTests.test_manyProcesses.<locals>._checkrs~����	
7��� � ��H�H�#�&�"�Q�[�[�/�3�q�x�x�=�)I�I���H�H�����u�.�.�/�� � ����!1�!1�2�6�	
7r%r2r"r#r�T)�
consumeErrors)r�r��CONCURRENT_PROCESS_TEST_COUNTr�r?rr�rrrr�DeferredListr
)	r rFr�argsrE�	deferredsr�rL�deferredLists	`        r"�test_manyProcesseszProcessTests.test_manyProcessesls����
	7�4�
��u�e�Z�0���	��	��4�5�	)�A�'�)�A����Q��� � ��E�4�Y�?����Q�Z�Z�(�		)��)�)�)�4�H��� � ���3��r%c�����tj�}t|��d}tj�t
t
dd|gt����fd�}�fd�}|j|�j|�S)z�
        A spawning a subprocess which echoes its stdin to its stdout via
        L{IReactorProcess.spawnProcess} will result in that echoed output being
        delivered to outReceived.
        r!r"r#r�c����j�j�j��jt�d���j	t�j�t�j�jz��y)Nr�)	�assertFalser�r+�hasattrrJr�r�r�r��r(rLr s ��r"�assertsz'ProcessTests.test_echo.<locals>.asserts�sV������Q�Y�Y��	�	�2��O�O�G�A�x�0�1����S����]�C����a�c�c�	�N�;r%c�<���jj�|Sr)r�r�)r�rLs �r"�takedownProcessz/ProcessTests.test_echo.<locals>.takedownProcess�s���
�K�K�"�"�$��Jr%)	rr�r�rr�r�rr
�
addErrback)r r�rrSrUrLs`    @r"�	test_echozProcessTests.test_echo�se����>�>�#����"��3�
����Q���u�e�Z�'H�i�X�	<�
	��#�#�G�,�7�7��H�Hr%c������gd��d}t��tj�x}�_t	j
�ttdd|g�ztd�����fd�}|j|�S)N)	sa\"b sa\b s a\\"bs a\\bs"foo bar" "s	abs"\sa"bsa'bstwisted.test.process_cmdliner"r#�r�rc�����j�jj�d��jj�j	�}�j|��y�Nr%)rJrrr�
splitlines)r	�	recvdArgsrJrLr s  ���r"r}z3ProcessTests.test_commandLine.<locals>.processEnded�sG������Q�V�V�_�_�.��4������)�4�4�6�I����Y��-r%�	r
rr�rrr�r�rr
)r rrvr}rJrLs`   @@r"�test_commandLinezProcessTests.test_commandLine�si���

��5�
��M��#�n�n�.�.��A�O����
�u�u�e�U�J�7�$�>�I�TX�	
�	.�
�}�}�\�*�*r%N)r/r0r1r2rrrrrr,rr��environ�get�lowerr�platform�getTyper@rMrWr_r3r%r"r�r��s���
��F�+�@!�!�
!�5�&�
�
�
���t�R� �&�&�(�F�2�	2����$�$�&�'�1�9��
 ��
 �D�
�
�
���t�R� �&�&�(�F�2�	2����$�$�&�'�1�9��
��
�8I�.+r%r�c�&�eZdZdZdZd�Zd�Zd�Zy)�TwoProcessProtocol���rc�6�tj�|_yrr�r)s r"rwzTwoProcessProtocol.__init__�r�r%c��yrr3rs  r"r#zTwoProcessProtocol.outReceived����r%c�H�d|_|jjd�y�NrG)r�rrr{r|s  r"r}zTwoProcessProtocol.processEnded�s����
��
�
���t�$r%N)r/r0r1�numr�rwr#r}r3r%r"rfrf�s��
�C��H�)�
�%r%rfc�,�eZdZd�Zdd�Zd�Zd�Zd�Zy)�TestTwoProcessesBasec�D�ddg|_ddg|_d|_d|_yr�)�	processes�pp�done�verboser)s r"�setUpzTestTwoProcessesBase.setUp�s&���������,�����	���r%c	��d}dD]s}t�|j|<||j|_tj|j|t
t
dd|gt|��}||j|<�uy)Nstwisted.test.process_reader)rrGr"r#�r�r)rfrrrmrr�r�rrq)r rrrmrLs     r"�createProcessesz$TestTwoProcessesBase.createProcesses�sv��3�
��
	$�C�-�/�D�G�G�C�L�"�D�G�G�C�L���$�$���������u�j�1����A�#$�D�N�N�3��
	$r%c�Z�|jrtd|z�|j|}|j|}|j	|j
d�|j
�|jr;t|jdj
|jdj
�yy)Nzclosing stdin [%d]�Process finished too earlyrrG)rt�printrqrrrPr��loseConnection�r rmrLrrs    r"�closezTestTwoProcessesBase.close�s����<�<��&��,�-��N�N�3���
�W�W�S�\��������&B�C�	�����<�<��$�'�'�!�*�%�%�t�w�w�q�z�':�':�;�r%c�z�tj|jD�cgc]}|j��c}�Scc}wr)r�
gatherResultsrrrr)r rLs  r"�_onClosezTestTwoProcessesBase._onClose�s)���"�"����#@�1�A�J�J�#@�A�A��#@s�8c���|jrtd�|j�tjd|j
d�tjd|j
d�|j
�S)N�starting processesrGrrS�rtr{rxr�	callLaterr~r�r)s r"�
test_closezTestTwoProcessesBase.test_close�sV���<�<��&�'��������!�T�Z�Z��+����!�T�Z�Z��+��}�}��r%N)r)r/r0r1rurxr~r�r�r3r%r"roro�s���$�<�B�r%ror0zOnly runs on Windowsc��eZdZy)�TwoProcessesNonPosixTestsN)r/r0r1r3r%r"r�r��s��	r%r��posixzOnly runs on POSIX platformc�*�eZdZd�Zd�Zd�Zd�Zd�Zy)�TwoProcessesPosixTestsc��t|j|j�D]A\}}|jr�	t	j
|jtj��C|j�S#t$rY�_wxYwr)�ziprrrqr�r��killr'r��SIGTERM�OSErrorr�)r rr�prs   r"�tearDownzTwoProcessesPosixTests.tearDownsj���$�'�'�4�>�>�2�	�F�B���;�;���G�G�B�F�F�F�N�N�3�	��}�}����	����s�.A5�5	B�Bc��|jrtd|z�|j|}|j|}|j	|j
d�t
j|jtj�|jr;t|jdj
|jdj
�yy)Nzkill [%d] with SIGTERMrzrrG)rtr{rqrrrPr�r�r�r'r�r�r}s    r"r�zTwoProcessesPosixTests.kills����<�<��*�S�0�1��N�N�3���
�W�W�S�\��������&B�C�
������v�~�~�&��<�<��$�'�'�!�*�%�%�t�w�w�q�z�':�':�;�r%c��|jrtd�|jd��tjd|j
d�tjd|j
d�|j
�S)Nr�r�rrGrS�rtr{rxrr�r�r�r)s r"�	test_killz TwoProcessesPosixTests.test_kill�[���<�<��&�'����A��&����!�T�Y�Y��*����!�T�Y�Y��*��}�}��r%c��|jrtd�|jd��tjd|j
d�tjd|j
d�|j
�S�Nr�rGr�rrSr�r)s r"�
test_closePtyz$TwoProcessesPosixTests.test_closePty%s[���<�<��&�'����A��&����!�T�Z�Z��+����!�T�Z�Z��+��}�}��r%c��|jrtd�|jd��tjd|j
d�tjd|j
d�|j
�Sr�r�r)s r"�test_killPtyz#TwoProcessesPosixTests.test_killPty-r�r%N)r/r0r1r�r�r�r�r�r3r%r"r�r�s��	�<���r%r�c�<�eZdZdZdZdZd�Zd�Zd�Zd�Z	d�Z
d	�Zy)
�	FDCheckerrr%Nc��||_yr)rrrus  r"rwzFDChecker.__init__;s	����
r%c�H�||_|jjd�yr)�failedrrr{)r �whys  r"�failzFDChecker.fail>s������
�
���t�$r%c�J�|jjdd�d|_y)Nrr�rG)r��writeToChild�stater)s r"r�zFDChecker.connectionMadeBs�����#�#�A�w�/���
r%c�&�|jdk(r�|dk7r|jd||fz�y|xj|z
c_t|j�dk(rY|jdk7r|jd|jz�yd|_d|_|jjdd	�y|jdk(r|jd
|�d|�d��y|jdk(r�|dk7r|jd
|�d|�d
��y|xj|z
c_t|j�dk(r5|jdk7r|jd|jz�yd|_y|jdk(r|jd
|�d|�d��yy)NrGz)read '%s' on fd %d (not 1) during state 1�srightoz"got '%s' on fd1, expected 'righto'r%rSr�sefghzread 'z' on fd z during state 2z (not 1) during state 3sclosedz"got '%s' on fd1, expected 'closed'r�z during state 4)r�r�r!r�r�r�r�s   r"rIzFDChecker.childDataReceivedFsk���:�:��?��!�|��	�	�E��RV��W�X���I�I���I��4�9�9�~��"��9�9�	�)��I�I�B�T�Y�Y�N�O����	���
����+�+�A�w�7���:�:��?��I�I��w�i�x��v�_�E�F���:�:��?��!�|��	�	�F�7�)�8�D�6�9P�Q�R���I�I���I��4�9�9�~��"��9�9�	�)��I�I�B�T�Y�Y�N�O����
���:�:��?��I�I��w�i�x��v�_�E�F��r%c���|jdk(r|jd|z�y|jdk(r=|dk7r|jd|z�yd|_|jjd�yy)NrGz%got connectionLost(%d) during state 1rSr�z-got connectionLost(%d) (not 4) during state 2r�r�)r�r�r��closeChildFDr�s  r"r`zFDChecker.childConnectionLosthsj���:�:��?��I�I�=��G�H���:�:��?��!�|��	�	�I�G�S�T���D�J��N�N�'�'��*��
r%c�f�|jj}|jdk7r|jd|z�y|jjdk7r)|jd|jjz�y|dk7r|jd|z�y|j
j
d�y)Nr�zprocessEnded early, rc %dzprocessEnded with signal %srzprocessEnded with rc %d)r�r�r�r�r�rrr{)r r��rcs   r"r}zFDChecker.processEndedts���
�\�\�
"�
"���:�:��?��I�I�1�B�6�7���<�<���$�&��I�I�3�f�l�l�6I�6I�I�J��
��7��I�I�/�"�4�5���
�
���t�$r%)r/r0r1r�r!r�rwr�r�rIr`r}r3r%r"r�r�6s0��
�E��D�
�F��%�� �D
�%r%r�c��eZdZd�Zd�Zy)�FDTestsc
�����d}tj�}t|��tj�t
t
dd|gtddddddd���|j��fd	��|S)
Nstwisted.test.process_fdsr"r#�w�rrS)rrGrSr�r�r��r��childFDsc�P���j�j�j�Sr)rPr�)�xrLr s ��r"�<lambda>z!FDTests.test_FD.<locals>.<lambda>�s���� 0� 0����1�8�8� D�r%)rr�r�rr�r�rr
)r rrvrLs`  @r"�test_FDzFDTests.test_FD�sd���0�
��N�N����a�L�����
��
�E�5�*�-�����s�s�s�C�	
�	
�
�
�D�E��r%c	�����d}t��tj�x}�_t	j
�ttdd|gtddd�����fd�}|j|�S)	Nstwisted.test.process_lingerr"r#r�rS)rGrSr�c�Z���j�jj�d�y)Nshere is some text
goodbye
)rJrrrs ��r"r}z)FDTests.test_linger.<locals>.processEnded�s������Q�V�V�_�_�.�0O�Pr%r^rs`   @r"�test_lingerzFDTests.test_linger�se���4�
��M��#�n�n�.�.��A�O����
��
�E�5�*�-����^�	
�	Q��}�}�\�*�*r%N)r/r0r1r�r�r3r%r"r�r��s���+r%r�c�<�eZdZdZdZdZd�Zd�Zd�Zd�Z	d�Z
d	�Zy)
r
zAccumulate data from a process.rNc�@�t�|_t�|_yr)r	rrr)s r"r�zAccumulator.connectionMade�s���I��	��I��	r%c�:�|jj|�yr)rr�rus  r"r#zAccumulator.outReceived�����	�	����r%c�:�|jj|�yr)rr�rus  r"r'zAccumulator.errReceived�r�r%c��yrr3r)s r"r,zAccumulator.outConnectionLost�rjr%c��yrr3r)s r"r.zAccumulator.errConnectionLost�rjr%c�v�d|_|j�&|jdc}|_|jd�yyrl)�closedrr{)r rzrvs   r"r}zAccumulator.processEnded�s;��������)�$(�$6�$6��!�A�t�!�
�J�J�t��*r%)r/r0r1r2r�rr�r#r'r,r.r}r3r%r"r
r
�s-��)�
�F��M����
�
�r%r
c�V�eZdZdZdZd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
d�Zd
�Zy)�PosixProcessBasez!
    Test running processes.
    Fc���tj|�D]&}t|�j�jcStd�j|�}td�j|�}|j
�r|j�jS|j
�r|j�jSt|�dtjd�d���)zq
        Return the path of the shell command named C{commandName}, looking at
        common locations.
        z/binz/usr/binz1 found in neither standard location nor on PATH (�PATH�))
r�whichr�asBytesModer�child�existsr�r�r`)r �commandName�loc�binLoc�	usrbinLocs     r"�
getCommandzPosixProcessBase.getCommand�s���
�?�?�;�/�	4�C��C�=�,�,�.�3�3�3�	4��&�!�'�'��4���Z�(�.�.�{�;�	��=�=�?��%�%�'�,�,�,�
�
�
�
��(�(�*�/�/�/���-�P�QS�Q[�Q[�\b�Qc�Pd�de�f��
r%c������jd�}tj�}t|��t	j
�|dgd�j����fd�}|j|�|S)Nr/struerwc����jjtj��j	�jj
jd��j�jj
j�yr�)	rzr7rr�rJr�r�r&r�rRs ��r"r�z6PosixProcessBase.test_normalTermination.<locals>.check�sS���
�H�H�M�M�%�+�+�,����Q�X�X�^�^�4�4�a�8����a�h�h�n�n�3�3�4r%)r�rr�rprr�rr
)r �cmdrvr�rLs`   @r"�test_normalTerminationz'PosixProcessBase.test_normalTermination�s[����o�o�f�%���N�N���"�1�%�����Q��g�Y�D����M�	5�
	
�
�
�e���r%c�����tj�}t|��tj�t
t
ddgd�j����fd�}|j|�|S)z�
        When a process terminates with a system exit code set to 1,
        C{processEnded} is called with a L{error.ProcessTerminated} error,
        the C{exitCode} attribute reflecting the system exit code.
        �-csimport sys; sys.exit(1)Nrwc����jjtj��j	�jj
jd��j�jj
j�yrl)	rzr7rr�rJr�r�r&r�rRs ��r"r�z8PosixProcessBase.test_abnormalTermination.<locals>.checksS���
�H�H�M�M�%�1�1�2����Q�X�X�^�^�4�4�a�8����a�h�h�n�n�3�3�4r%)rr�rprr�r�rr
)r rvr�rLs`  @r"�test_abnormalTerminationz)PosixProcessBase.test_abnormalTermination�s\���
�N�N���"�1�%�����
��
�E�5�6���;�;�	
�	5�
	
�
�
�e���r%c��d}tj�}t||�}tj|t
t
dd|gt|j��|S)N�twisted.test.process_signalr"r�rw)rr�r�rr�r�rr�r r�rrvrLs     r"�_testSignalzPosixProcessBase._testSignal
sP��3�
��N�N����1�c�"�����
��
�E�4��,���;�;�	
��r%c�$�|jd�S)ai
        Sending the SIGHUP signal to a running process interrupts it, and
        C{processEnded} is called with a L{error.ProcessTerminated} instance
        with the C{exitCode} set to L{None} and the C{signal} attribute set to
        C{signal.SIGHUP}. C{os.WTERMSIG} can also be used on the C{status}
        attribute to extract the signal value.
        �HUP�r�r)s r"�test_signalHUPzPosixProcessBase.test_signalHUP�������&�&r%c�$�|jd�S)ai
        Sending the SIGINT signal to a running process interrupts it, and
        C{processEnded} is called with a L{error.ProcessTerminated} instance
        with the C{exitCode} set to L{None} and the C{signal} attribute set to
        C{signal.SIGINT}. C{os.WTERMSIG} can also be used on the C{status}
        attribute to extract the signal value.
        r6r�r)s r"�test_signalINTzPosixProcessBase.test_signalINT!r�r%c�$�|jd�S)ak
        Sending the SIGKILL signal to a running process interrupts it, and
        C{processEnded} is called with a L{error.ProcessTerminated} instance
        with the C{exitCode} set to L{None} and the C{signal} attribute set to
        C{signal.SIGKILL}. C{os.WTERMSIG} can also be used on the C{status}
        attribute to extract the signal value.
        �KILLr�r)s r"�test_signalKILLz PosixProcessBase.test_signalKILL+�������'�'r%c�$�|jd�S)ak
        Sending the SIGTERM signal to a running process interrupts it, and
        C{processEnded} is called with a L{error.ProcessTerminated} instance
        with the C{exitCode} set to L{None} and the C{signal} attribute set to
        C{signal.SIGTERM}. C{os.WTERMSIG} can also be used on the C{status}
        attribute to extract the signal value.
        �TERMr�r)s r"�test_signalTERMz PosixProcessBase.test_signalTERM5r�r%c��tj}tj|tj�}|jtjtj|�|j	tj�S)z�
        The disposition of signals which are ignored in the parent
        process is reset to the default behavior for the child
        process.
        )r��SIGUSR1�SIG_IGN�
addCleanupr�)r r��handlers   r"�test_childSignalHandlingz)PosixProcessBase.test_childSignalHandling?sR�� �����-�-��v�~�~�6������
�
�v�~�~�w�?�������/�/r%c�����tjj�r�jrt	d���jd�}t
j�}t|��d�}tj}|t_
dt_	tj�|dgd�j����fd�}|j|�|t_
|S#|t_
wxYw)	zJ
        Raise an error during execvpe to check error management.
        z+Test is flaky from a Darwin bug. See #8840.�falsec��td��)N�Ouch)r�)�commandrJ�environments   r"�buggyexecvpez:PosixProcessBase.test_executionError.<locals>.buggyexecvpees
���v�&�&r%TsfalseNrwc���dj�j�jz�}�jd|��jd|�y)Nr%sUpon execvpesOuch)r�rtrs�assertIn)r(rtrLr s  ��r"r�z3PosixProcessBase.test_executionError.<locals>.checkps:����(�(�1�9�9�q�y�y�#8�9���
�
�o�w�7��
�
�g�w�/r%)rrc�isMacOSXrrr�rr�rpr��execvper�_neverUseSpawnr�r
)r r�rvr��
oldexecvper�rLs`     @r"�test_executionErrorz$PosixProcessBase.test_executionErrorYs�������$�$�&�4�;�;��H�I�I��o�o�g�&���N�N���"�1�%��	'��Z�Z�
�!��
�"&���
	$�� � ��C�(���d�k�k�R�
0�

�M�M�%� �#�B�J����$�B�J�s�<C�
C(c�X�����tj��tj��d}G��fd�dtj�}t	j
|�ttdd|gtd��g���fd�}�j|���fd	�}�j|��S)
z�
        The handler which reaps a process is removed when the process is
        reaped, even if the protocol's C{processEnded} method raises an
        exception.
        r!c�&��eZdZdZ�fd�Z�fd�Zy)�FPosixProcessBase.test_errorInProcessEnded.<locals>.ErrorInProcessEndedzM
            A protocol that raises an error in C{processEnded}.
            c�(���j|�yr�r{�r r��	connecteds  �r"�makeConnectionzUPosixProcessBase.test_errorInProcessEnded.<locals>.ErrorInProcessEnded.makeConnection������"�"�9�-r%c�\��tjd�jd�td��)NrzDeliberate error)rr�r{r��r rz�endeds  �r"r}zSPosixProcessBase.test_errorInProcessEnded.<locals>.ErrorInProcessEnded.processEnded�s%����!�!�!�U�^�^�T�:�"�#5�6�6r%N�r/r0r1r2rr}�rrs��r"�ErrorInProcessEndedr�s���
�
.�
7r%rr"r#NrYc����j|j��j|jtj�|j�yr)r?r'r�r�reapProcessHandlersr|)r�r'r s ��r"�cbConnectedz>PosixProcessBase.test_errorInProcessEnded.<locals>.cbConnected�s9����J�J�y�}�}�%��M�M�)�-�-��)D�)D�E�
�$�$�&r%c����jt�}�jt|�d��j	�dt
j�y)NrGr)�flushLoggedErrorsr�rJr��assertNotInrr)r(�excsr'r s  ��r"�checkTerminatedzBPosixProcessBase.test_errorInProcessEnded.<locals>.checkTerminated�sA����)�)�,�7�D����S��Y��*����S��V�W�%@�%@�Ar%)	rr�rr8rr�r�rr
)r rrrrrrr's`    @@@r"�test_errorInProcessEndedz)PosixProcessBase.test_errorInProcessEndedzs�����N�N�$�	���� ��4�
�
	7�(�":�":�
	7�	����!��
�E�5�*�-���	
���	'�	���k�*�	B�	���/�*��r%N)r/r0r1r2rr�r�r�r�r�r�r�r�r�rrr3r%r"r�r��sF����F��(
��0�'�'�(�(�0�4�B6r%r�c��eZdZdZd�Zd�Zy)�
MockSignalzF
    Neuter L{signal.signal}, but pass other attributes unscathed
    c�,�tj|�Sr)r��	getsignal)r r��actions   r"r�zMockSignal.signal�s������$�$r%c�"�tt|�Sr)r�r�)r �attrs  r"�__getattr__zMockSignal.__getattr__�s���v�t�$�$r%N)r/r0r1r2r�r r3r%r"rr�s���%�%r%rc�H�eZdZdZdZdZdZdZdZdZ	dZ
dZdZdZ
dZdZd�Zd�Zd	�Zd
�Zd�Zd�Zd
�Zd�Zd�Zd�Zd�Zd�Zdddddddd�d�Zd�Zd�Zd�Zd�Z d�Z!d�Z"d�Z#d�Z$d�Z%d�Z&d �Z'd!�Z(d"�Z)d#�Z*d$�Z+d%�Z,d&�Z-d'�Z.d(�Z/d)�Z0d*�Z1d+�Z2d,�Z3d-�Z4d.�Z5d/�Z6d0�Z7d1�Z8y)2�MockOSaF
    The mock OS: overwrite L{os}, L{fcntl} and {sys} functions with fake ones.

    @ivar exited: set to True when C{_exit} is called.
    @type exited: C{bool}

    @ivar O_RDWR: dumb value faking C{os.O_RDWR}.
    @type O_RDWR: C{int}

    @ivar O_NOCTTY: dumb value faking C{os.O_NOCTTY}.
    @type O_NOCTTY: C{int}

    @ivar WNOHANG: dumb value faking C{os.WNOHANG}.
    @type WNOHANG: C{int}

    @ivar raiseFork: if not L{None}, subsequent calls to fork will raise this
        object.
    @type raiseFork: L{None} or C{Exception}

    @ivar raiseExec: if set, subsequent calls to execvpe will raise an error.
    @type raiseExec: C{bool}

    @ivar fdio: fake file object returned by calls to fdopen.
    @type fdio: C{BytesIO} or C{BytesIO}

    @ivar actions: hold names of some actions executed by the object, in order
        of execution.

    @type actions: C{list} of C{str}

    @ivar closed: keep track of the file descriptor closed.
    @type closed: C{list} of C{int}

    @ivar child: whether fork return for the child or the parent.
    @type child: C{bool}

    @ivar pipeCount: count the number of time that C{os.pipe} has been called.
    @type pipeCount: C{int}

    @ivar raiseWaitPid: if set, subsequent calls to waitpid will raise
        the error specified.
    @type raiseWaitPid: L{None} or a class

    @ivar waitChild: if set, subsequent calls to waitpid will return it.
    @type waitChild: L{None} or a tuple

    @ivar euid: the uid returned by the fake C{os.geteuid}
    @type euid: C{int}

    @ivar egid: the gid returned by the fake C{os.getegid}
    @type egid: C{int}

    @ivar seteuidCalls: stored results of C{os.seteuid} calls.
    @type seteuidCalls: C{list}

    @ivar setegidCalls: stored results of C{os.setegid} calls.
    @type setegidCalls: C{list}

    @ivar path: the path returned by C{os.path.expanduser}.
    @type path: C{str}

    @ivar raiseKill: if set, subsequent call to kill will raise the error
        specified.
    @type raiseKill: L{None} or an exception instance.

    @ivar readData: data returned by C{os.read}.
    @type readData: C{str}
    FNTrr%c��g|_g|_d|_d|_d|_d|_d|_d|_d�|_d�|_	g|_
g|_y	)
z-
        Initialize data structures.
        rrg������r�i�c��yr�r3�r�s r"r�z!MockOS.__init__.<locals>.<lambda>��r%c��yrlr3r's r"r�z!MockOS.__init__.<locals>.<lambda>r(r%N)�actionsr��	pipeCount�O_RDWR�O_NOCTTY�WNOHANG�F_GETFD�
FD_CLOEXEC�WEXITSTATUS�	WIFEXITED�seteuidCalls�setegidCallsr)s r"rwzMockOS.__init__s[����������������
����������&���$��������r%c��y)zK
        Fake fcntl.fcntl for CLOEXEC file descriptor enumeration.
        rr3)r �op�fl�args    r"�fcntlzMockOS.fcntl#s��r%c��y)ze
        Fake C{os.open}. Return a non fd number to be sure it's not used
        elsewhere.
        ���r3)r �dev�flagss   r"�openzMockOS.open)s��
r%c�,�tjd�S)zT
        Fake C{os.fstat}.  Return a C{os.stat_result} filled with garbage.
        )
rrrrrrrrrr)r��stat_result�r �fds  r"�fstatzMockOS.fstat0s���~�~�i�(�(r%c�F�|dk(rt�|_|jSJ�)zz
        Fake C{os.fdopen}. Return a file-like object whose content can
        be tested later via C{self.fdio}.
        �wb)r	�fdio)r rB�flags   r"�fdopenz
MockOS.fdopen6s'��
�4�<��	�D�I��y�y��
�5r%c�:�|jjd�y)z1
        Fake C{os.setsid}. Save action.
        �setsidN�r*r?r)s r"rJz
MockOS.setsidAs��	
�����H�%r%c��|jjdtj�f�|j�|j�|j
ryy)z�
        Fake C{os.fork}. Save the action in C{self.actions}, and return 0 if
        C{self.child} is set, or a dumb number.
        �forkr�)r*r?�gc�	isenabled�	raiseForkr�r)s r"rMzMockOS.forkGsB��
	
�����V�R�\�\�^�4�5��>�>�%��.�.� �
�Z�Z��r%c�:�|jj|�y)zK
        Fake C{os.close}, saving the closed fd in C{self.closed}.
        N)r�r?rAs  r"r~zMockOS.closeUs��	
�����2�r%c��y)z.
        Fake C{os.dup2}. Do nothing.
        Nr3)r �fd1�fd2s   r"�dup2zMockOS.dup2[r(r%c�@�|jjd||f�y)z0
        Fake C{os.write}. Save action.
        r�NrK)r rBr!s   r"r�zMockOS.write`s��	
�����W�b�$�/�0r%c�V�|jjd||f�|jS)z�
        Fake C{os.read}: save action, and return C{readData} content.

        @param fd: The file descriptor to read.

        @param size: The maximum number of bytes to read.

        @return: A fixed C{bytes} buffer.
        �read)r*r?�readData)r rB�sizes   r"rYzMockOS.readfs'��	
�����V�R��.�/��}�}�r%c�h�|jjd�|jrtd��y)zn
        Fake C{os.execvpe}. Save the action, and raise an error if
        C{self.raiseExec} is set.
        �exec�BarN)r*r?�	raiseExecr�)r r�rJr�s    r"r�zMockOS.execvpess-��
	
�����F�#��>�>��u�%�%�r%r3)�file_actions�	setpgroup�resetidsrJ�
setsigmask�	setsigdef�	schedulerc�:�|jjd�y)z;
        Fake C{os.posix_spawnp}. Save the action.
        �posix_spawnpNrK)r rr�r�r`rarbrJrcrdres           r"rgzMockOS.posix_spawnp|s��"	
�����N�+r%c�n�|xjdz
c_d|jzdzd|jzfS)z�
        Fake C{os.pipe}. Return non fd numbers to be sure it's not used
        elsewhere, and increment C{self.pipeCount}. This is used to uniquify
        the result.
        rGr$)r+r)s r"�pipezMockOS.pipe�s4��	
���!����D�N�N�"�Q�&��T�^�^�(;�;�;r%c��y)z;
        Fake C{os.ttyname}. Return a dumb string.
        �foor3rAs  r"�ttynamezMockOS.ttyname�s��r%c�^�|jjd|f�d|_t��)zs
        Fake C{os._exit}. Save the action, set the C{self.exited} flag, and
        raise C{SystemError}.
        �exitT)r*r?�exited�SystemError)r �codes  r"�_exitzMockOS._exit�s+��
	
�����V�T�N�+�����m�r%c��y)z6
        Override C{fcntl.ioctl}. Do nothing.
        Nr3)r rBr=r8s    r"�ioctlzMockOS.ioctl�r(r%c��y)z?
        Override C{fdesc.setNonBlocking}. Do nothing.
        Nr3rAs  r"�setNonBlockingzMockOS.setNonBlocking�r(r%c��|jjd�|j�|j�|j�|jSy)z�
        Override C{os.waitpid}. Return values meaning that the child process
        has exited, save executed action.
        �waitpid)rGr)r*r?�raiseWaitPid�	waitChild)r r'�optionss   r"rxzMockOS.waitpid�sE��
	
�����I�&����(��#�#�#��>�>�%��>�>�!�r%c��y)zD
        Override C{sys.settrace} to keep coverage working.
        Nr3)r r8s  r"�settracezMockOS.settrace�r(r%c��y)z>
        Override C{os.getgid}. Return a dumb number.
        i�r3r)s r"�getgidz
MockOS.getgid����r%c��y)z>
        Override C{os.getuid}. Return a dumb number.
        i�r3r)s r"�getuidz
MockOS.getuid�r�r%c�>�|jjd|f�y)z4
        Override C{os.setuid}. Do nothing.
        �setuidNrK�r �vals  r"r�z
MockOS.setuid����	
�����X�s�O�,r%c�>�|jjd|f�y)z4
        Override C{os.setgid}. Do nothing.
        �setgidNrKr�s  r"r�z
MockOS.setgid�r�r%c�@�|jjd||f�y)z6
        Override C{os.setregid}. Do nothing.
        �setregidNrK�r �val1�val2s   r"r�zMockOS.setregid����	
�����Z��t�4�5r%c�@�|jjd||f�y)z<
        Override C{os.setreuid}.  Save the action.
        �setreuidNrKr�s   r"r�zMockOS.setreuid�r�r%c�@�|jjd||f�y)z>
        Override L{util.switchUID}. Save the action.
        �	switchuidNrK)r �uid�gids   r"�	switchUIDzMockOS.switchUID�s��	
�����[�#�s�3�4r%c��y)zK
        Override C{pty.openpty}, returning fake file descriptors.
        )����r3r)s r"�openptyzMockOS.openpty�s��r%c�>�|jjd|f�y)z{
        Override C{os.chdir}. Save the action.

        @param path: The path to change the current directory to.
        �chdirNrK�r rs  r"r�zMockOS.chdir����	
�����W�d�O�,r%c��|jS)zE
        Mock C{os.geteuid}, returning C{self.euid} instead.
        )�euidr)s r"�geteuidzMockOS.geteuid�����y�y�r%c��|jS)zE
        Mock C{os.getegid}, returning C{self.egid} instead.
        )�egidr)s r"�getegidzMockOS.getegidr�r%c�:�|jj|�y)z3
        Mock C{os.seteuid}, store result.
        N)r3r?�r r�s  r"�seteuidzMockOS.seteuid���	
��� � ��&r%c�:�|jj|�y)z3
        Mock C{os.setegid}, store result.
        N)r4r?r�s  r"�setegidzMockOS.setegidr�r%c��|jS)z-
        Mock C{os.path.expanduser}.
        )rr�s  r"�
expanduserzMockOS.expanduserr�r%c��y)z'
        Mock C{pwd.getpwnam}.
        )rrrGrSr3)r �users  r"�getpwnamzMockOS.getpwnam���r%c��y)zN
        Override C{os.listdir}, returning fake contents of '/dev/fd'
        )z-1z-2r3r�s  r"�listdirzMockOS.listdir r�r%c�p�|jjd||f�|j�|j�y)zh
        Override C{os.kill}: save the action and raise C{self.raiseKill} if
        specified.
        r�N)r*r?�	raiseKill)r r'�signalIDs   r"r�zMockOS.kill&s6��
	
�����V�S�(�3�4��>�>�%��.�.� �&r%c�>�|jjd|f�y)zl
        Override C{os.unlink}. Save the action.

        @param filename: The file name to remove.
        �unlinkNrK)r �filenames  r"r�z
MockOS.unlink/s��	
�����X�x�0�1r%c�>�|jjd|f�y)zo
        Override C{os.umask}. Save the action.

        @param mask: The new file mode creation mask.
        �umaskNrK)r �masks  r"r�zMockOS.umask7r�r%c��y)zL
        Return a fixed PID value.

        @return: A fixed value.
        i�r3r)s r"�getpidz
MockOS.getpid?s��r%c��y)z`
        Return a fixed filesystem encoding.

        @return: A fixed value of "utf8".
        �utf8r3r)s r"�getfilesystemencodingzMockOS.getfilesystemencodingGs��r%)9r/r0r1r2ror_rFr�ryrQrzr�r�rr�rZrwr9r>rCrHrJrMr~rVr�rYr�rgrirlrrrtrvrxr}rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r3r%r"r"r"�s+��C�J�F��I��D��E��L��I��I��D��D��D��I��H��"��)�	�&����
1��&��������,�&<��	��
�

��
��-�-�6�6�5��-���'�'����!�2�-��r%r"c��eZdZdZd�Zy)�DumbProcessWriterz1
    A fake L{ProcessWriter} used for tests.
    c��y�z<
        Here's the faking: don't do anything here.
        Nr3r)s r"�startReadingzDumbProcessWriter.startReadingUr(r%N�r/r0r1r2r�r3r%r"r�r�P����r%r�c��eZdZdZd�Zy)�DumbProcessReaderz1
    A fake L{ProcessReader} used for tests.
    c��yr�r3r)s r"r�zDumbProcessReader.startReading`r(r%Nr�r3r%r"r�r�[r�r%r�c��eZdZdZd�Zy)�DumbPTYProcessz.
    A fake L{PTYProcess} used for tests.
    c��yr�r3r)s r"r�zDumbPTYProcess.startReadingkr(r%Nr�r3r%r"r�r�fr�r%r�c��eZdZd�Zy)�ForkOrSpawnc��|dk(ry|dk(ryy)N�rMFTrgFr3)r �others  r"�__eq__zForkOrSpawn.__eq__rs���O�#���N�"��r%N)r/r0r1r�r3r%r"r�r�qs��r%r�c��eZdZdZe�dZd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
d�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z y)�MockProcessTestsz?
    Mock a process runner to test forked child code path.
    Nz1twisted.internet.process is never used on Windowsc���tj�r |jtj�n|jtj�t�|_d|j_d|j_|jtd|j�|jtd|j�|jtd|j�|jtd|jj�|jtd|j�|jtjdt�|jtjd	t�|jtjd
d��|jtd|j�t�|_|jtd
|j �y)z|
        Replace L{process} os, fcntl, sys, switchUID, fdesc and pty modules
        with the mock class L{MockOS}.
        i���r�r9rr��fdesc�processReaderFactory�processWriterFactory�_trySpawnInsteadOfForkc��yr�r3)�ar�s  r"r�z(MockProcessTests.setUp.<locals>.<lambda>�r(r%�ptyr�N)rOrPr��enable�disabler"�mockosr�r�rrr��Processr�r�r�mocksigr)s r"ruzMockProcessTests.setUp�s+��
�<�<�>��O�O�B�I�I�&��O�O�B�J�J�'��h��������������
�
�7�D�$�+�+�.��
�
�7�G�T�[�[�1��
�
�7�E�4�;�;�/��
�
�7�K����)>�)>�?��
�
�7�G�T�[�[�1��
�
�7�?�?�$:�<M�N��
�
�7�?�?�$:�<M�N��
�
�7�?�?�$<�>S�T��
�
�7�E�4�;�;�/�!�|����
�
�7�H�d�l�l�3r%c��it_y)z6
        Reset processes registered for reap.
        N)rrr)s r"r�zMockProcessTests.tearDown�s��')��#r%c�d�|j|jjt�dg�y)z}
        A process should have been launched, but I don't care whether it was
        with fork() or posix_spawnp().
        rxN)rJr�r*r�r)s r"�assertProcessLaunchedz&MockProcessTests.assertProcessLaunched�s%��
	
������,�,�{�}�i�.H�Ir%c���tj�d}tj�}t	|�}	tj||dgdd��|jd�|jtj��y#t$rP|j|jj�|j|jjgd��Y�|wxYw)zk
        Test a classic spawnProcess. Check the path of the client code:
        fork, exec, exit.
        �
/mock/ouch�ouchNFrw�Should not be here�r�r]�rnrG)rOr�rr�rprr�r�rpr+r�rorJr*rPrP�r r�rvrLs    r"�
test_mockForkzMockProcessTests.test_mockFork�s���
	�	�	�����N�N���"�1�%��	,�� � ��C�'���U�K�
�I�I�*�+�	
�������(���	��O�O�D�K�K�.�.�/�������#�#�%K�
�	�s�B�AC �C c� �d|j_d}tj�}t	|�}tj||dgdd��|jt|jj�hd��|j�y)z�
        Assert that in the main process, spawnProcess disables the garbage
        collector, calls fork, closes the pipe file descriptors it created for
        the child process, and calls waitpid.
        Fr�r�Nrw>���r%rg)r�r�rr�rprr�rJ�setr�r�r�s    r"�_mockForkInParentTestz&MockProcessTests._mockForkInParentTest�sn��"��������N�N���"�1�%�����Q��g�Y�D��G�����T�[�[�/�/�0�,�?��"�"�$r%c��tj�|j�|jtj��y)z�
        The garbage collector should be enabled when L{reactor.spawnProcess}
        returns if it was initially enabled.

        @see L{_mockForkInParentTest}
        N)rOr�r�r+rPr)s r"�,test_mockForkInParentGarbageCollectorEnabledz=MockProcessTests.test_mockForkInParentGarbageCollectorEnabled�s+��	�	�	���"�"�$��������'r%c��tj�|j�|jtj��y)z�
        The garbage collector should be disabled when L{reactor.spawnProcess}
        returns if it was initially disabled.

        @see L{_mockForkInParentTest}
        N)rOr�r�rPrPr)s r"�-test_mockForkInParentGarbageCollectorDisabledz>MockProcessTests.test_mockForkInParentGarbageCollectorDisabled�s-��	�
�
���"�"�$��������(r%c	�6�d}tj�}t|�}|jtt
j||dgdd��|j|jj�|j|jjgd��y)zg
        Test a TTY spawnProcess: check the path of the client code:
        fork, exec, exit.
        r�r�NTrw)r�rJr]r�)rr�rpr8rprr�r+r�rorJr*r�s    r"�test_mockForkTTYz!MockProcessTests.test_mockForkTTY�s���
���N�N���"�1�%�������-�-�q�#��y�d�SW�	�	
�	
������*�*�+�����K�K���!Q�	
r%c��ttjd�|j_td�}|j
ttj|d�|j|jjdg�y)z�
        Assert that if the fork call fails, no other process setup calls are
        made and that spawnProcess raises the exception fork raised.
        Nr�)r��errno�EAGAINr�rQrpr8rr�rJr*�r rs  r"�_mockWithForkErrorz#MockProcessTests._mockWithForkError�s\��
!(����d� ;�����)�$�/�����'�7�#7�#7��4�H�������,�,��.?�@r%c��tj�|j�|jtj��y)z�
        The garbage collector should be enabled when L{reactor.spawnProcess}
        raises because L{os.fork} raised, if it was initially enabled.
        N)rOr�r�r+rPr)s r"�-test_mockWithForkErrorGarbageCollectorEnabledz>MockProcessTests.test_mockWithForkErrorGarbageCollectorEnableds+��
	�	�	�����!��������'r%c��tj�|j�|jtj��y)z�
        The garbage collector should be disabled when
        L{reactor.spawnProcess} raises because L{os.fork} raised, if it was
        initially disabled.
        N)rOr�r�rPrPr)s r"�.test_mockWithForkErrorGarbageCollectorDisabledz?MockProcessTests.test_mockWithForkErrorGarbageCollectorDisabled
s-��	�
�
�����!��������(r%c��|j�|jt|jj�hd��y)z}
        When C{os.fork} raises an exception, the file descriptors created
        before are closed and don't leak.
        >r����r%r;r$rgN)r�rJr�r�r�r)s r"�test_mockForkErrorCloseFDsz+MockProcessTests.test_mockForkErrorCloseFDss0��
	
���!�����T�[�[�/�/�0�2J�Kr%c
��ttjd�|j_td�}|j
ttj|ddddd���|j|jjdg�|j|jjg�|j
ttj|ddddd���|jt|jj�d	d
h�y)z�
        When C{os.forks} raises an exception and that file descriptors have
        been specified with the C{childFDs} arguments of
        L{reactor.spawnProcess}, they are not closed.
        Ni����i����r�)rrGrS�r�r�r�rgr$)
r�r�r�r�rQrpr8rr�rJr*r�r�r�s  r"�test_mockForkErrorGivenFDsz+MockProcessTests.test_mockForkErrorGivenFDss���!(����d� ;�����)�$�/������� � ������-�	�	
�	
������,�,��.?�@�������+�+�R�0�	
����� � ������-�	�	
�	
����T�[�[�/�/�0�2�r�(�;r%c�l�ttjd�|j_td�}|j
ttj|dd��|j|jjdg�|jt|jj�ddh�y)z�
        When C{os.fork} raises an exception, the file descriptors created by
        C{pty.openpty} are closed and don't leak, when C{usePTY} is set to
        C{True}.
        NTr�r�r�r�)
r�r�r�r�rQrpr8rr�rJr*r�r�r�s  r"�test_mockForkErrorClosePTYz+MockProcessTests.test_mockForkErrorClosePTY8s���!(����d� ;�����)�$�/�����'�7�#7�#7��4�PT��U�������,�,��.?�@�����T�[�[�/�/�0�3��*�=r%c�V�ttjd�|j_td�}|j
ttj|dd��|j|jjdg�|j|jjg�y)z�
        If a tuple is passed to C{usePTY} to specify slave and master file
        descriptors and that C{os.fork} raises an exception, these file
        descriptors aren't closed.
        N)i��i��rkr�r�)r�r�r�r�rQrpr8rr�rJr*r�r�s  r"�test_mockForkErrorPTYGivenFDsz.MockProcessTests.test_mockForkErrorPTYGivenFDsDs���!(����d� ;�����)�$�/������W�)�)�8�T�BS�	�	
�	
������,�,��.?�@�������+�+�R�0r%c���d}tj�}t|�}d|j_	tj||dgdd��|jd�y#t$r�|j|jj�|j|jjgd��|jd	|jj�|jd
|jj�|jd|jj�|jd|jjj!��YywxYw)
z�
        Spawn a process but simulate an error during execution in the client
        path: C{os.execvpe} raises an error. It should close all the standard
        fds, try to print the error encountered, and exit cleanly.
        r�Tr�NFrwr�r�rrGrSsRuntimeError: Bar)rr�rpr�r_rr�r�rpr+rorJr*r�r�rFrr�s    r"�test_mockWithExecErrorz'MockProcessTests.test_mockWithExecErrorRs
�����N�N���"�1�%�� $�����	,�� � ��C�'���U�K�
�I�I�*�+���
	M��O�O�D�K�K�.�.�/�������#�#�%K�
�
�M�M�!�T�[�[�/�/�0��M�M�!�T�[�[�/�/�0��M�M�!�T�[�[�/�/�0��M�M�.����0@�0@�0I�0I�0K�L�
	M�s�A!�!C<E �E c�Z�d}tj�}t|�}	tj||dgddd��|jd�y#t$rP|j|jj�|j|jjgd��YywxYw)	z�
        Try creating a process with setting its uid: it's almost the same path
        as the standard path, but with a C{switchUID} call before the exec.
        r�r�NF��r�rr�r�)r��r�r�r�r)r�rr�r]r��rr�rprr�r�rpr+r�rorJr*r�s    r"�test_mockSetUidz MockProcessTests.test_mockSetUidms���
���N�N���"�1�%��	,�� � ��C�'���U�PT�U�
�I�I�*�+���	��O�O�D�K�K�.�.�/�������#�#��

�	���A�AB*�)B*c��d|j_d}tj�}t	|�}tj||dgddd��|j�y)z�
        When spawning a child process with a UID different from the UID of the
        current process, the current process does not have its UID changed.
        Fr�r�Nrr)r�r�rr�rprr�r�r�s    r"�test_mockSetUidInParentz(MockProcessTests.test_mockSetUidInParent�sQ��
"��������N�N���"�1�%�����Q��g�Y�D��D�Q��"�"�$r%c�Z�d}tj�}t|�}	tj||dgddd��|jd�y#t$rP|j|jj�|j|jjgd��YywxYw)	z�
        Try creating a PTY process with setting its uid: it's almost the same
        path as the standard path, but with a C{switchUID} call before the
        exec.
        r�r�NT�rr�)r�rJrr)r�rr�r]r�rr�s    r"�test_mockPTYSetUidz#MockProcessTests.test_mockPTYSetUid�s������N�N���"�1�%��	,�� � ��C�'���T�t�T� 
�I�I�*�+���
	��O�O�D�K�K�.�.�/�������#�#��
�
	�rc�6�d|j_d}tj�}t	|�}t
j}	tt
_tj||dgddd��|t
_|j�y#|t
_wxYw)z�
        When spawning a child process with PTY and a UID different from the UID
        of the current process, the current process does not have its UID
        changed.
        Fr�r�NTrr)r�r�rr�rprrr�rr�r�)r r�rvrL�
oldPTYProcesss     r"�test_mockPTYSetUidInParentz+MockProcessTests.test_mockPTYSetUidInParent�s}��"��������N�N���"�1�%���*�*�
�	/�!/�G��� � ��C�'���T�t�T�!.�G���"�"�$��"/�G��s�+B�
Bc��d|j_d}d|j_tj�}t|�}t
j||dgdd��}|j�t�|j_
|j�|j�}|jt|�d�|djt�y)	z;
        Test that reapProcess logs errors raised.
        Fr��rrr�NrwrGr)r�r�rzrr�rprr�r�r�ry�reapProcessrrJr�r7)r r�rvrL�proc�errorss      r"�test_mockWithWaitErrorz'MockProcessTests.test_mockWithWaitError�s���"������� &������N�N���"�1�%���#�#�A�s�W�I�4��N���"�"�$�#*�9���� ������'�'�)������V��a�(��q�	���w�r%c��d|j_d}d|j_tj�}t|�}t
j||dgdd��}|j�t�|j_
tj|jj_|j�y)z{
        Test that reapProcess doesn't log anything when waitpid raises a
        C{OSError} with errno C{ECHILD}.
        Fr�r r�Nrw)r�r�rzrr�rprr�r�r�ryr��ECHILDr!)r r�rvrLr"s     r"�!test_mockErrorECHILDInReapProcessz2MockProcessTests.test_mockErrorECHILDInReapProcess�s���
"������� &������N�N���"�1�%���#�#�A�s�W�I�4��N���"�"�$�#*�9���� �).������ � �&����r%c�J��gd���fd�}||j_td�}|jtt
j|d�|j|jjg�|jt|jj�hd��y)z�
        If C{os.pipe} raises an exception after some pipes where created, the
        created pipes are closed and don't leak.
        )rgr$r;r%c�x��	�jd��jd�fS#t$rt��wxYwr�)�pop�
IndexErrorr�)�pipess�r"riz3MockProcessTests.test_mockErrorInPipe.<locals>.pipe�s9���
 ��y�y��|�U�Y�Y�q�\�1�1���
 ��i��
 �s�!%�9N>r%r;r$rg)r�rirpr8r�rr�rJr*r�r�)r rirr,s   @r"�test_mockErrorInPipez%MockProcessTests.test_mockErrorInPipe�s{���
!��	 � �����)�$�/�����'�7�#7�#7��4�H�������,�,�b�1�����T�[�[�/�/�0�2B�Cr%c�.�d|j_d|j_d}td�}t	j
||dgdd��}|j
d�|j|jjdd	d
dtjfg�y)z{
        L{process.Process.signalProcess} calls C{os.kill} translating the given
        signal string to the PID.
        Fr r�Nr�rwr�r�rxr�rN)r�r�rzrprr�r�rJr*r��SIGKILL�r r�rLr"s    r"r�zMockProcessTests.test_kill�s���
"����� &�������"�4�(���#�#�A�s�W�I�4��N�����6�"�����K�K���
�i�&�"�f�n�n�)E�F�	
r%c���d|j_d}td�}tj||dgdd��}|jtj|jd�y)zz
        L{process.Process.signalProcess} raises L{error.ProcessExitedAlready}
        if the process has exited.
        Fr�Nr�rwr�)	r�r�rprr�r8rr9r�r0s    r"�test_killExitedz MockProcessTests.test_killExitedsY��
"�������"�4�(���#�#�A�s�W�I�4��N��	
���%�4�4�d�6H�6H�&�Qr%c�@�d|j_d|j_d}td�}t	j
||dgdd��}t
tjd�|j_	|jtj|jd�y)	a*
        L{process.Process.signalProcess} raises L{error.ProcessExitedAlready}
        if the process has exited but that twisted hasn't seen it (for example,
        if the process has been waited outside of twisted): C{os.kill} then
        raise C{OSError} with C{errno.ESRCH} as errno.
        Fr r�Nr�rwz	Not foundr�)r�r�rzrprr�r�r��ESRCHr�r8rr9r�r0s    r"�test_killExitedButNotDetectedz.MockProcessTests.test_killExitedButNotDetectedsz��"����� &�������"�4�(���#�#�A�s�W�I�4��N�� '����[� A��������%�4�4�d�6H�6H�&�Qr%c��d|j_d|j_d}td�}t	j
||dgdd��}t
tjd�|j_	|jt|jd�}|j|jtj�y)	z�
        L{process.Process.signalProcess} doesn't mask C{OSError} exceptions if
        the errno is different from C{errno.ESRCH}.
        Fr r�Nr�rwzInvalid signalr�)
r�r�rzrprr�r�r��EINVALr�r8r�rJ)r r�rLr"r�s     r"�test_killErrorInKillz%MockProcessTests.test_killErrorInKill(s���
"����� &�������"�4�(���#�#�A�s�W�I�4��N�� '����6F� G����������);�);�V�D��������E�L�L�1r%)!r/r0r1r2r�skiprur�r�r�r�r�r�r�r�rrrrr
rrrrrrr$r'r-r�r2r5r8r3r%r"r�r�zs������B��4�2)�J�)�0%� 	(�	)�
�"A�(�)�L�<�8
>�1�,�6,�6%�,�:%�& �$�$D�&
� R�
R�2r%r�c��eZdZd�Zd�Zy)�PosixProcessTestsc����d}t��tj�x}�_t	j
�ttdtdj|��gdd�j����fd�}|j|�S)z�
        Bytes written to stderr by the spawned process are passed to the
        C{errReceived} callback on the C{ProcessProtocol} passed to
        C{spawnProcess}.
        �42r�z"import sys; sys.stderr.write('{}')N�/tmpr�c�Z���jd�jj��y)Ns42)rJrrrs ��r"r}z3PosixProcessTests.test_stderr.<locals>.processEndedVs������U�A�F�F�O�O�$5�6r%)r
rr�rrr�r�r�formatrr
)r r�rvr}rLs`   @r"�test_stderrzPosixProcessTests.test_stderr?s|������M��#�n�n�.�.��A�O����
�����E�L�L�U�S�T�
�
���;�;�	
�	7��}�}�\�*�*r%c�`�����jd�}d�t��tj�x}�_tj�||dgdd�j���jj���jj����fd�}|j|�S)N�gzipsTthere's no place like home!
there's no place like home!
there's no place like home!
r�r>r�c�����j}|jdd�tj|��5}�j	|j���ddd�y#1swYyxYw)Nr)�fileobj)r�seekrC�GzipFilerJrY)r	r?�gfrLr�r s   ���r"r}z4PosixProcessTests.test_process.<locals>.processEndedfsR������A�
�F�F�1�a�L����q�)�
/�R�� � �����A�.�
/�
/�
/�s�!A � A))r�r
rr�rrr�rr�r�r�r
)r r�rvr}rLr�s`   @@r"r@zPosixProcessTests.test_process[s�����o�o�f�%��0���M��#�n�n�.�.��A�O����
�s�S�%�L�d�����	
�	
�����!��	����� �	/��}�}�\�*�*r%N)r/r0r1rAr@r3r%r"r;r;7s��+�8+r%r;c� �eZdZdZdZd�Zd�Zy)�PosixProcessPTYTestszE
    Just like PosixProcessTests, but use ptys instead of pipes.
    Tc� ���d}t��tj�x}�_t	j
�ttdd|gt�j���jjd���fd�}|j|�S)Nstwisted.test.process_ttyr"r#rws
hello world!
c����jtj�jjd��j�jj�dd�jj��d��y)Nr�shello world!
hello world!
z)Error message from process_tty follows:

z

)r8rr9r�r�rJrrrs ��r"r}z:PosixProcessPTYTests.test_openingTTY.<locals>.processEnded�s_�������*�*�A�K�K�,E�,E�u�
�
��������!�3�-.�F�F�O�O�,=�@�	
r%)r
rr�rrr�r�rrr�r�r
rs`   @r"�test_openingTTYz$PosixProcessPTYTests.test_openingTTY�sw���0�
��M��#�n�n�.�.��A�O����
��
�E�5�*�-���;�;�	
�	
�����+�,�	��}�}�\�*�*r%c
��tdddg}t�}|jttj
|t|dddi��y)Nr"r��print('hello')rG�r)rr��r�r
r8r�rr��r �pyArgsrLs   r"�test_badArgsz!PosixProcessPTYTests.test_badArgs�sJ�����'8�9���M������� � �
������Y�	�	
r%N)r/r0r1r2rrMrTr3r%r"rJrJos����F�+�8
r%rJc��eZdZdZd�Zy)�Win32SignalProtocolz�
    A win32-specific process protocol that handles C{processEnded}
    differently: processes should exit with exit code 1.
    c�^�|jtj�s'|jj	td|����S|j}|jdk7r1|jj	td|j����S|jjd�y)z�
        Callback C{self.deferred} with L{None} if C{reason} is a
        L{error.ProcessTerminated} failure with C{exitCode} set to 1.
        Otherwise, errback with a C{ValueError} describing the problem.
        r�rGzWrong exit code: N)	r�rr�rrr�r�r�r�r{)r rzr�s   r"r}z Win32SignalProtocol.processEnded�s����|�|�E�3�3�4��=�=�(�(��6I�&��4R�)S�T�T��L�L���:�:��?��=�=�(�(��6G��
�
�|�4T�)U�V�V��
�
���t�$r%N)r/r0r1r2r}r3r%r"rVrV�s���
%r%rVc�F�eZdZdZd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zy)�Win32ProcessTestsz?
    Test process programs that are packaged with twisted.
    c����t��tj�x}�_t	j
�||||��jjd��jj���fd�}|j|�S)zH
        Spawn a process, write to stdin, and check the output.
        rc����j�jj�d��j�jj�d�y)Nserr
err
sout
hello, world
out
)rJrrrrs ��r"r}z9Win32ProcessTests._test_stdinReader.<locals>.processEnded�s:������Q�V�V�_�_�.�
�>����Q�V�V�_�_�.�0K�Lr%)
r
rr�rrr�r�r�r�r
)r r�rJr�rrvr}rLs`      @r"�_test_stdinReaderz#Win32ProcessTests._test_stdinReader�sn���
�M��#�n�n�.�.��A�O����Q��t�S�$�7�	�����/�*�	����� �	M��}�}�\�*�*r%c���ddl}ttj�j	�}|dddg}ttj�}tjjtj�jtj��|d<|j�}|jtj��}|j||||�}|S)z=
        Pass L{bytes} args to L{_test_stdinReader}.
        rNr"r#s twisted.test.process_stdinreaders
PYTHONPATH)�win32apirr�
executable�_asBytesPath�dictr�r`�pathsepr�r�encoder��GetTempPathr\)r r^r�rJr�rrvs       r"�test_stdinReader_bytesArgsz,Win32ProcessTests.test_stdinReader_bytesArgs�s���	�����(�5�5�7���u�e�%H�I���2�:�:����Z�Z�_�_�S�X�X�6�=�=��%�%�'�
��M���#�#�%���{�{�3�4�4�6�7���"�"�5�$��T�:���r%c��ddl}ttj�j}|dddg}t
}tjjtj�}||d<|j�}|j||||�}|S)z?
        Pass L{unicode} args to L{_test_stdinReader}.
        rNr�r�z twisted.test.process_stdinreaderr)r^rrr_rrr�rbr�rdr\)r r^r�rJr��
pythonPathrrvs        r"�test_stdinReader_unicodeArgsz.Win32ProcessTests.test_stdinReader_unicodeArgs�sx��	�����(�-�-���t�T�#E�F�����Z�Z�_�_�S�X�X�.�
�&��L���#�#�%���"�"�5�$��T�:���r%c	��tdddg}t�}|jttj
|t|d��|jttj
|t|d��|jttj
|t|d��|jttj
|t|ddi�	�y)
Nr"r�rOrG)r�)r�r�r�rrQrRs   r"rTzWin32ProcessTests.test_badArgs�s������'8�9���M�����*�g�&:�&:�A�u�f�RS��T����*�g�&:�&:�A�u�f�RS��T����*�g�&:�&:�A�u�f�UV��W������,�,�a���!�S��	�	
r%c��d}tj�}t||�}tj|t
t
dd|gt��|S)Nr�r"r#r�)rr�rVrr�r�rr�s     r"r�zWin32ProcessTests._testSignal	sA��3�
��N�N�����3�'�����Q���u�e�Z�'H�i�X��r%c�$�|jd�S)z�
        Sending the SIGTERM signal terminates a created process, and
        C{processEnded} is called with a L{error.ProcessTerminated} instance
        with the C{exitCode} attribute set to 1.
        r�r�r)s r"r�z!Win32ProcessTests.test_signalTERM
	�������'�'r%c�$�|jd�S)z�
        Sending the SIGINT signal terminates a created process, and
        C{processEnded} is called with a L{error.ProcessTerminated} instance
        with the C{exitCode} attribute set to 1.
        r6r�r)s r"r�z Win32ProcessTests.test_signalINT	s������&�&r%c�$�|jd�S)z�
        Sending the SIGKILL signal terminates a created process, and
        C{processEnded} is called with a L{error.ProcessTerminated} instance
        with the C{exitCode} attribute set to 1.
        r�r�r)s r"r�z!Win32ProcessTests.test_signalKILL	rlr%c������	�ddl�	tj��tj��G��fd�dtj�}|�}t
dddg}t
j|t
|�����	fd�}�j|����	fd	�}�j|�tj��g�S)
zU
        The win32 handles should be properly closed when the process exits.
        rNc�&��eZdZdZ�fd�Z�fd�Zy)�;Win32ProcessTests.test_closeHandles.<locals>.SimpleProtocolzZ
            A protocol that fires deferreds when connected and disconnected.
            c�(���j|�yrrrs  �r"rzJWin32ProcessTests.test_closeHandles.<locals>.SimpleProtocol.makeConnection0	r	r%c�(���jd�yrrrs  �r"r}zHWin32ProcessTests.test_closeHandles.<locals>.SimpleProtocol.processEnded3	s������t�$r%Nr
rs��r"�SimpleProtocolrq+	s���
�
.�
%r%rtr"r�rOc�����j|���j�j��j�j��j�_�j�_yr)�assertIs�GetHandleInformation�hProcess�hThread)r�r"r r^s ���r"rz8Win32ProcessTests.test_closeHandles.<locals>.cbConnected:	sK����M�M�)�T�*��)�)�$�-�-�8��)�)�$�,�,�7� �M�M�D�M��<�<�D�Lr%c�l���j�j��j�j��j�j��j	�j
�j�j��j	�j
�j�j�yr)r&r'rxryr8rrw)r(r"r r^s ���r"rz<Win32ProcessTests.test_closeHandles.<locals>.checkTerminatedE	s�������d�h�h�'����d�m�m�,����d�l�l�+�������� =� =�t�}�}�
�
������� =� =�t�|�|�
r%)
r^rr�rr8r�rr�r
r�)
r rtrLrSrrrrr"r^s
`     @@@@r"�test_closeHandlesz#Win32ProcessTests.test_closeHandles"	s����	��N�N�$�	���� ��		%�X�5�5�		%�
������'8�9���#�#�A�u�f�5��	(�	���k�*�	�	���/�*��"�"�I�u�#5�6�6r%N)
r/r0r1r2r\rerhrTr�r�r�r�r{r3r%r"rYrY�s4���+� �"
�
��(�'�(�27r%rYc���eZdZdZd�Zeejjej�k7djejjej���d��Zy)�Win32UnicodeEnvironmentTestsz2
    Tests for Unicode environment on Windows
    c�"��dtjd<�jtjtjd�t
j
tgtj�}�fd�}|j�j|�S)z�
        C{os.environ} (inherited by every subprocess on Windows)
        contains Unicode keys and Unicode values which can be ASCII-encodable.
        �VALUE_ASCII�	KEY_ASCIIc�0���j|dd�y)Ns	KEY_ASCIIsVALUE_ASCII�rJ)r�r s �r"�gotEnvironmentz[Win32UnicodeEnvironmentTests.test_AsciiEncodeableUnicodeEnvironment.<locals>.gotEnvironmentk	s������X�l�3�^�Dr%�
r�r`r��operator�delitemr�r�rr�r
)r rLr�s`  r"�&test_AsciiEncodeableUnicodeEnvironmentzCWin32UnicodeEnvironmentTests.test_AsciiEncodeableUnicodeEnvironmenta	sd���
#0��
�
�;������(�(�"�*�*�k�B�$�(�(��"�b�j�j�A��	E��{�{�}�(�(��8�8r%z�sys.stdout.encoding: {} does not match sys.getfilesystemencoding(): {} .  May need to set PYTHONUTF8 and PYTHONIOENCODING environment variables.c�2���d}d�d}|tj|<�jtjtj|�t
j
tgtj�}��fd�}|j�j|�S)zt
        L{os.environ} (inherited by every subprocess on Windows) can
        contain a UTF-8 string value.
        �!TWISTED_BUILD_SOURCEVERSIONAUTHORs!TWISTED_BUILD_SOURCEVERSIONAUTHOR�Speciał Committórc����j�|��j|�djtjj
��y)Nr�)r�rJrcrr�encoding)r��envKeyBytesr s ��r"r�zQWin32UnicodeEnvironmentTests.test_UTF8StringInEnvironment.<locals>.gotEnvironment�	s?����M�M�+�x�0������%�'<�'C�'C�C�J�J�DW�DW�'X�
r%r�)r �envKey�envValrLr�r�s`    @r"�test_UTF8StringInEnvironmentz9Win32UnicodeEnvironmentTests.test_UTF8StringInEnvironmentp	sr���5��:��&��#��
�
�6������(�(�"�*�*�f�=�$�(�(��"�b�j�j�A��	��{�{�}�(�(��8�8r%N)r/r0r1r2r�rrrr�r�r@r�r3r%r"r}r}W	so���
9���
�
���8�s�8�8�:�:�	A�AG���J�J���!:��!:�!:�!<�B
�	�9��9r%r}c��eZdZdZd�Zd�Zy)�DumbWin32ProcTestsz3
    L{twisted.internet._dumbwin32proc} tests.
    c���
�ddlm}ddlm}�j	|d|�tt�jd�j}ttj�j}tj�}t|�}d}|d||g}|jt|d|id��
�j!d	�
j"��j!d
t%�
���
�fd�}	|j'|	�S)z�
        Simple test for the pid attribute of Process on win32.
        Launch process with mock win32process. The only mock aspect of this
        module is that the pid of the process created will always be 42.
        r��_dumbwin32proc)�mock_win32process�win32process�process_cmdline.pyzcmd.exe�/cN�*z<Process pid=42>c�<���j�j�yrr%)r�rLr s ��r"�
pidCompleteCbz2DumbWin32ProcTests.test_pid.<locals>.pidCompleteCb�	s������a�e�e�$r%)�twisted.internetr��twisted.testr�rr�__file__�siblingrrr_rr�rpr�rrJr'�reprr
)r r�r�rr�rv�processProto�comspecr�r�rLs`         @r"�test_pidzDumbWin32ProcTests.test_pid�	s����	4�2��
�
�>�>�3D�E��h�'�/�/�0D�E�J�J�
�����(�-�-���N�N���-�a�0������e�Z�0���"�"�7�L�$��R��N������Q�U�U�#����+�T�!�W�5�	%��}�}�]�+�+r%c��ddlm}td�}|jd�||j�}|j|d�y)zP
        Look for the string after the shebang C{#!}
        in a file.
        r)�_findShebangsexample.cgis#!/usr/bin/pythonz/usr/bin/pythonN)�twisted.internet._dumbwin32procr�r�
setContentrrJ)r r��	cgiScript�programs    r"�test_findShebangz#DumbWin32ProcTests.test_findShebang�	s@��
	A��^�,�	����1�2��y�~�~�.������"3�4r%N)r/r0r1r2r�r�r3r%r"r�r��	s���,�6
5r%r�c�4�eZdZdZej
d��Zy)�Win32CreateProcessFlagsTestsz2
    Check the flags passed to CreateProcess.
    c#��	�
K�ddlm}g�	|jj�
�	�
fd�}|j	|jd|�t
j}tt�jd�}tj�}t|�}ttjd�}|d||j g}|j#t$|d|id�|��|j'�	|jj(g�y�w)	a�
        Verify that the flags passed to win32process.CreateProcess() prevent a
        new console window from being created. Use the following script
        to test this interactively::

            # Add the following lines to a script named
            #   should_not_open_console.pyw
            from twisted.internet import reactor, utils

            def write_result(result):
            open("output.log", "w").write(repr(result))
            reactor.stop()

            PING_EXE = r"c:\windows\system32\ping.exe"
            d = utils.getProcessOutput(PING_EXE, ["slashdot.org"])
            d.addCallbacks(write_result)
            reactor.run()

        To test this, run::

            pythonw.exe should_not_open_console.pyw
        rr�c	�F���	j|��
|||||||||�	S)a�
            See the Windows API documentation for I{CreateProcess} for further details.

            @param appName: The name of the module to be executed
            @param commandLine: The command line to be executed.
            @param processAttributes: Pointer to SECURITY_ATTRIBUTES structure or None.
            @param threadAttributes: Pointer to SECURITY_ATTRIBUTES structure or  None
            @param bInheritHandles: boolean to determine if inheritable handles from this
                                    process are inherited in the new process
            @param creationFlags: flags that control priority flags and creation of process.
            @param newEnvironment: pointer to new environment block for new process, or None.
            @param currentDirectory: full path to current directory of new process.
            @param startupinfo: Pointer to STARTUPINFO or STARTUPINFOEX structure
            @return: True on success, False on failure
            @rtype: L{bool}
            r>)�appName�commandLine�processAttributes�threadAttributes�bInheritHandles�
creationFlags�newEnvironment�currentDirectory�startupinfor=�realCreateProcesss         ��r"�fakeCreateprocesszBWin32CreateProcessFlagsTests.test_flags.<locals>.fakeCreateprocess�	s9���6
�L�L��'�$���!� ���� ��
�

r%�
CreateProcessr��COMSPECr�N)r�r�r�r�rrr_rr�r�rr�rpr�r�r`rr�rrJ�CREATE_NO_WINDOW)r r�r��exerrvr�r�r�r=r�s         @@r"�
test_flagsz'Win32CreateProcessFlagsTests.test_flags�	s������0	4���*�7�7�E�E��&	�P	
�
�
�>�.�.��AR�S��n�n���h�'�/�/�0D�E�
��N�N���-�a�0���b�j�j��+�,����c�:�?�?�3�����w��d�C��T�J��������!<�!<�!M�!M� N�O�s�DDN)r/r0r1r2r�inlineCallbacksr�r3r%r"r�r��	s%������NP��NPr%r�c�.�eZdZdZd�Zd�Zd�Zd�Zd�Zy)�	UtilTestsz\
    Tests for process-related helper functions (currently only
    L{procutils.which}.
    c��tjj}|j�}||d�|_||d�|_||jd�|_||jd�|_||j
d�|_||j
d�|_	|j|j|j|jfD]}tj|��||jd�df||jd�df||jd�df||jd�df||jd�dffD]5\}}t|d�j�tj||��7tjjd	d
�|_tj"j|j|j|j|jf�tjd	<y
)z�
        Create several directories and files, some of which are executable
        and some of which are not.  Save the current PATH setting.
        rk�baz�barr_i��executable.binrrEr�N)r�rr��mktemprkr��foobar�foobaz�bazfoo�bazbar�makedirsr>r~�chmodr`ra�oldPathrb)r �j�baserv�name�modes      r"ruzUtilTests.setUp"
s���

�G�G�L�L���{�{�}���T�5�>����T�5�>�������%�(�������%�(�������%�(�������%�(������d�k�k�4�;�;����C�	�A��K�K��N�	��t�{�{�L�
)�5�1�
�t�x�x��
&��.�
�t�{�{�L�
)�5�1�
�t�{�{�,�
-�u�5�
�t�{�{�L�
)�1�-�
�	!�J�D�$�
��t��"�"�$��H�H�T�4� �	!��z�z�~�~�f�d�3����Z�Z�_�_�
�[�[�$�+�+�t�{�{�D�K�K�@�
��
�
�6�r%c�2�tjtjj|jd�t
j�|j�	tjd=y|jtjd<y#t$rYywxYw)z�
        Restore the saved PATH setting, and set all created files readable
        again so that they can be deleted easily.
        r_Nr�)
r�r�rr�r��stat�S_IWUSRr�r`�KeyErrorr)s r"r�zUtilTests.tearDownD
sl��
	��������d�k�k�<�8�$�,�,�G��<�<��
��J�J�v�&�"&���B�J�J�v����
��
�s�B
�
	B�Bc�p�tjd=|jtjd�g�y)z}
        Test that if C{os.environ} does not have a C{'PATH'} key,
        L{procutils.which} returns an empty list.
        r�r_N)r�r`rJrr�r)s r"�test_whichWithoutPATHzUtilTests.test_whichWithoutPATHR
s)��

�J�J�v���������6��;r%c�R�tjj}tjd�}||j
d�||jd�g}tjj�r"|j||jd��|j||�y)Nr_)
r�rr�rr�r�r�rrc�	isWindowsr?r�rJ)r r��paths�
expectedPathss    r"�
test_whichzUtilTests.test_whichZ
sx���G�G�L�L������-���4�;�;��5�q����l�7S�T�
����%�%�'�� � ��4�;�;��!=�>�����
�.r%c��tjj}tjj	dd�}tj
jd�tjd<	t
jd�}|�tjd=n|tjd<	||jd�||jd�||jd�g}tjj�r"|j||jd��|j||�y#|�tjd=w|tjd<wxYw)N�PATHEXT)z.binz.exez.shr_r�)r�rr�r`rarbrr�r�r�rrcr�r?r�rJ)r r��oldr�r�s     r"�test_whichPathExtzUtilTests.test_whichPathExtb
s���G�G�L�L���j�j�n�n�Y��-�� "�
�
���0G� H��
�
�9��	,��O�O�L�1�E��{��J�J�y�)�(+��
�
�9�%�
�d�k�k�<�(�
�d�k�k�<�(�
�d�k�k�+�,�
�
�
���%�%�'�� � ��4�;�;��!=�>�����
�.���{��J�J�y�)�(+��
�
�9�%�s�,D4�4)EN)	r/r0r1r2rur�r�r�r�r3r%r"r�r�
s!���
 
�D.�<�/�/r%r�c�,�eZdZdZdZd�Zd�Zd�Zd�Zy)�ClosingPipesProcessProtocolr%c�D�tj�|_||_yr)rr�rr�outOrErr)r r�s  r"rwz$ClosingPipesProcessProtocol.__init__{
s�����(��
� ��
r%c�:�|jj|�yr)rrr{r|s  r"r}z(ClosingPipesProcessProtocol.processEnded
s���
�
���v�&r%c�.�|xj|z
c_yr)�outputrs  r"r#z'ClosingPipesProcessProtocol.outReceived�
������t��r%c�.�|xj|z
c_yr)�errputrs  r"r'z'ClosingPipesProcessProtocol.errReceived�
r�r%N)	r/r0r1r�r�rwr}r#r'r3r%r"r�r�w
s ��
�F�
�F�!�'��r%r�c�$�eZdZd�Zd�Zd�Zd�Zy)�ClosingPipesTestsc�*�td�}|j|jtj�|jj|j|�tj|ttddtd|fz�gd��|dk(r|jj�n&|dk(r|jj�nt�|jjd	�|jj!�|jS)
a
        Create a child process and close one of its output descriptors using
        L{IProcessTransport.closeStdout} or L{IProcessTransport.closeStderr}.
        Return a L{Deferred} which fires after verifying that the descriptor was
        really closed.
        Tr"r�zpinput()
import sys, os, time
for i in range(1000):
    os.write(%d, b"foo\n")
    time.sleep(0.01)
sys.exit(42)
Nr�rGrSsgo
)r��
assertFailurerrrr�r
�_endProcessrr�r�rr��closeStdout�closeStderrr�r�r�)r rBrLs   r"�doitzClosingPipesTests.doit�
s���
(��-�����1�:�:�u�'>�'>�?�	�
�
���t�/�/��3����
������	%�)+�u�	-��	
�"�)	
�.��7�
�K�K�#�#�%�
�1�W�
�K�K�#�#�%���	
�����'�"�	
����� ��z�z�r%c��|j|jdd|z�|j|jd�|jS)zo
        Check that a failed write prevented the process from getting to its
        custom exit code.
        r�zprocess reason was %rr%)�assertNotEqualr�rJr�r�)r rzrLs   r"r�zClosingPipesTests._endProcess�
s=��	
���F�O�O�R�1H�6�1Q�R�������3�'��x�x�r%c�V���jd�}�fd�}|j|�|S)zQ
        ProcessProtocol.transport.closeStdout actually closes the pipe.
        rGc���tjj�r%�jd|��jd|�n�jd|�tjj	�dk7r�jd|�yy)NsOSErrors22sBrokenPipeErrorr0sBroken pipe)rrcr�r�rd�r�r s �r"rFz-ClosingPipesTests.test_stdout.<locals>._check�
sj������)�)�+��
�
�j�&�1��
�
�e�V�,��
�
�0�&�9����'�'�)�W�4��
�
�n�f�5�5r%�r�r
�r rvrFs`  r"�test_stdoutzClosingPipesTests.test_stdout�
s*���
�I�I�a�L��	6�	
�
�
�f���r%c�V���jd�}�fd�}|j|�|S)zQ
        ProcessProtocol.transport.closeStderr actually closes the pipe.
        rSc�*���j|d�yr[r�r�s �r"rFz-ClosingPipesTests.test_stderr.<locals>._check�
s���
���V�S�)r%r�r�s`  r"rAzClosingPipesTests.test_stderr�
s*���
�I�I�a�L��	*�
	
�
�
�f���r%N)r/r0r1r�r�r�rAr3r%r"r�r��
s��
-�^��$r%r�)[r2r�rOrCr�r�r�r�rrrrr9�ImportErrorr�r�_process�twisted.internet.processrrr�objectrr	�zope.interface.verifyr
rrr
rr�twisted.pythonrr�twisted.python.compatr�twisted.python.filepathr�twisted.python.logr�
twisted.trialr_rr�rHrar`rrbr�r8r�TestCaser5rpr�r�r�r�r�r�r��IReactorProcessr�rfrorcrdr�r�r�r�r
r�rr"r�r�r�r�r�r;rJrVrYr}r�r�r�r�r�r3r%r"�<module>r
s����"
�	���	�
��
�%���	�4�Q�Q��G��.�H�H�-�/�,�"�"�	���� �%�%�� "�������	��*�*�/�/�#�(�(�3�	�,��$�(�2�2�$�.V'�8�,�,�V'�r"�X�5�5�"�8(%�(�2�2�(%�V $�8�+�+� $�FE%�X�-�-�E%�P	*�1�	*�A@�X�5�5�A@�H-�.�-�$�5��>�"�
�"�"�7�D�1�1�-��T+�8�$�$�T+�	�T+�n%��1�1�%�(�(�V���� � �"�g�-�/E�F��"�
�"�"�7�D�1�1�-��	� 4�h�6G�6G�	�	�G�
	����� � �"�g�-�/L�M��"�
�"�"�7�D�1�1�-��,�1�8�3D�3D�,�	�N�
,�^I%��(�(�I%�X���� � �"�g�-�/L�M��"�
�"�"�7�D�1�1�-��!+�h���!+�	�N�
!+�H�(�*�*��:h�h�V	%�	%�N�N�b�
���
���Z����z2�x�(�(�z2�z
���� � �"�g�-�/L�M��"�
�"�"�7�D�1�1�-��0+��)�)�+;�0+�	�N�
0+�f���� � �"�g�-�/L�M��"�
�"�"�7�D�1�1�-��4
�8�,�,�.>�4
�	�N�
4
�n%�.�%�(���� � �"�g�-�/E�F��"�
�"�"�7�D�1�1�-��P7��)�)�P7�	�G�
P7�f���� � �"�g�-�/E�F��"�
�"�"�7�D�1�1�-��/9�8�#4�#4�/9�	�G�
/9�d���� � �"�g�-�/E�F��"�
�"�"�7�D�1�1�-��*5��*�*�*5�	�G�
*5�Z���� � �"�g�-�/E�F��"�
�"�"�7�D�1�1�-��TP�8�#4�#4�TP�	�G�
TP�nX/��!�!�X/�v�(�":�":��$�"�
�"�"�7�D�1�1�-��X��)�)�X�	�X��YS���E������G��M��M��J�	�s"�W:�X�:X�X�
X�X

Zerion Mini Shell 1.0