Ë
    oj¹1  ã                   óD  — d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZ ddlmZmZ ddlmZ ddlmZ ddlmZ dd	lmZmZ d
Z eej4                  «      Zej8                  j;                  ej<                  «      ed<    G d„ de	j>                  «      Z  G d„ de«      Z!y)z¯
Tests for L{twisted.internet.stdio}.

@var properEnv: A copy of L{os.environ} which has L{bytes} keys/values on POSIX
    platforms and native L{str} keys/values on Windows.
é    N)ÚskipIf)ÚdeferÚerrorÚprotocolÚreactorÚstdio)ÚfilepathÚlog)ÚrequireModule)Úplatform)ÚConnectionLostNotifyingProtocol)ÚSkipTestÚTestCases   xyz123abc Twisted is great!Ú
PYTHONPATHc                   ó,   — e Zd ZdZdZd„ Zd„ Zd„ Zd„ Zy)ÚStandardIOTestProcessProtocolaÕ  
    Test helper for collecting output from a child process and notifying
    something when it exits.

    @ivar onConnection: A L{defer.Deferred} which will be called back with
    L{None} when the connection to the child process is established.

    @ivar onCompletion: A L{defer.Deferred} which will be errbacked with the
    failure associated with the child process exiting when it exits.

    @ivar onDataReceived: A L{defer.Deferred} which will be called back with
    this instance whenever C{childDataReceived} is called, or L{None} to
    suppress these callbacks.

    @ivar data: A C{dict} mapping file descriptors to strings containing all
    bytes received from the child process on each file descriptor.
    Nc                 óv   — t        j                  «       | _        t        j                  «       | _        i | _        y ©N)r   ÚDeferredÚonConnectionÚonCompletionÚdata©Úselfs    ú9/usr/lib/python3/dist-packages/twisted/test/test_stdio.pyÚ__init__z&StandardIOTestProcessProtocol.__init__8   s'   € Ü!ŸN™NÓ,ˆÔÜ!ŸN™NÓ,ˆÔØˆ�	ó    c                 ó:   — | j                   j                  d «       y r   )r   Úcallbackr   s    r   ÚconnectionMadez,StandardIOTestProcessProtocol.connectionMade=   s   € Ø×Ñ×"Ñ" 4Õ(r   c                 óÀ   — | j                   j                  |d«      |z   | j                   |<   | j                  �&| j                  dc}| _        |j                  | «       yy)z“
        Record all bytes received from the child process in the C{data}
        dictionary.  Fire C{onDataReceived} if it is not L{None}.
        r   N)r   ÚgetÚonDataReceivedr   )r   ÚnameÚbytesÚds       r   ÚchildDataReceivedz/StandardIOTestProcessProtocol.childDataReceived@   sW   € ð
 Ÿ)™)Ÿ-™-¨¨cÓ2°UÑ:ˆ�	‰	�$‰Ø×ÑÐ*Ø%)×%8Ñ%8¸$Ð"ˆAˆtÔ"Ø�J‰J�tÕð +r   c                 ó:   — | j                   j                  |«       y r   )r   r   )r   Úreasons     r   ÚprocessEndedz*StandardIOTestProcessProtocol.processEndedJ   s   € Ø×Ñ×"Ñ" 6Õ*r   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r#   r   r    r'   r*   © r   r   r   r   #   s"   „ ñð$ €Nòò
)òó+r   r   c                   óÀ   — e Zd Z ej                  «       r
 ed«      €dZd„ Zd„ Zd„ Z	d„ Z
