mò
­fIc           @   sT  d  Z  d k Z d k Z d d d g Z y e i d ƒ Z Wn e e f j
 o d Z n Xg  Z d „  Z	 d f  d	 „  ƒ  YZ
 d
 e
 f d „  ƒ  YZ e i d  d j p e i d j o7 [
 [ d d d „ Z d d d „ Z d d d „ Z nA d d d „ Z d d d „ Z d d d „ Z e i d d
 g ƒ d „  Z e d j o e ƒ  n d S(   sn  Spawn a command with pipes to its stdin, stdout, and optionally stderr.

The normal os.popen(cmd, mode) call spawns a shell command and provides a
file interface to just the input or output of the process depending on
whether mode is 'r' or 'w'.  This module provides the functions popen2(cmd)
and popen3(cmd) which return two or three pipes to the spawned command.
Nt   popen2t   popen3t   popen4t   SC_OPEN_MAXi   c          C   s    x t  D] }  |  i ƒ  q Wd  S(   N(   t   _activet   instt   poll(   R   (    (    t#   /mit/python/lib/python2.4/popen2.pyt   _cleanup   s     t   Popen3c           B   s>   t  Z d  Z d Z e d d „ Z d „  Z d „  Z d „  Z RS(   sw   Class representing a child process.  Normally instances are created
    by the factory functions popen2() and popen3().iÿÿÿÿc   
      C   sG  t  ƒ  t i ƒ  \ } } t i ƒ  \ }	 } | o t i ƒ  \ } } n t i
 ƒ  |  _ |  i d j oL t i | d ƒ t i | d ƒ | o t i | d ƒ n |  i | ƒ n t i | ƒ t i | d | ƒ |  _ t i | ƒ t i |	 d | ƒ |  _ | o) t i | ƒ t i | d | ƒ |  _ n
 d |  _ t i |  ƒ d S(   sd  The parameter 'cmd' is the shell command to execute in a
        sub-process.  On UNIX, 'cmd' may be a sequence, in which case arguments
        will be passed directly to the program without shell intervention (as
        with os.spawnv()).  If 'cmd' is a string it will be passed to the shell
        (as with os.system()).   The 'capturestderr' flag, if true, specifies
        that the object should capture standard error output of the child
        process.  The default is false.  If the 'bufsize' parameter is
        specified, it specifies the size of the I/O buffers to/from the child
        process.i    i   i   t   wt   rN(   R   t   ost   pipet   p2creadt   p2cwritet   c2preadt   c2pwritet   capturestderrt   erroutt   errint   forkt   selft   pidt   dup2t
   _run_childt   cmdt   closet   fdopent   bufsizet   tochildt	   fromchildt   childerrt   NoneR   t   append(
   R   R   R   R   R   R   R   R   R   R   (    (    R   t   __init__   s,    	 	c         C   s‘   t  | t ƒ o d d | g } n x> t d t ƒ D]- } y t i | ƒ Wq3 t j
 o q3 Xq3 Wz t i	 | d | ƒ Wd  t i
 d ƒ Xd  S(   Ns   /bin/shs   -ci   i    i   (   t
   isinstanceR   t
   basestringt   xranget   MAXFDt   iR   R   t   OSErrort   execvpt   _exit(   R   R   R(   (    (    R   R   A   s     	 c         C   s€   |  i d j  oi yL t i |  i t i ƒ \ } } | |  i j o | |  _ t i |  ƒ n Wqy t i j
 o qy Xn |  i S(   sh   Return the exit status of the child process if it has finished,
        or -1 if it hasn't finished yet.i    N(	   R   t   stsR   t   waitpidR   t   WNOHANGR   t   removet   error(   R   R   R,   (    (    R   R   N   s     		c         C   s`   |  i d j  oI t i |  i d ƒ \ } } | |  i j o | |  _ t i |  ƒ qY n |  i S(   s9   Wait for and return the exit status of the child process.i    N(   R   R,   R   R-   R   R   R/   (   R   R   R,   (    (    R   t   wait[   s     	(	   t   __name__t
   __module__t   __doc__R,   t   FalseR#   R   R   R1   (    (    (    R   R	      s    "		t   Popen4c           B   s   t  Z d  Z d d „ Z RS(   Niÿÿÿÿc         C   sæ   t  ƒ  t i ƒ  \ } } t i ƒ  \ } } t i ƒ  |  _	 |  i	 d j oA t i
 | d ƒ t i
 | d ƒ t i
 | d ƒ |  i | ƒ n t i | ƒ t i | d | ƒ |  _ t i | ƒ t i | d | ƒ |  _ t i |  ƒ d  S(   Ni    i   i   R
   R   (   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R"   (   R   R   R   R   R   R   R   (    (    R   R#   h   s    (   R2   R3   R!   R    R#   (    (    (    R   R6   e   s   i   t   wint   os2emxiÿÿÿÿt   tc         C   s%   t  i |  | | ƒ \ } } | | f S(   s¸  Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
        be a sequence, in which case arguments will be passed directly to the
        program without shell intervention (as with os.spawnv()). If 'cmd' is a
        string it will be passed to the shell (as with os.system()). If
        'bufsize' is specified, it sets the buffer size for the I/O pipes. The
        file objects (child_stdout, child_stdin) are returned.N(   R   R    R   t   modeR   R
   R   (   R   R   R:   R   R
   (    (    R   R    ~   s     c         C   s+   t  i |  | | ƒ \ } } } | | | f S(   sÆ  Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
        be a sequence, in which case arguments will be passed directly to the
        program without shell intervention (as with os.spawnv()). If 'cmd' is a
        string it will be passed to the shell (as with os.system()). If
        'bufsize' is specified, it sets the buffer size for the I/O pipes. The
        file objects (child_stdout, child_stdin, child_stderr) are returned.N(   R   R   R   R:   R   R
   R   t   e(   R   R   R:   R;   R   R
   (    (    R   R   ˆ   s     c         C   s%   t  i |  | | ƒ \ } } | | f S(   s¿  Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
        be a sequence, in which case arguments will be passed directly to the
        program without shell intervention (as with os.spawnv()). If 'cmd' is a
        string it will be passed to the shell (as with os.system()). If
        'bufsize' is specified, it sets the buffer size for the I/O pipes. The
        file objects (child_stdout_stderr, child_stdin) are returned.N(   R   R   R   R:   R   R
   R   (   R   R   R:   R   R
   (    (    R   R   ’   s     c         C   s"   t  |  t | ƒ } | i | i f S(   s¸  Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
        be a sequence, in which case arguments will be passed directly to the
        program without shell intervention (as with os.spawnv()). If 'cmd' is a
        string it will be passed to the shell (as with os.system()). If
        'bufsize' is specified, it sets the buffer size for the I/O pipes. The
        file objects (child_stdout, child_stdin) are returned.N(   R	   R   R5   R   R   R   R   (   R   R   R:   R   (    (    R   R    œ   s     c         C   s(   t  |  t | ƒ } | i | i | i f S(   sÆ  Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
        be a sequence, in which case arguments will be passed directly to the
        program without shell intervention (as with os.spawnv()). If 'cmd' is a
        string it will be passed to the shell (as with os.system()). If
        'bufsize' is specified, it sets the buffer size for the I/O pipes. The
        file objects (child_stdout, child_stdin, child_stderr) are returned.N(   R	   R   t   TrueR   R   R   R   R    (   R   R   R:   R   (    (    R   R   ¦   s     c         C   s   t  |  | ƒ } | i | i f S(   s¿  Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
        be a sequence, in which case arguments will be passed directly to the
        program without shell intervention (as with os.spawnv()). If 'cmd' is a
        string it will be passed to the shell (as with os.system()). If
        'bufsize' is specified, it sets the buffer size for the I/O pipes. The
        file objects (child_stdout_stderr, child_stdin) are returned.N(   R6   R   R   R   R   R   (   R   R   R:   R   (    (    R   R   °   s     c          C   s  d } d } t i d j o
 d } n | i ƒ  } d GHt | ƒ \ } } | i	 | ƒ | i
 ƒ  | i ƒ  } | i ƒ  | j o t d | | f ƒ ‚ n d GHy t | g ƒ \ } } }  Wn t | ƒ \ } } }  n X| i	 | ƒ | i
 ƒ  | i ƒ  } | i ƒ  | j o t d | | f ƒ ‚ n |  i ƒ  } | o t d | f ƒ ‚ n x t D] } | i ƒ  q[Wt o t d	 ƒ ‚ n d
 GHd  S(   Nt   cats   ab cd
t   ntt   mores   testing popen2...s   wrote %r read %rs   testing popen3...s   unexpected %r on stderrs   _active not emptys   All OK(   R   t   teststrR   t   namet   stript   expectedR    R   R
   t   writeR   t   readt   gott
   ValueErrorR   R;   R   R   R1   (   R;   R@   R   R   R   R
   RC   RF   (    (    R   t   _test¼   s>    


 t   __main__(   R4   R   t   syst   __all__t   sysconfR'   t   AttributeErrorRG   R   R   R	   R6   t   platformR    R   R   t   extendRH   R2   (   RK   R'   R   R    R	   RJ   R   R6   R   R   RH   R   (    (    R   t   ?   s.   			L$





	#