
9Mc           @   s  d  Z  e Z d d l Z d d l Z d d l Z d d l Z d d l m Z m	 Z	 d d l
 m Z d d l 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 d l m Z m Z d d l m Z d d l m  Z  m! Z! d d l m" Z" d d l# m$ Z$ e$ j j%   rMd d l m& Z& n  d e f d     YZ' d e f d     YZ( d e( f d     YZ) e*   j+ e) j,    d e( f d     YZ- e*   j+ e- j,    d e f d     YZ. d e/ f d     YZ0 d e f d     YZ1 d S(   s2   
Tests for implementations of L{IReactorProcess}.
iN(   t   TestCaset   SkipTest(   t   ReactorBuilder(   t   set(   t   msgt   err(   t   platform(   t   FilePath(   t   utils(   t   IReactorProcesst   IProcessTransport(   t   Deferredt   succeed(   t   ProcessProtocol(   t   ProcessDonet   ProcessTerminated(   t   _signals(   t   runtime(   t   processt    _ShutdownCallbackProcessProtocolc           B   s    e  Z d  Z d   Z d   Z RS(   sh   
    An L{IProcessProtocol} which fires a Deferred when the process it is
    associated with ends.
    c         C   s   | |  _  d  S(   N(   t   whenFinished(   t   selfR   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   __init__#   s    c         C   s   |  j  j d   d  S(   N(   R   t   callbackt   None(   R   t   reason(    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   processEnded'   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR      s   	t   ProcessTestsBuilderBasec           B   sh   e  Z d  Z e g Z d   Z d   Z e e d d  d k rK d e _
 n  d   Z d   Z d   Z RS(	   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}.
    c            s   t    } t |  } |  j       j | t j t j d d g d |  j } |  j t j	 |   | j
   f d    |  j    d S(   s   
        L{IReactorProcess.spawnProcess} connects the protocol passed to it
        to a transport which provides L{IProcessTransport}.
        s   -ct    t   usePTYc            s
     j    S(   N(   t   stop(   t   ignored(   t   reactor(    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   <lambda>N   s    N(   R   R   t   buildReactort   spawnProcesst   syst
   executableR    t
   assertTrueR
   t
   providedByt   addCallbackt
   runReactor(   R   t   endedt   protocolt	   transport(    (   R#   sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   test_processTransportInterface7   s    		c            s  |  j      |  j r d } n i  } t j     f d   } t j t j |  t   }   j t	 |  t
 j t
 j d d g d |  j d |  j d   j   s |  j d  n  g  } | j | j  | r d S| j   f d	    |  j    |  j |  d S(
   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.
        c             s     j    d  S(   N(   R   (   t   args(   t   signaled(    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   handlere   s    s   -cR   R    t   childFDsix   s,   Timed out waiting for child process to exit.Nc            s
     j    S(   N(   R!   (   R"   (   R#   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR$      s    (   R%   R    R   t	   threadingt   Eventt   signalt   SIGCHLDR   R&   R   R'   R(   t   waitt   isSett   failR+   t   appendR,   R)   (   R   R4   R3   R-   t   result(    (   R#   R2   sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   test_spawnProcessEarlyIsReapedR   s*    			"R8   s;   Platform lacks SIGCHLD, early-spawnProcess test can't work.c            s   d   t  t d     t    d } d t f    f d     Y}  j     j  j |   t j t j d | g d  j	   f d   }  j
 |   j t   j
  f d	     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.
        t   TERMt   SIGsE   import sys
sys.stdout.write('x')
sys.stdout.flush()
sys.stdin.read()
t   Exiterc              s8   e  Z   f d    Z d   Z  f d   Z d   Z RS(   c            s(   t  d | | f  |  j j    d  S(   Ns   childDataReceived(%d, %r)(   R   R/   t   signalProcess(   R   t   fdt   data(   t   sigName(    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   childDataReceived   s    c         S   s   t  d | f  d  S(   Ns   childConnectionLost(%d)(   R   (   R   RC   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   childConnectionLost   s    c            s%   t  d | f    j | g  d  S(   Ns   processExited(%r)(   R   R   (   R   R   (   t   exited(    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   processExited   s    c         S   s   t  d | f  d  S(   Ns   processEnded(%r)(   R   (   R   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR      s    (   R   R   RF   RG   RI   R   (    (   RE   RH   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRA      s   		s   -cR    c            s~   |  \ } | j  t  | j } t j   rT   j | j d     j | j	 d  n&   j | j     j | j	 d   d  S(   Ni   (
   t   trapR   t   valueR   t	   isWindowst   assertIdenticalR7   R   t   assertEqualt   exitCode(   t   .0t   failureR   (   R   t   sigNum(    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   cbExited   s    	c            s
     j    S(   N(   R!   (   t   ign(   R#   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR$      s    N(   t   getattrR7   R   R   R%   t   callWhenRunningR&   R'   R(   R    R+   t
   addErrbackR   R,   (   R   t   sourceRA   RS   (    (   RE   RH   RR   R   R#   sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   test_processExitedWithSignal   s    	
c            s   |  j      d d g } t t d d	  d	 k	 }   j j } | | k rb | rb t d |   n  t j j d k r t d   n  g      f d   }   j	 |  |  j
    |  j  d g  d	 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   Glib2Reactort   Gtk2Reactort   siginterrupts(   %s is not supported without siginterruptt   _installHandlerUsingSignals@   _signals._installHandlerUsingSignal doesn't support this featurec             s[   zI t  j d t j f  }  t  j d t j f  }  j | j    Wd    j   Xd  S(   Ns$   %s -c "import time; time.sleep(0.1)"s1   %s -c "import time; time.sleep(0.5); print 'Foo'"(   t   ost   popenR'   R(   R<   t   readR!   (   t   f1t   f2(   R#   R=   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   f   s    		s   Foo
N(   R%   RU   R7   R   t	   __class__R   R   R   t   installHandlerRV   R,   RN   (   R   t   skippedReactorst   hasSigInterruptt   reactorClassNameRc   (    (   R#   R=   sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt'   test_systemCallUninterruptedByChildExit   s    c      	      s  d d l  m } | d k r+ t d   n  t t  } | j   j   j   j   } d d | j f d d d	 f }  f d
     j     d t f    f d     Y} |   }   j	   j
 | t j t j d d d j |  g d  j  j    d S(   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   platformTypet   posixs$   Test only applies to POSIX platformss
   import syss   sys.path.insert(0, '%s')s$   from twisted.internet import processs-   sys.stdout.write(str(process._listOpenFDs()))s   sys.stdout.flush()c            s     j  d |   d  S(   Ns   [0, 1, 2, 3](   t   assertEquals(   t   output(   R   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   checkOutput  s    t   Protocolc              s,   e  Z d    Z d   Z    f d   Z RS(   c         S   s   g  |  _  d  S(   N(   Rm   (   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR     s    c         S   s   |  j  j |  d  S(   N(   Rm   R<   (   R   RD   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   outReceived  s    c            s,   z   d j  |  j   Wd   j   Xd  S(   NR   (   t   joinRm   R!   (   R   R   (   Rn   R#   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR     s    (   R   R   R   Rp   R   (    (   R#   Rn   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRo     s   		s   -Wignores   -cs   
R    N(   t   twisted.python.runtimeRj   R   R   t   __file__t   parentt   pathR%   R   RV   R&   R'   R(   Rq   R    R,   (   R   Rj   t   heret   topRX   Ro   t   proto(    (   R#   R   Rn   sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   test_openFileDescriptors   s&    		
N(   R   R   R   R	   t   requiredInterfacesR0   R>   RU   R7   R   t   skipRY   Ri   Ry   (    (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR   ,   s   			6	E	.t   ProcessTestsBuilderc           B   s}   e  Z d  Z e Z e e  j d  j Z	 e
 j   r? d Z n d Z d   Z d   Z d   Z d   Z d   Z d	   Z RS(
   sp   
    Builder defining tests relating to L{IReactorProcess} for child processes
    which do not have a PTY.
    s   process_helper.pyt   windowsR   c            s  t     i t    d 6t    d 6t    d 6 d t f   f d     Y} d } |  j       j   j |   t j t j d | g d |  j  f d	   }  j |   f d
   }  j |  d   }  j |   j	 t
    f d   }  j |  |  j    d S(   s   
        L{IProcessProtocol.childConnectionLost} is called each time a file
        descriptor associated with a child process is closed.
        i    i   i   t   Closerc              s&   e  Z   f d    Z  f d   Z RS(   c            s     j  |  d  S(   N(   R   (   R   R/   (   t	   connected(    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   makeConnectionF  s    c            s     | j  d   d  S(   N(   R   R   (   R   t   childFD(   t   lost(    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRG   I  s    (   R   R   R   RG   (    (   R   R   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR~   E  s   sw   import os, sys
while 1:
    line = sys.stdin.readline().strip()
    if not line:
        break
    os.close(int(line))
s   -cR    c            s'     j  d   d j   f d    S(   Ns   2
i   c            s     S(   N(    (   RT   (   R/   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR$   [  s    (   t   writeR+   (   R/   (   R   (   R/   sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   cbConnectedY  s    c            s'     j  d   d j   f d    S(   Ns   1
i   c            s     S(   N(    (   RT   (   R/   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR$   `  s    (   R   R+   (   R/   (   R   (   R/   sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt
   lostSecond^  s    c         S   s   |  j  d  d  S(   Ns   
(   R   (   R/   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt	   lostFirstc  s    c            s     j    d  S(   N(   R!   (   R"   (   R#   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   cbEndedh  s    N(   R   R   R%   RV   R&   R'   R(   R    R+   RW   R   R,   (   R   R~   RX   R   R   R   R   (    (   R#   R   R   sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   test_childConnectionLost=  s$    	$	c            s   t     g   d t f   f d     Y}  j       j   j |   t j t j  j d  j g d  j	   f d   }  j
 |   j t   j
   f d     j    d S(   s   
        L{IProcessProtocol.processEnded} is called after the child process
        exits and L{IProcessProtocol.childConnectionLost} is called for each of
        its file descriptors.
        t   Enderc              s8   e  Z d    Z   f d   Z d   Z  f d   Z RS(   c         S   s%   t  d | | f  |  j j   d  S(   Ns   childDataReceived(%d, %r)(   R   R/   t   loseConnection(   R   RC   RD   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRF   z  s    c            s"   t  d | f    j |  d  S(   Ns   childConnectionLost(%d)(   R   R<   (   R   R   (   R   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRG   ~  s    c         S   s   t  d | f  d  S(   Ns   processExited(%r)(   R   (   R   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRI     s    c            s%   t  d | f    j | g  d  S(   Ns   processEnded(%r)(   R   R   (   R   R   (   R-   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR     s    (   R   R   RF   RG   RI   R   (    (   R   R-   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR   y  s   		t   childR    c            s?   |  \ } | j  t   j t    t d d d g   d  S(   Ni    i   i   (   RJ   R   RN   R   (   RP   RQ   (   R   R   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR     s    c            s
     j    S(   N(   R!   (   RT   (   R#   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR$     s    N(   R   R   R%   RV   R&   R'   R(   t   keepStdioOpenProgramt   keepStdioOpenArgR    R+   RW   R   R,   (   R   R   R   (    (   R#   R   R   R-   sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   test_processEndedp  s    	
c            s   t     t     g   d t f    f d     Y}  j       j   j |   t j t j  j d  j g d  j	    f d   }  j
 |    f d   }  j
 |   j t   j
   f d     j    d S(	   s   
        L{IProcessProtocol.processExited} is called when the child process
        exits, even if file descriptors associated with the child are still
        open.
        t   Waiterc              s2   e  Z d    Z    f d   Z  f d   Z RS(   c         S   s   t  d | | f  d  S(   Ns   childDataReceived(%d, %r)(   R   (   R   RC   RD   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRF     s    c            sD   t  d | f    j |  t    d k r@  j d   n  d  S(   Ns   childConnectionLost(%d)i   (   R   R<   t   lenR   R   (   R   R   (   R   t   allLost(    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRG     s    c            s2   t  d | f    j | g  |  j j   d  S(   Ns   processExited(%r)(   R   R   R/   R   (   R   R   (   RH   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRI     s    (   R   R   RF   RG   RI   (    (   R   R   RH   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR     s   	R   R    c            s;   |  \ } | j  t  t d   f   j   g    S(   Ns   cbExited; lost = %s(   RJ   R   R   RN   (   RP   RQ   (   R   R   R   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyRS     s
    c            s)     j  t   t d d d g   d  S(   Ni    i   i   (   RN   R   (   R"   (   R   R   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt	   cbAllLost  s    c            s
     j    S(   N(   R!   (   RT   (   R#   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR$     s    N(   R   R   R%   RV   R&   R'   R(   R   R   R    R+   RW   R   R,   (   R   R   RS   R   (    (   R#   R   R   R   RH   sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   test_processExited  s"    		"
c         C   sU   |  j    } t | d  } | j t j j |  t j  | j   t j j |  S(   sj   
        Write the given list of lines to a text file and return the absolute
        path to it.
        t   wt(	   t   mktempt   fileR   R^   t   linesepRq   t   closeRu   t   abspath(   R   t   sourceLinest   scriptt
   scriptFile(    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   makeSourceFile  s
     
c            s   d   j  d t j f d d  f d g   t j  d   j       f d      f d         f d	   }   j |   j    d
 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.
        s   this is the shebang outputs   #!%ss
   import syss   sys.stdout.write('%s')s   sys.stdout.flush()i  c            sZ   |  \ } } } t  d | | | f    j |     j | d    j | d  d  S(   Ns   cbProcessExited((%r, %r, %d))R   i    (   R   RN   (   RP   t   outR   t   code(   R   t   SHEBANG_OUTPUT(    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   cbProcessExited  s
    c            s     j    |  S(   N(   R!   (   t   passthrough(   R#   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   shutdown  s    
c             s@   t  j  d   }  |  j   |  j   |  j t  d  S(   NR#   (   R   t   getProcessOutputAndValuet   addBothR+   RW   R   (   t   d(   R#   R   R   R   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   start  s    N(   R   R'   R(   R^   t   chmodR%   RV   R,   (   R   R   (    (   R#   R   R   R   R   R   sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   test_shebang  s    
c            s   d  d d d d d g   j  d j t t t d d	      j      f d
     f d           f d   }  j |   j   d S(   sq   
        Arguments given to spawnProcess are passed to the child process as
        originally intended.
        s   import 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()t   hellot   "s    	|<>^&s   "\\"hello\\"s   "foo\ bar baz\""R   i   i   c            s-   |  j  d  }  |  j     j  |   d  S(   Nt    (   t   splitt   popRl   (   Rm   (   R   R1   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   processFinished  s    
c            s     j    |  S(   N(   R!   (   R=   (   R#   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR      s    
c             sF   t  d   }  |  j     f d    |  j   |  j   d  S(   Nc            s#   t  j t j d  g  d   S(   Ns   -cR#   (   R   t   getProcessOutputR'   R(   (   t   dummy(   R#   R1   RX   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR$   &  s   (   R   R   R+   R   (   R   (   R1   R   RX   R   R#   (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt
   spawnChild$  s    N(   R<   Rq   t   mapt   chrt   xrangeR%   RV   R,   (   R   R   (    (   R   R#   R   R1   RX   R   sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt    test_processCommandLineArguments  s    	
((   R   R   R   t   FalseR    R   Rs   t   siblingRu   R   R   RL   R   R   R   R   R   R   R   (    (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR|   -  s   		3	.	2		%t   PTYProcessTestsBuilderc           B   sE   e  Z d  Z e Z e j   r' d Z n e j   rC i d d 6Z	 n  RS(   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(
   R   R   R   t   TrueR    R   RL   R{   t   isMacOSXRf   (    (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR   1  s   	t   PotentialZombieWarningTestsc           B   s   e  Z d  Z d   Z RS(   sE   
    Tests for L{twisted.internet.error.PotentialZombieWarning}.
    c         C   sv   d d l  m } | j |  j |  j g  } |  j | d d t  |  j | d d d  |  j t |  d  d S(	   s   
        Accessing L{PotentialZombieWarning} via the
        I{PotentialZombieWarning} attribute of L{twisted.internet.error}
        results in a deprecation warning being emitted.
        i(   t   errori    t   categoryt   messages   twisted.internet.error.PotentialZombieWarning was deprecated in Twisted 10.0.0: There is no longer any potential for zombie process.i   N(   t   twisted.internetR   t   PotentialZombieWarningt   flushWarningst   test_deprecatedRl   t   DeprecationWarningR   (   R   R   t   warnings(    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR   F  s    (   R   R   R   R   (    (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR   B  s   t   FakeResourceModulec           B   s   e  Z d  Z d Z d   Z RS(   sf   
    Fake version of L{resource} which hard-codes a particular rlimit for maximum
    open files.
    i   c         C   s
   d d g S(   s   
        Mocks just the getrlimit method of the resource module, simulating what
        happens on OS-X when a ridiculous number is returned.
        i    l            (    (   R   t   no(    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt	   getrlimit`  s    (   R   R   R   t   RLIMIT_NOFILER   (    (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR   Z  s   t   FDDetectorTestc           B   s   e  Z d  Z e j j   r$ d Z n  e Z e Z	 e Z
 e 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 RS(   sj  
    Tests for _FDDetector class in twisted.internet.process, which detects
    which function to drop in place for the _listOpenFDs method.

    @ivar devfs: A flag indicating whether the filesystem fake will indicate
        that /dev/fd exists.

    @ivar accurateDevFDResults: A flag indicating whether the /dev/fd fake
        returns accurate open file information.

    @ivar procfs: A flag indicating whether the filesystem fake will indicate
        that /proc/<pid>/fd exists.

    @ivar openedFile: A flag indicating whether to include an extra file
        descriptor in the result of listing /dev/fd.
    sH   Process support does not need to detect open file descriptors on Windowsc         C   s   d S(   s>   
        Fake os.getpid, always return the same thing
        i{   (    (   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   getpid  s    c         C   s   | d k r+ |  j  r" d d d g St  n  | d k r |  j r |  j sV d d d g S|  j ro d d d d g Sd d d g Sq t    n  d S(   s   
        Fake os.listdir, depending on what mode we're in to simulate
        behaviour.

        @param arg: the directory to list
        s   /proc/123/fdt   0t   1t   2s   /dev/fdt   3N(   t   procfst   OSErrort   devfst   accurateDevFDResultst
   openedFile(   R   t   arg(    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   listdir  s    					c         C   s   t  |  _ d S(   s   
        This is a mock for L{open}.  It just keeps track of the fact that a file
        has opened so an extra file descriptor is found in /dev/fd.
        N(   R   R   (   R   t   fnamet   mode(    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   openfile  s    c         C   s7   y d d l  } Wn t k
 r) d } n X| |  _ d S(   sF   
        Save the resource module so we can restore it later.
        iN(   t   resourcet   ImportErrorR   t   savedResourceModule(   R   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   saveResourceModule  s
    
c         C   s!   d t j d <|  j |  j  d S(   sp   
        Make the L{resource} module unimportable for the remainder of the
        current test method.
        R   N(   R   R'   t   modulest
   addCleanupt   replaceResourceModule(   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   hideResourceModule  s    c         C   s$   t    t j d <|  j |  j  d S(   sc   
        Make a L{FakeResourceModule} instance importable at the L{resource}
        name.
        R   N(   R   R'   R   R   R   (   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   revealResourceModule  s    c         C   s   |  j  t j d <d S(   sI   
        Restore the original resource module to L{sys.modules}.
        R   N(   R   R'   R   (   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR     s    c         C   s@   t  j   |  _ |  j |  j _ |  j |  j _ |  j |  j _ d S(   s   
        Set up the tests, giving ourselves a detector object to play with and
        setting up its testable knobs to refer to our mocked versions.
        N(   R   t   _FDDetectort   detectorR   R   R   (   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   setUp  s    c         C   sh   t  j   } | j j } | j   | j j } | j   | j j } |  j | |  |  j | |  d S(   s   
        Check that the identity of _listOpenFDs changes after running _listOpenFDs
        the first time, but not after the second time it's run.

        In other words, check that the monkey patching actually works.
        N(   R   R   t   _listOpenFDst	   func_namet   assertNotEqualsRl   (   R   R   t   firstt   secondt   third(    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt!   test_identityOfListOpenFDsChanges  s    

c         C   s;   t  |  _ t |  _ t |  _ |  j |  j j   j d  d S(   s   
        If there is no proc filesystem and /dev/fd returns accurate open file
        descriptor information, L{process._devFDImplementation} is chosen.  This
        is like FreeBSD with I{fdescfs} mounted.
        t   _devFDImplementationN(	   R   R   R   R   R   Rl   R   t   _getImplementationR   (   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   test_accurateDevFDResults  s    			c         C   s;   t  |  _ t |  _ t  |  _ |  j |  j j   j d  d S(   s   
        If there is no proc filesystem and /dev/fd returns inaccurate
        information, L{_process._fallbackFDImplementation} is selected.  This is
        like FreeBSD without I{fdescfs} mounted.
        t   _fallbackFDImplementationN(	   R   R   R   R   R   Rl   R   R   R   (   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   test_inaccurateDevFDResults  s    			c         C   s2   t  |  _ t |  _ |  j |  j j   j d  d S(   s{   
        If there is a proc filesystem, L{process._procFDImplementation} is
        selected.  This is like Linux.
        t   _procFDImplementationN(   R   R   R   R   Rl   R   R   R   (   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   test_procFilesystem  s
    		c         C   s<   t  |  _ t  |  _ |  j   |  j |  j j   j d  d S(   s   
        If there is no proc filesystem and no /dev/fd but the L{resource} module
        is available, L{process._resourceFDImplementation} is selected.  We
        shall conjecture that this is like most proprietary UNIXes.
        t   _resourceFDImplementationN(   R   R   R   R   Rl   R   R   R   (   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   test_resourceModuleOnly  s    		
c         C   s<   t  |  _ t  |  _ |  j   |  j |  j j   j d  d S(   s  
        If there is no proc filesystem, no /dev/fd, and no L{resource} module is
        available, L{process._fallbackFDImplementation} is selected.  This maybe
        be like certain proprietary UNIXes without a nicely compiled Python
        runtime.
        R   N(   R   R   R   R   Rl   R   R   R   (   R   (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   test_withoutResourceModule*  s    		
N(   R   R   R   R   R   RL   R{   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyR   i  s,   																(2   R   t   typet   __metaclass__R^   R'   R7   R5   t   twisted.trial.unittestR    R   t#   twisted.internet.test.reactormixinsR   t   twisted.python.compatR   t   twisted.python.logR   R   Rr   R   t   twisted.python.filepathR   R   R   t   twisted.internet.interfacesR	   R
   t   twisted.internet.deferR   R   t   twisted.internet.protocolR   t   twisted.internet.errorR   R   R   t   twisted.pythonR   RL   R   R   R   R|   t   globalst   updatet   makeTestCaseClassesR   R   t   objectR   R   (    (    (    sF   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_process.pyt   <module>   s8   0  