d„ Zd	„ Zd
„ Zd„ Zd„ Zd„ Zd„ Z e ej                  «       d«      d„ «       Zy)ÚStandardInputOutputTestsÚwin32processNzIOn windows, spawnProcess is not available in the absence of win32process.c                 óÐ   — t         j                  dd|z   t        j                  j                  gt        |«      z   }t        j                  |t         j                  |fdt        i|¤ŽS )a_  
        Launch a child Python process and communicate with it using the
        given ProcessProtocol.

        @param proto: A L{ProcessProtocol} instance which will be connected
        to the child process.

        @param sibling: The basename of a file containing the Python program
        to run in the child process.

        @param *args: strings which will be passed to the child process on
        the command line as C{argv[2:]}.

        @param **kw: additional arguments to pass to L{reactor.spawnProcess}.

        @return: The L{IProcessTransport} provider for the spawned process.
        s   -ms   twisted.test.Úenv)ÚsysÚ
executabler   Ú	__class__r,   ÚlistÚspawnProcessÚ	properEnv)r   ÚprotoÚsiblingÚargsÚkws        r   Ú_spawnProcessz&StandardInputOutputTests._spawnProcessU   s`   € ô& �N‰NØØ˜wÑ&Ü×Ñ×(Ñ(ð	
ô
 �‹Jñˆô ×#Ñ# E¬3¯>©>¸4ÑUÄYÐUÐRTÑUÐUr   c                 ó>   ‡ ‡— ˆ fd„}ˆfd„}|j                  ||«      S )Nc                 ó.   •— ‰j                  d| ›�«       y )Nz%Process terminated with non-Failure: )Úfail)Úresultr   s    €r   Úcbz4StandardInputOutputTests._requireFailure.<locals>.cbp   s   ø€ Ø�I‰IÐ=¸f¸ZÐHÕIr   c                 ó   •—  ‰| «      S r   r/   )Úerrr   s    €r   Úebz4StandardInputOutputTests._requireFailure.<locals>.ebs   s   ø€ Ù˜C“=Ð r   )ÚaddCallbacks)r   r&   r   rD   rG   s   ` `  r   Ú_requireFailurez(StandardInputOutputTests._requireFailureo   s!   ù€ ô	Jô	!ð �~‰~˜b "Ó%Ð%r   c                 óÜ   ‡ ‡‡— ‰ j                  «       Št        j                  d‰z   «       t        «       Š‰j                  }‰ j                  ‰d‰«       ˆˆˆ fd„}‰ j                  ||«      S )z„
        Verify that a protocol connected to L{StandardIO} can disconnect
        itself using C{transport.loseConnection}.
        úChild process logging to s   stdio_test_loseconnc                 ó  •— t        ‰«      5 }|D ](  }t        j                  d|j                  «       z   «       Œ* 	 d d d «       ‰j	                  d‰j
                  «       | j                  t        j                  «       y # 1 sw Y   ŒExY w)NzChild logged: é   )	Úopenr
   ÚmsgÚrstripÚfailIfInr   Útrapr   ÚProcessDone)r)   ÚfÚlineÚerrorLogFileÚpr   s      €€€r   r*   zBStandardInputOutputTests.test_loseConnection.<locals>.processEndedƒ   so   ø€ ä�lÓ#ð > qØò >�DÜ—G‘GÐ,¨t¯{©{«}Ñ<Õ=ñ>÷>ð �M‰M˜!˜QŸV™VÔ$Ø�K‰Kœ×)Ñ)Õ*÷>ð >ús   �.B Â B	)Úmktempr
   rO   r   r   r?   rI   )r   r&   r*   rV   rW   s   `  @@r   Útest_loseConnectionz,StandardInputOutputTests.test_loseConnectionx   s`   ú€ ð
 —{‘{“}ˆÜ�‰Ð+¨lÑ:Ô;Ü)Ó+ˆØ�N‰NˆØ×Ñ˜1Ð4°lÔCö	+ð ×#Ñ# A |Ó4Ð4r   c                 óB  ‡— | j                  «       }t        j                  d|z   «       t        «       Št	        j
                  «       ‰_        ˆfd„}‰j                  j                  |«       d„ }| j                  ‰j                  |«      }| j                  ‰d|«       |S )z´
        When stdin is closed and the protocol connected to it implements
        L{IHalfCloseableProtocol}, the protocol's C{readConnectionLost} method
        is called.
        rK   c                 óT   •— ‰j                   }‰j                  j                  «        |S r   )r   Ú	transportÚ
closeStdin)Úignoredr&   rW   s     €r   ÚcbByteszAStandardInputOutputTests.test_readConnectionLost.<locals>.cbBytes™   s!   ø€ Ø—‘ˆAØ�K‰K×"Ñ"Ô$ØˆHr   c                 óB   — | j                  t        j                  «       y r   )rR   r   rS   )r)   s    r   r*   zFStandardInputOutputTests.test_readConnectionLost.<locals>.processEnded    s   € Ø�K‰Kœ×)Ñ)Õ*r   s   stdio_test_halfclose)
rX   r
   rO   r   r   r   r#   ÚaddCallbackrI   r?   )r   rV   r_   r*   r&   rW   s        @r   Útest_readConnectionLostz0StandardInputOutputTests.test_readConnectionLostŽ   s‹   ø€ ð —{‘{“}ˆÜ�‰Ð+¨lÑ:Ô;Ü)Ó+ˆÜ Ÿ>™>Ó+ˆÔô	ð
 	
×Ñ×$Ñ$ WÔ-ò	+ð × Ñ  ×!1Ñ!1°<Ó@ˆà×Ñ˜1Ð5°|ÔDØˆr   c                 óÜ   ‡ ‡— t        «       Š	 ‰ j                  ‰dt        d¬«       ˆˆ fd„}‰ j                  ‰j                  |«      S # t        $ r}t	        t        |«      «      ‚d}~ww xY w)z¶
        Verify that a write made directly to stdout using L{os.write}
        after StandardIO has finished is reliably received by the
        process reading that stdout.
        s   stdio_test_lastwriteT)ÚusePTYNc                 óÄ   •— ‰j                  ‰j                  d   j                  t        «      d‰j                  ›d�«       | j	                  t
        j                  «       y)z‰
            Asserts that the parent received the bytes written by the child
            immediately after the child starts.
            rM   z	Received z) from child, did not find expected bytes.N)Ú
assertTruer   ÚendswithÚUNIQUE_LAST_WRITE_STRINGrR   r   rS   ©r)   rW   r   s    €€r   r*   zEStandardInputOutputTests.test_lastWriteReceived.<locals>.processEndedÂ   sM   ø€ ð
 �O‰OØ—‘�q‘	×"Ñ"Ô#;Ó<Ø˜AŸF™F˜:Ð%NÐOôð �K‰Kœ×)Ñ)Õ*r   )r   r?   rh   Ú
ValueErrorr   ÚstrrI   r   )r   Úer*   rW   s   `  @r   Útest_lastWriteReceivedz/StandardInputOutputTests.test_lastWriteReceived¨   so   ù€ ô *Ó+ˆð	#Ø×ÑØÐ*Ô,DÈTð ô õ		+ð ×#Ñ# A§N¡N°LÓAÐAøô ò 	#äœ3˜q›6Ó"Ð"ûð	#ús   ŽA	 Á		A+ÁA&Á&A+c                 ó†   ‡ ‡— t        «       Š‰j                  }‰ j                  ‰d«       ˆˆ fd„}‰ j                  ||«      S )zƒ
        Verify that the transport of a protocol connected to L{StandardIO}
        has C{getHost} and C{getPeer} methods.
        s   stdio_test_hostpeerc                 óÈ   •— ‰j                   d   j                  «       \  }}‰j                  |«       ‰j                  |«       | j                  t        j
                  «       y )NrM   )r   Ú
