Your IP : 172.28.240.42


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


ƇNc@sdZeZddlZddlZddlZddlZddlmZm	Z	ddl
mZddlm
Z
ddlmZmZddlmZddlmZdd	lmZdd
lmZmZddlmZmZddlmZdd
lm Z m!Z!ddlm"Z"defdYZ#defdYZ$de$fdYZ%e&j'e%j(de$fdYZ)e&j'e)j(defdYZ*dS(s2
Tests for implementations of L{IReactorProcess}.
iN(tTestCasetSkipTest(tReactorBuilder(tset(tmsgterr(tplatform(tFilePath(tutils(tIReactorProcesstIProcessTransport(tDeferredtsucceed(tProcessProtocol(tProcessDonetProcessTerminated(t_signalst _ShutdownCallbackProcessProtocolcBs)eZdZdZdZdZRS(s
    An L{IProcessProtocol} which fires a Deferred when the process it is
    associated with ends.

    @ivar received: A C{dict} mapping file descriptors to lists of bytes
        received from the child process on those file descriptors.
    cCs||_i|_dS(N(twhenFinishedtreceived(tselfR((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt__init__#s	cCs |jj|gj|dS(N(Rt
setdefaulttappend(Rtfdtbytes((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytchildDataReceived(scCs|jjddS(N(RtcallbacktNone(Rtreason((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytprocessEnded,s(t__name__t
__module__t__doc__RRR(((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRs		tProcessTestsBuilderBasecBseZdZegZdZdZdZdZdZ	dZ
dZee
dd
d
krxd	e_nd
ZdZdZRS(s
    Base class for L{IReactorProcess} tests which defines some tests which
    can be applied to PTY or non-PTY uses of C{spawnProcess}.

    Subclasses are expected to set the C{usePTY} attribute to C{True} or
    C{False}.
    cst}t|}|jj|tjtjddgd|j}|jtj	||j
fd|jdS(s
        L{IReactorProcess.spawnProcess} connects the protocol passed to it
        to a transport which provides L{IProcessTransport}.
        s-cttusePTYcs
jS(N(tstop(tignored(treactor(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt<lambda>SsN(RRtbuildReactortspawnProcesstsyst
executableR$t
assertTrueR
t
providedBytaddCallbackt
runReactor(Rtendedtprotocolt	transport((R'sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyttest_processTransportInterface<s		cs|jt}t|dtjdfd}j||jfd|j|jdj	j
ddS(s
        Helper for testing L{IProcessTransport} write functionality.  This
        method spawns a child process and gives C{write} a chance to write some
        bytes to it.  It then verifies that the bytes were actually written to
        it (by relying on the child process to echo them back).

        @param write: A two-argument callable.  This is invoked with a process
            transport and some bytes to write to it.
        shello, worlds2import sys
sys.stdout.write(sys.stdin.readline())
cs]jtjtjdg}y|Wn!tdd|jdnXdS(Ns-cs!Unhandled exception while writingtKILL(R*R+R,RRt
signalProcess(R3(twriteR2R'Rtprogram(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytstartupls
cs
jS(N(R%(R&(R'(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR(vsR#iN(R)RRtostlineseptcallWhenRunningR/R0tassertEqualtjoinR(RR7R1R9((R2R'RR7R8sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt
_writeTestWs
	


cCsd}|j|dS(s|
        L{IProcessTransport.write} writes the specified C{str} to the standard
        input of the child process.
        cSs|j|dS(N(R7(R3R((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR7sN(R?(RR7((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt
test_write|s	cCsd}|j|dS(s
        L{IProcessTransport.writeSequence} writes the specified C{list} of
        C{str} to the standard input of the child process.
        cSs|jt|dS(N(t
writeSequencetlist(R3R((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR7sN(R?(RR7((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyttest_writeSequences	cCsd}|j|dS(s
        L{IProcessTransport.writeToChild} writes the specified C{str} to the
        specified file descriptor of the child process.
        cSs|jd|dS(Ni(twriteToChild(R3R((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR7sN(R?(RR7((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyttest_writeToChilds	cs fd}j|dS(s
        L{IProcessTransport.writeToChild} raises L{KeyError} if passed a file
        descriptor which is was not set up by L{IReactorProcess.spawnProcess}.
        cs2zjt|jd|Wd|j|XdS(Ni
(tassertRaisestKeyErrorRDR7(R3R(R(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR7sN(R?(RR7((RsF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt"test_writeToChildBadFileDescriptorscs|j|jrd}ni}tjfd}tjtj|t}jt	|t
jt
jddgd|jd|jdj
s|jdng}|j|j|rdS|jfd	|j|j|dS(
s
        If, before the reactor is started with L{IReactorCore.run}, a
        process is started with L{IReactorProcess.spawnProcess} and
        terminates, the process is reaped once the reactor is started.
        csjdS(N(R(targs(tsignaled(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pythandlerss-cR#R$tchildFDsixs,Timed out waiting for child process to exit.Ncs
jS(N(R%(R&(R'(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR(s(R)R$Rt	threadingtEventtsignaltSIGCHLDRR*RR+R,twaittisSettfailR/RR0R-(RRLRKR1tresult((R'RJsF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyttest_spawnProcessEarlyIsReapeds*			"

RPs;Platform lacks SIGCHLD, early-spawnProcess test can't work.csdttdtd}dtffdY}jjj|tjtjd|gdj	fd}j
|jtj
fd	j
d
S(s
        The C{reason} argument passed to L{IProcessProtocol.processExited} is a
        L{ProcessTerminated} instance if the child process exits with a signal.
        tTERMtSIGsEimport sys
sys.stdout.write('x')
sys.stdout.flush()
sys.stdin.read()
tExitercs8eZfdZdZfdZdZRS(cs(td||f|jjdS(NschildDataReceived(%d, %r)(RR3R6(RRtdata(tsigName(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRscSstd|fdS(NschildConnectionLost(%d)(R(RR((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytchildConnectionLostscs%td|fj|gdS(NsprocessExited(%r)(RR(RR(texited(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt
processExitedscSstd|fdS(NsprocessEnded(%r)(R(RR((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRs(RR RR[R]R((RZR\(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRXs		s-cR$cs~|\}|jt|j}tjrTj|jdj|j	dn&j|jj|j	ddS(Ni(
ttrapRtvalueRt	isWindowstassertIdenticalRORR=texitCode(t.0tfailureR(RtsigNum(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytcbExiteds
	cs
jS(N(R%(tign(R'(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR(#sN(tgetattrRORR
R)R<R*R+R,R$R/t
addErrbackRR0(RtsourceRXRf((RZR\ReRR'sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyttest_processExitedWithSignals	


cs|jdddg}ttdd
d
k	}jj}||kre|retd|ntjjdkrtdngfd}j	||j
|jd	gd
S(s
        If a child process exits while a system call is in progress, the system
        call should not be interfered with.  In particular, it should not fail
        with EINTR.

        Older versions of Twisted installed a SIGCHLD handler on POSIX without
        using the feature exposed by the SA_RESTART flag to sigaction(2).  The
        most noticable problem this caused was for blocking reads and writes to
        sometimes fail with EINTR.
        tGlib2ReactortGtk2ReactortPortableGtkReactortsiginterrupts(%s is not supported without siginterruptt_installHandlerUsingSignals@_signals._installHandlerUsingSignal doesn't support this featurecs[zItjdtjf}tjdtjf}j|jWdjXdS(Ns$%s -c "import time; time.sleep(0.1)"s1%s -c "import time; time.sleep(0.5); print 'Foo'"(R:tpopenR+R,RtreadR%(tf1tf2(R'RT(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytfDs		sFoo
N(R)RhRORt	__class__RRRtinstallHandlerR<R0R=(RtskippedReactorsthasSigInterrupttreactorClassNameRu((R'RTsF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt'test_systemCallUninterruptedByChildExit(s


c	sddlm}|dkr+tdntt}|jjjj}dd|jfddd	f}fd
jdtffdY}|}j	j
|tjtjd
ddj
|gdjjdS(s
        A spawned process has only stdin, stdout and stderr open
        (file descriptor 3 is also reported as open, because of the call to
        'os.listdir()').
        i(tplatformTypetposixs$Test only applies to POSIX platformss
import sysssys.path.insert(0, '%s')s$from twisted.internet import processs-sys.stdout.write(str(process._listOpenFDs()))ssys.stdout.flush()csjd|dS(Ns[0, 1, 2, 3](R=(toutput(R(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytcheckOutputistProtocolcs,eZdZdZfdZRS(cSs
g|_dS(N(R~(R((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRoscSs|jj|dS(N(R~R(RRY((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytoutReceivedrscs,zdj|jWdjXdS(NR#(R>R~R%(RR(RR'(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRus(RR RRR((R'R(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRns		s-Wignores-cs
R$N(ttwisted.python.runtimeR|RRt__file__tparenttpathR)R
R<R*R+R,R>R$R0(RR|therettopRjRtproto((R'RRsF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyttest_openFileDescriptorsVs&
	
	
N(RR R!R	trequiredInterfacesR4R?R@RCRERHRURhRORtskipRkR{R(((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR"1s			%	
	
	
		6	E	.tProcessTestsBuildercBs}eZdZeZeejdjZ	e
jr?dZndZdZ
dZdZdZdZd	ZRS(
sp
    Builder defining tests relating to L{IReactorProcess} for child processes
    which do not have a PTY.
    sprocess_helper.pytwindowsR#cstitd6td6td6dtffdY}d}|jjj|tjtjd|gd|jfd	}j|fd
}j|d}j|j	t
fd}j||jd
S(s
        L{IProcessProtocol.childConnectionLost} is called each time a file
        descriptor associated with a child process is closed.
        iiitClosercs&eZfdZfdZRS(csj|dS(N(R(RR3(t	connected(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytmakeConnectionscs|jddS(N(RR(RtchildFD(tlost(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR[s(RR RR[((RR(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRsswimport os, sys
while 1:
    line = sys.stdin.readline().strip()
    if not line:
        break
    os.close(int(line))
s-cR$cs'jddjfdS(Ns2
icsS(N((Rg(R3(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR(s(R7R/(R3(R(R3sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytcbConnecteds
cs'jddjfdS(Ns1
icsS(N((Rg(R3(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR(s(R7R/(R3(R(R3sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt
lostSeconds
cSs|jddS(Ns
(R7(R3((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt	lostFirstscsjdS(N(R%(R&(R'(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytcbEndedsN(RR
R)R<R*R+R,R$R/RiRR0(RRRjRRRR((R'RRsF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyttest_childConnectionLosts$	$

	


cstgdtffdY}jjj|tjtjjdjgdj	fd}j
|jtj
fdj
dS(s
        L{IProcessProtocol.processEnded} is called after the child process
        exits and L{IProcessProtocol.childConnectionLost} is called for each of
        its file descriptors.
        tEndercs8eZdZfdZdZfdZRS(cSs%td||f|jjdS(NschildDataReceived(%d, %r)(RR3tloseConnection(RRRY((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRscs"td|fj|dS(NschildConnectionLost(%d)(RR(RR(R(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR[scSstd|fdS(NsprocessExited(%r)(R(RR((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR]scs%td|fj|gdS(NsprocessEnded(%r)(RR(RR(R1(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRs(RR RR[R]R((RR1(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRs		tchildR$cs?|\}|jtjttdddgdS(Niii(R^RR=R(RcRd(RR(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRs
cs
jS(N(R%(Rg(R'(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR(sN(RR
R)R<R*R+R,tkeepStdioOpenProgramtkeepStdioOpenArgR$R/RiRR0(RRR((R'RRR1sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyttest_processEndeds	


csttgdtffdY}jjj|tjtjjdjgdj	fd}j
|fd}j
|jtj
fdj
dS(	s
        L{IProcessProtocol.processExited} is called when the child process
        exits, even if file descriptors associated with the child are still
        open.
        tWaitercs2eZdZfdZfdZRS(cSstd||fdS(NschildDataReceived(%d, %r)(R(RRRY((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRscsDtd|fj|tdkr@jdndS(NschildConnectionLost(%d)i(RRtlenRR(RR(RtallLost(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR[s
cs2td|fj|g|jjdS(NsprocessExited(%r)(RRR3R(RR(R\(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR]s(RR RR[R]((RRR\(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRs	RR$cs;|\}|jttdfjgS(NscbExited; lost = %s(R^RRR=(RcRd(RRR(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRfs

cs)jttdddgdS(Niii(R=R(R&(RR(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt	cbAllLostscs
jS(N(R%(Rg(R'(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR(!sN(RR
R)R<R*R+R,RRR$R/RiRR0(RRRfR((R'RRRR\sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyttest_processExiteds"		"



cCsU|j}t|d}|jtjj|tj|jtjj|S(sj
        Write the given list of lines to a text file and return the absolute
        path to it.
        twt(	tmktemptfileR7R:R;R>tcloseRtabspath(RtsourceLinestscriptt
scriptFile((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytmakeSourceFile&s
 
csdjdtjfddfdgtjdjfdfdfd	}j|jd
S(s
        Spawning a process with an executable which is a script starting
        with an interpreter definition line (#!) uses that interpreter to
        evaluate the script.
        sthis is the shebang outputs#!%ss
import sysssys.stdout.write('%s')ssys.stdout.flush()icsZ|\}}}td|||fj|j|dj|ddS(NscbProcessExited((%r, %r, %d))R#i(RR=(RctoutRtcode(RtSHEBANG_OUTPUT(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytcbProcessExitedCs
csj|S(N(R%(tpassthrough(R'(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytshutdownIs
cs@tjd}|j|j|jtdS(NR'(RtgetProcessOutputAndValuetaddBothR/RiR(td(R'RRR(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytstartMs

N(RR+R,R:tchmodR)R<R0(RR((R'RRRRRsF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyttest_shebang2s


csddddddgjdjtttdd	jfd
fdfd}j|jd
S(sq
        Arguments given to spawnProcess are passed to the child process as
        originally intended.
        simport sys, os
try:
  import msvcrt
  msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
except ImportError:
  pass
for arg in sys.argv[1:]:
  sys.stdout.write(arg + chr(0))
  sys.stdout.flush()thellot"s 	|<>^&s"\\"hello\\"s"foo\ bar baz\""R#iics-|jd}|jj|dS(Nt(tsplittpopR=(R~(RRI(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pytprocessFinishedps
csj|S(N(R%(RT(R'(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRvs
csFtd}|jfd|j|jdS(Ncs#tjtjdgdS(Ns-cR'(RtgetProcessOutputR+R,(tdummy(R'RIRj(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR(|s(RRR/R(R(RIRRjRR'(sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt
spawnChildzs
N(RR>tmaptchrtxrangeR)R<R0(RR((RR'RRIRjRsF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt test_processCommandLineArgumentsWs	
(
(RR R!tFalseR$RRtsiblingRRRR`RRRRRRR(((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRs		3	.	2		%tPTYProcessTestsBuildercBsEeZdZeZejr'dZnejrCidd6Z	nRS(si
    Builder defining tests relating to L{IReactorProcess} for child processes
    which have a PTY.
    s"PTYs are not supported on Windows.s#OS X's poll() does not support PTYss(twisted.internet.pollreactor.PollReactor(
RR R!tTrueR$RR`RtisMacOSXRx(((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRs	tPotentialZombieWarningTestscBseZdZdZRS(sE
    Tests for L{twisted.internet.error.PotentialZombieWarning}.
    cCsvddlm}|j|j|jg}|j|ddt|j|ddd|jt|ddS(	s
        Accessing L{PotentialZombieWarning} via the
        I{PotentialZombieWarning} attribute of L{twisted.internet.error}
        results in a deprecation warning being emitted.
        i(terroritcategorytmessagestwisted.internet.error.PotentialZombieWarning was deprecated in Twisted 10.0.0: There is no longer any potential for zombie process.iN(ttwisted.internetRtPotentialZombieWarningt
flushWarningsttest_deprecatedR=tDeprecationWarningR(RRtwarnings((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRs(RR R!R(((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRs(+R!ttypet
__metaclass__R:R+RORMttwisted.trial.unittestRRt#twisted.internet.test.reactormixinsRttwisted.python.compatRttwisted.python.logRRRRttwisted.python.filepathRRRttwisted.internet.interfacesR	R
ttwisted.internet.deferRRttwisted.internet.protocolR
ttwisted.internet.errorRRRRR"RtglobalstupdatetmakeTestCaseClassesRR(((sF/usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt<module>s.0S