Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/test/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyc


=Nc@sdZddlZddlZddlZddlmZddlmZmZddl	m
Z
ddlmZm
Z
mZmZmZddlmZdZdZe
jryddlZWqek
rd	ZqXnd
ejfdYZdejfd
YZdS(s&
Tests for L{twisted.internet.stdio}.
iN(tunittest(tfilepathtlog(tplatform(terrortdefertprotocoltstdiotreactor(tConnectionLostNotifyingProtocolsxyz123abc Twisted is great!sIOn windows, spawnProcess is not available in the absence of win32process.tStandardIOTestProcessProtocolcBs8eZdZdZdZdZdZdZRS(s
    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
    C{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 C{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.
    cCs+tj|_tj|_i|_dS(N(RtDeferredtonConnectiontonCompletiontdata(tself((s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyt__init__5scCs|jjddS(N(RtcallbacktNone(R((s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pytconnectionMade;scCsV|jj|d||j|<|jdk	rR|jd}|_|j|ndS(s
        Record all bytes received from the child process in the C{data}
        dictionary.  Fire C{onDataReceived} if it is not C{None}.
        tN(RtgettonDataReceivedRR(Rtnametbytestd((s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pytchildDataReceived?s cCs|jj|dS(N(R
R(Rtreason((s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pytprocessEndedJsN(	t__name__t
__module__t__doc__RRRRRR(((s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyR
!s			tStandardInputOutputTestCasecBseZeZdZdZdZdZdZdZ	dZ
dZdZd	Z
d
ZdZejjdkrd
e_nejrde_ndZejjdkrde_nRS(cOsddl}ttj}tjjtjjtjjtjj|j	|j
ddg|d<tjt
jt	j|jtjjgt|}tj|tj|d||S(s_
        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.
        iNt
PYTHONPATHRtenv(ttwistedtdicttostenvirontpathseptjointpathtabspathtdirnamet__file__Rtsyst
executableRtFilePathtsiblingRt	__class__RtlisttspawnProcess(RtprotoR0targstkwR#tsubenv((s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyt
_spawnProcessSs		!cs.fd}fd}|j||S(Ncsjd|fdS(Ns'Process terminated with non-Failure: %r(tfail(tresult(R(s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pytcbxscs
|S(N((terr(R(s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pytebzs(taddCallbacks(RRRR;R=((RRs;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyt_requireFailurewscsgjtjdtj}jdfd}j||S(s
        Verify that a protocol connected to L{StandardIO} can disconnect
        itself using C{transport.loseConnection}.
        sChild process logging to sstdio_test_loseconn.pycsUx+tD]}tjd|jq
Wjdj|jtjdS(NsChild logged: i(	tfileRtmsgtrstriptfailIfInRttrapRtProcessDone(Rtline(RtpterrorLogFile(s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyRs(tmktempRRAR
R
R8R?(RRR((RRGRHs;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyttest_loseConnections		cs|j}tjd|ttj_fd}jj|d}|jj|}|j	d||S(s
        When stdin is closed and the protocol connected to it implements
        L{IHalfCloseableProtocol}, the protocol's C{readConnectionLost} method
        is called.
        sChild process logging to csj}jj|S(N(R
t	transportt
closeStdin(tignoredR(RG(s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pytcbBytess	
cSs|jtjdS(N(RDRRE(R((s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyRssstdio_test_halfclose.py(
RIRRAR
RRRtaddCallbackR?R8(RRHRNRR((RGs;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyttest_readConnectionLosts		
csvtyjdtdtWn(tk
rP}tjt|nXfd}jj	|S(s
        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_lastwrite.pytusePTYcs>jjdjtdjf|jtjdS(s
            Asserts that the parent received the bytes written by the child
            immediately after the child starts.
            is4Received %r from child, did not find expected bytes.N(t
assertTrueRtendswithtUNIQUE_LAST_WRITE_STRINGRDRRE(R(RGR(s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyRs
(
R
R8RTtTruet
ValueErrorRtSkipTesttstrR?R
(RteR((RRGs;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyttest_lastWriteReceiveds	
csDtj}jdfd}j||S(s
        Verify that the transport of a protocol connected to L{StandardIO}
        has C{getHost} and C{getPeer} methods.
        sstdio_test_hostpeer.pycsGjdj\}}j|j||jtjdS(Ni(Rt
splitlinest
failUnlessRDRRE(Rthosttpeer(RRG(s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyRs

(R
R
R8R?(RRR((RGRs;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyttest_hostAndPeers
		csDtj}jdfd}j||S(s
        Verify that the C{write} method of the transport of a protocol
        connected to L{StandardIO} sends bytes to standard out.
        sstdio_test_write.pycs+jjdd|jtjdS(Nisok!(tassertEqualRRDRRE(R(RGR(s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyRs(R
R
R8R?(RRR((RGRs;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyt
test_writes
		csDtj}jdfd}j||S(s
        Verify that the C{writeSequence} method of the transport of a
        protocol connected to L{StandardIO} sends bytes to standard out.
        sstdio_test_writeseq.pycs+jjdd|jtjdS(Nisok!(R`RRDRRE(R(RGR(s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyRs(R
R
R8R?(RRR((RGRs;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyttest_writeSequences
		cCsW|j}t|d}x+tdD]}|jt|dq(W|j|S(Ntwis
(RIR@txrangetwriteRXtclose(RtjunkPathtjunkFileti((s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyt	_junkPaths
cstj}gtdfdjdjjfd}j||S(s
        Verify that the transport of a protocol connected to L{StandardIO}
        is a working L{IProducer} provider.
        idcsNrJjtjdjdtjddndS(Ns
ig{Gz?(tappendRXtpopReRt	callLaterR(tign(RttoWritetwrittentproc(s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyRssstdio_test_producer.pycsQjjddjjdtf|jtjdS(NiRs*Connection lost with %d writes left to go.(R`RR(tfailIftlenRDRRE(R(RoRRpRG(s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyR$s (R
R
trangeR8RROR?(RRR((RRoRRGRpRqs;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyt
test_producers		csVtj}jjdfd}j||S(s
        Verify that the transport of a protocol connected to L{StandardIO}
        is a working L{IConsumer} provider.
        sstdio_test_consumer.pycs7jjdtj|jtjdS(Ni(R`RR@treadRDRRE(R(RgRRG(s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyR7s#(R
R
RjR8R?(RRR((RgRRGs;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyt
test_consumer+s		cs.tj}t|}tjjjd_}j|j	t
d|j}tj
stj\}}jtj	|jtj	|||d<ntj||dtjfdtjdfd}|j||S(sE
        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.
        RctstdouttstdinicsSx<D]4}|kr'jdSjt|PqWtjddS(Ni(tloseConnectionReRXRRm(tvalue(tcountthowManyt
connectiontspin(s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyR\s

icsLjjdjjdjtttdS(NiR(R`tnextt
getContentR(tmapRXRt(R(R|R}RR)(s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pytcbLosths	(RRR	RR/RItopentnormalt
addCleanupRfR$tfilenoRt	isWindowsR%tpipeRt
StandardIOt	itertoolsR|RRmRO(Rt
onConnLostR4RtkwargstrRcR((R}RR)RR|R~s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyttest_normalFileStandardOut=s&

tEPollReactorsrepoll(7) does not support normal files.  See #4429.  This should be a todo but technical limitations prevent this.spStandardIO does not accept stdout as an argument to Windows.  Testing redirection to a file is therefore harder.cCstj|j}|jd}|j}|j|j|jtt	j
tjd|}|j
t|d|tj|jfdS(s
        Using StandardIO with epollreactor with stdout redirected to a
        normal file fails with a comprehensible error (until it is
        supported, when #4429 is resolved).  See also #2259 and #3442.
        RcRxsThis reactor does not support this type of file descriptor (fd %d, mode %d) (for example, epollreactor does not support normal files.  See #4429).N(RR/RIRRRRftassertRaisestRuntimeErrorRRRtProtocolR`RXR%tfstattst_mode(RR)Rtfdtexc((s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyt(test_normalFileStandardOutGoodEpollErrorzs	sNOnly epollreactor is expected to fail with stdout redirected to a normal file.(RRtskipWindowsNopywin32tskipR8R?RJRPRZR_RaRbRjRuRwRRR1RRR(((s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyR Os(	$				'								2	(RR%R-Rt
twisted.trialRttwisted.pythonRRttwisted.python.runtimeRttwisted.internetRRRRRttwisted.test.test_tcpR	RTRRRtwin32processtImportErrortProcessProtocolR
tTestCaseR (((s;/usr/lib/python2.7/dist-packages/twisted/test/test_stdio.pyt<module>s$(

.