splitlinesrf   rR   r   rS   )r)   ÚhostÚpeerrW   r   s      €€r   r*   z?StandardInputOutputTests.test_hostAndPeer.<locals>.processEndedØ   sG   ø€ ØŸ™ ™×-Ñ-Ó/‰JˆD�$Ø�O‰O˜DÔ!Ø�O‰O˜DÔ!Ø�K‰Kœ×)Ñ)Õ*r   ©r   r   r?   rI   ©r   r&   r*   rW   s   `  @r   Útest_hostAndPeerz)StandardInputOutputTests.test_hostAndPeerÏ   sA   ù€ ô
 *Ó+ˆØ�N‰NˆØ×Ñ˜1Ð4Ô5õ	+ð ×#Ñ# A |Ó4Ð4r   c                 ó†   ‡ ‡— t        «       Š‰j                  }‰ j                  ‰d«       ˆˆ fd„}‰ j                  ||«      S )z�
        Verify that the C{write} method of the transport of a protocol
        connected to L{StandardIO} sends bytes to standard out.
        s   stdio_test_writec                 ó‚   •— ‰j                  ‰j                  d   d«       | j                  t        j                  «       y ©NrM   s   ok!©ÚassertEqualr   rR   r   rS   ri   s    €€r   r*   z9StandardInputOutputTests.test_write.<locals>.processEndedê   ó-   ø€ Ø×Ñ˜QŸV™V A™Y¨Ô/Ø�K‰Kœ×)Ñ)Õ*r   rs   rt   s   `  @r   Ú
test_writez#StandardInputOutputTests.test_writeà   sA   ù€ ô
 *Ó+ˆØ�N‰Nˆà×Ñ˜1Ð1Ô2õ	+ð ×#Ñ# A |Ó4Ð4r   c                 ó†   ‡ ‡— t        «       Š‰j                  }‰ j                  ‰d«       ˆˆ fd„}‰ j                  ||«      S )z˜
        Verify that the C{writeSequence} method of the transport of a
        protocol connected to L{StandardIO} sends bytes to standard out.
        s   stdio_test_writeseqc                 ó‚   •— ‰j                  ‰j                  d   d«       | j                  t        j                  «       y rx   ry   ri   s    €€r   r*   zAStandardInputOutputTests.test_writeSequence.<locals>.processEndedú   r{   r   rs   rt   s   `  @r   Útest_writeSequencez+StandardInputOutputTests.test_writeSequenceð   sA   ù€ ô
 *Ó+ˆØ�N‰Nˆà×Ñ˜1Ð4Ô5õ	+ð ×#Ñ# A |Ó4Ð4r   c                 ó¶   — | j                  «       }t        |d«      5 }t        d«      D ]  }|j                  d|fz  «       Œ 	 d d d «       |S # 1 sw Y   |S xY w)NÚwbi   ó   %d
)rX   rN   ÚrangeÚwrite)r   ÚjunkPathÚjunkFileÚis       r   Ú	_junkPathz"StandardInputOutputTests._junkPath   s\   € Ø—;‘;“=ˆÜ�(˜DÓ!ð 	/ XÜ˜4“[ò /�Ø—‘˜w¨!¨™~Õ.ñ/÷	/ð ˆ÷	/ð ˆús   �&AÁAc                 ó  ‡ ‡‡‡‡‡— t        «       Š‰j                  }g Št        t        d«      «      Šˆˆˆˆfd„Š‰ j	                  ‰d«      Š‰j
                  j                  ‰«       ˆˆ ˆˆfd„}‰ j                  ||«      S )z€
        Verify that the transport of a protocol connected to L{StandardIO}
        is a working L{IProducer} provider.
        éd   c                 ó¨   •— ‰rO‰j                  d‰j                  «       fz  «       ‰j                  ‰d   «       t        j                  d‰d «       y y )Nr‚   éÿÿÿÿg{®Gáz„?)ÚappendÚpopr„   r   Ú	callLater)Úignr    ÚprocÚtoWriteÚwrittens    €€€€r   r    z>StandardInputOutputTests.test_producer.<locals>.connectionMade  sG   ø€ ÙØ—‘˜w¨'¯+©+«-Ð)9Ñ9Ô:Ø—
‘
˜7 2™;Ô'Ü×!Ñ! $¨¸Õ=ð r   s   stdio_test_producerc                 óÞ   •— ‰j                  ‰j                  d   dj                  ‰«      «       ‰j                  ‰dt	        ‰«      fz  «       | j                  t        j                  «       y )NrM   r   z*Connection lost with %d writes left to go.)rz   r   ÚjoinÚassertFalseÚlenrR   r   rS   )r)   rW   r   r’   r“   s    €€€€r   r*   z<StandardInputOutputTests.test_producer.<locals>.processEnded  sW   ø€ Ø×Ñ˜QŸV™V A™Y¨¯©°Ó(9Ô:Ø×ÑØÐEÌÈWËÈÑWôð �K‰Kœ×)Ñ)Õ*r   )r   r   r8   rƒ   r?   r   ra   rI   )r   r&   r*   r    rW   r‘   r’   r“   s   `  @@@@@r   Útest_producerz&StandardInputOutputTests.test_producer  so   ý€ ô
 *Ó+ˆØ�N‰NˆàˆÜ”u˜S“zÓ"ˆ÷	>ð ×!Ñ! !Ð%;Ó<ˆà	�‰×"Ñ" >Ô2÷	+ð ×#Ñ# A |Ó4Ð4r   c                 ó¬   ‡ ‡‡— t        «       Š‰j                  }‰ j                  «       Š‰ j                  ‰d‰«       ˆˆˆ fd„}‰ j	                  ||«      S )z€
        Verify that the transport of a protocol connected to L{StandardIO}
        is a working L{IConsumer} provider.
        s   stdio_test_consumerc                 óà   •— t        ‰d«      5 }‰j                  ‰j                  d   |j                  «       «       d d d «       | j	                  t
        j                  «       y # 1 sw Y   Œ)xY w)NÚrbrM   )rN   rz   r   ÚreadrR   r   rS   )r)   rT   r…   rW   r   s     €€€r   r*   z<StandardInputOutputTests.test_consumer.<locals>.processEnded1  sT   ø€ Ü�h Ó%ð 6¨Ø× Ñ  §¡¨¡¨A¯F©F«HÔ5÷6à�K‰Kœ×)Ñ)Õ*÷6ð 6ús   Ž.A$Á$A-)r   r   rˆ   r?   rI   )r   r&   r*   r…   rW   s   `  @@r   Útest_consumerz&StandardInputOutputTests.test_consumer%  sO   ú€ ô
 *Ó+ˆØ�N‰Nˆà—>‘>Ó#ˆà×Ñ˜1Ð4°hÔ?ö	+ð
 ×#Ñ# A |Ó4Ð4r   zpStandardIO does not accept stdout as an argument to Windows.  Testing redirection to a file is therefore harder.c                 óÖ  ‡ ‡‡	‡
‡‡— t        j                  «       }t        |«      }t        j                  ‰ j                  «       «      Š‰j                  d«      x‰ _        }‰ j                  |j                  «       t        |j                  «       ¬«      }t        j                  «       s\t        j                  «       \  }}‰ j                  t        j                  |«       ‰ j                  t        j                  |«       ||d<   t!        j"                  |fi |¤ŽŠdŠ
t%        j&                  «       Š	ˆˆ	ˆ
ˆfd„Št)        j*                  d‰«       ˆ	ˆ
ˆˆ fd„}|j-                  |«       |S )aE  
        If L{StandardIO} is created with a file descriptor which refers to a
        normal file (ie, a file from the filesystem), L{StandardIO.write}
        writes bytes to that file.  In particular, it does not immediately
        consider the file closed or call its protocol's C{connectionLost}
        method.
        r�   )ÚstdoutÚstdiné   c                  óš   •— ‰D ]/  } | ‰k(  r‰j                  «         y ‰j                  d| fz  «        n t        j                  d‰«       y )Nó   %dr   )ÚloseConnectionr„   r   r�   )ÚvalueÚ
connectionÚcountÚhowManyÚspins    €€€€r   r©   zAStandardInputOutputTests.test_normalFileStandardOut.<locals>.spin\  sS   ø€ Øò �Ø˜GÒ#Ø×-Ñ-Ô/ÙØ× Ñ  ¨%¨Ñ!1Ô2Ùðô ×Ñ˜a Õ&r   r   c           	      óÀ   •— ‰j                  t        ‰«      ‰dz   «       ‰j                  ‰j                  «       dj                  d„ t	        ‰«      D «       «      «       y )NrM   r   c              3   ó(   K  — | ]
  }d |fz  –— Œ y­w)r£   Nr/   )Ú.0r‡   s     r   ú	<genexpr>zVStandardInputOutputTests.test_normalFileStandardOut.<locals>.cbLost.<locals>.<genexpr>l  s   è ø€ Ò+Q¸Q¨E°Q°D­LÑ+Qùs   ‚)rz   ÚnextÚ
getContentr•   rƒ   )r)   r§   r¨   Úpathr   s    €€€€r   ÚcbLostzCStandardInputOutputTests.test_normalFileStandardOut.<locals>.cbLosti  sH   ø€ Ø×ÑœT %›[¨'°A©+Ô6Ø×ÑØ—‘Ó! 3§8¡8Ñ+QÄ%ÈÃ.Ô+QÓ#Qõr   )r   r   r   r	   ÚFilePathrX   rN   ÚnormalÚ
addCleanupÚcloseÚdictÚfilenor   Ú	isWindowsÚosÚpiper   Ú
StandardIOÚ	itertoolsr§   r   r�   ra   )r   Ú
onConnLostr;   r³   ÚkwargsÚrÚwr±   r¦   r§   r¨   r°   r©   s   `       @@@@@r   Útest_normalFileStandardOutz3StandardInputOutputTests.test_normalFileStandardOut8  s	  ý€ ô —^‘^Ó%ˆ
Ü/°
Ó;ˆÜ× Ñ  §¡£Ó/ˆØ#Ÿy™y¨›Ð.ˆŒ�fØ�‰˜Ÿ™Ô%ä˜VŸ]™]›_Ô-ˆÜ×!Ñ!Ô#ô —7‘7“9‰DˆAˆqØ�O‰OœBŸH™H aÔ(Ø�O‰OœBŸH™H aÔ(ØˆF�7‰OÜ×%Ñ% eÑ6¨vÑ6ˆ
ð ˆÜ—‘Ó!ˆ÷	'ô 	×Ñ˜!˜TÔ"÷	ð 	×Ñ˜vÔ&ØÐr   )r+   r,   r-   r   r¸   r   Úskipr?   rI   rY   rb   rm   ru   r|   r   rˆ   r˜   r�   r   rÁ   r/   r   r   r1   r1   N   sŠ   „ Ø€x×ÑÔ¡¨nÓ =Ð Eð'ð 	ò
Vò4&ò5ò,ò4%BòN5ò"5ò 5ò ò5ò<5ñ& Øˆ×ÑÓð	=óñ
3óñ
3r   r1   )"r.   r¼   r¹   r5   Úunittestr   Útwisted.internetr   r   r   r   r   Útwisted.pythonr	   r
   Útwisted.python.reflectr   Útwisted.python.runtimer   Útwisted.test.test_tcpr   Útwisted.trial.unittestr   r   rh   r¶   Úenvironr:   Úpathsepr•   r°   ÚProcessProtocolr   r1   r/   r   r   ú<module>rÍ      s   ðñó Û 	Û 
Ý ç CÕ Cß (Ý 0Ý +Ý Aß 5ð :Ð á�—‘Ó€	ØŸ*™*Ÿ/™/¨#¯(©(Ó3€	ˆ,Ñ ô(+ H×$<Ñ$<ô (+ôVb˜xõ br   