]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/distrib/all/subprocess.py
   1 # subprocess - Subprocesses with accessible I/O streams 
   3 # For more information about this module, see PEP 324. 
   5 # Copyright (c) 2003-2004 by Peter Astrand <astrand@lysator.liu.se> 
   7 # By obtaining, using, and/or copying this software and/or its 
   8 # associated documentation, you agree that you have read, understood, 
   9 # and will comply with the following terms and conditions: 
  11 # Permission to use, copy, modify, and distribute this software and 
  12 # its associated documentation for any purpose and without fee is 
  13 # hereby granted, provided that the above copyright notice appears in 
  14 # all copies, and that both that copyright notice and this permission 
  15 # notice appear in supporting documentation, and that the name of the 
  16 # author not be used in advertising or publicity pertaining to 
  17 # distribution of the software without specific, written prior 
  20 # THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 
  21 # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 
  22 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR 
  23 # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 
  24 # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 
  25 # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 
  26 # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
  28 """subprocess - Subprocesses with accessible I/O streams 
  30 This module allows you to spawn processes and connect to their 
  31 input/output/error pipes and obtain their return codes under Unix. 
  32 This module intends to replace several other, older modules and 
  41 Information about how the subprocess module can be used to replace these 
  42 modules and functions can be found below. 
  46 Using the subprocess module 
  47 =========================== 
  48 This module defines one class called Popen: 
  50 class Popen(args, bufsize=0, executable=None, 
  51             stdin=None, stdout=None, stderr=None, 
  52             preexec_fn=None, close_fds=False, shell=False, 
  53             cwd=None, env=None, universal_newlines=False, 
  54             startupinfo=None, creationflags=0): 
  59 args should be a string, or a sequence of program arguments.  The 
  60 program to execute is normally the first item in the args sequence or 
  61 string, but can be explicitly set by using the executable argument. 
  63 On UNIX, with shell=False (default): In this case, the Popen class 
  64 uses os.execvp() to execute the child program.  args should normally 
  65 be a sequence.  A string will be treated as a sequence with the string 
  66 as the only item (the program to execute). 
  68 On UNIX, with shell=True: If args is a string, it specifies the 
  69 command string to execute through the shell.  If args is a sequence, 
  70 the first item specifies the command string, and any additional items 
  71 will be treated as additional shell arguments. 
  73 On Windows: the Popen class uses CreateProcess() to execute the child 
  74 program, which operates on strings.  If args is a sequence, it will be 
  75 converted to a string using the list2cmdline method.  Please note that 
  76 not all MS Windows applications interpret the command line the same 
  77 way: The list2cmdline is designed for applications using the same 
  78 rules as the MS C runtime. 
  80 bufsize, if given, has the same meaning as the corresponding argument 
  81 to the built-in open() function: 0 means unbuffered, 1 means line 
  82 buffered, any other positive value means use a buffer of 
  83 (approximately) that size.  A negative bufsize means to use the system 
  84 default, which usually means fully buffered.  The default value for 
  85 bufsize is 0 (unbuffered). 
  87 stdin, stdout and stderr specify the executed programs' standard 
  88 input, standard output and standard error file handles, respectively. 
  89 Valid values are PIPE, an existing file descriptor (a positive 
  90 integer), an existing file object, and None.  PIPE indicates that a 
  91 new pipe to the child should be created.  With None, no redirection 
  92 will occur; the child's file handles will be inherited from the 
  93 parent.  Additionally, stderr can be STDOUT, which indicates that the 
  94 stderr data from the applications should be captured into the same 
  95 file handle as for stdout. 
  97 If preexec_fn is set to a callable object, this object will be called 
  98 in the child process just before the child is executed. 
 100 If close_fds is true, all file descriptors except 0, 1 and 2 will be 
 101 closed before the child process is executed. 
 103 if shell is true, the specified command will be executed through the 
 106 If cwd is not None, the current directory will be changed to cwd 
 107 before the child is executed. 
 109 If env is not None, it defines the environment variables for the new 
 112 If universal_newlines is true, the file objects stdout and stderr are 
 113 opened as a text files, but lines may be terminated by any of '\n', 
 114 the Unix end-of-line convention, '\r', the Macintosh convention or 
 115 '\r\n', the Windows convention.  All of these external representations 
 116 are seen as '\n' by the Python program.  Note: This feature is only 
 117 available if Python is built with universal newline support (the 
 118 default).  Also, the newlines attribute of the file objects stdout, 
 119 stdin and stderr are not updated by the communicate() method. 
 121 The startupinfo and creationflags, if given, will be passed to the 
 122 underlying CreateProcess() function.  They can specify things such as 
 123 appearance of the main window and priority for the new process. 
 127 This module also defines two shortcut functions: 
 129 call(*args, **kwargs): 
 130     Run command with arguments.  Wait for command to complete, then 
 131     return the returncode attribute. 
 133     The arguments are the same as for the Popen constructor.  Example: 
 135     retcode = call(["ls", "-l"]) 
 140 Exceptions raised in the child process, before the new program has 
 141 started to execute, will be re-raised in the parent.  Additionally, 
 142 the exception object will have one extra attribute called 
 143 'child_traceback', which is a string containing traceback information 
 144 from the childs point of view. 
 146 The most common exception raised is OSError.  This occurs, for 
 147 example, when trying to execute a non-existent file.  Applications 
 148 should prepare for OSErrors. 
 150 A ValueError will be raised if Popen is called with invalid arguments. 
 155 Unlike some other popen functions, this implementation will never call 
 156 /bin/sh implicitly.  This means that all characters, including shell 
 157 metacharacters, can safely be passed to child processes. 
 162 Instances of the Popen class have the following methods: 
 165     Check if child process has terminated.  Returns returncode 
 169     Wait for child process to terminate.  Returns returncode attribute. 
 171 communicate(input=None) 
 172     Interact with process: Send data to stdin.  Read data from stdout 
 173     and stderr, until end-of-file is reached.  Wait for process to 
 174     terminate.  The optional stdin argument should be a string to be 
 175     sent to the child process, or None, if no data should be sent to 
 178     communicate() returns a tuple (stdout, stderr). 
 180     Note: The data read is buffered in memory, so do not use this 
 181     method if the data size is large or unlimited. 
 183 The following attributes are also available: 
 186     If the stdin argument is PIPE, this attribute is a file object 
 187     that provides input to the child process.  Otherwise, it is None. 
 190     If the stdout argument is PIPE, this attribute is a file object 
 191     that provides output from the child process.  Otherwise, it is 
 195     If the stderr argument is PIPE, this attribute is file object that 
 196     provides error output from the child process.  Otherwise, it is 
 200     The process ID of the child process. 
 203     The child return code.  A None value indicates that the process 
 204     hasn't terminated yet.  A negative value -N indicates that the 
 205     child was terminated by signal N (UNIX only). 
 208 Replacing older functions with the subprocess module 
 209 ==================================================== 
 210 In this section, "a ==> b" means that b can be used as a replacement 
 213 Note: All functions in this section fail (more or less) silently if 
 214 the executed program cannot be found; this module raises an OSError 
 217 In the following examples, we assume that the subprocess module is 
 218 imported with "from subprocess import *". 
 221 Replacing /bin/sh shell backquote 
 222 --------------------------------- 
 225 output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0] 
 228 Replacing shell pipe line 
 229 ------------------------- 
 230 output=`dmesg | grep hda` 
 232 p1 = Popen(["dmesg"], stdout=PIPE) 
 233 p2 = Popen(["grep", "hda"], stdin=p1.stdout) 
 234 output = p2.communicate()[0] 
 237 Replacing os.system() 
 238 --------------------- 
 239 sts = os.system("mycmd" + " myarg") 
 241 p = Popen("mycmd" + " myarg", shell=True) 
 242 sts = os.waitpid(p.pid, 0) 
 246 * Calling the program through the shell is usually not required. 
 248 * It's easier to look at the returncode attribute than the 
 251 A more real-world example would look like this: 
 254     retcode = call("mycmd" + " myarg", shell=True) 
 256         print >>sys.stderr, "Child was terminated by signal", -retcode 
 258         print >>sys.stderr, "Child returned", retcode 
 260     print >>sys.stderr, "Execution failed:", e 
 267 pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg") 
 269 pid = Popen(["/bin/mycmd", "myarg"]).pid 
 274 retcode = os.spawnlp(os.P_WAIT, "/bin/mycmd", "mycmd", "myarg") 
 276 retcode = call(["/bin/mycmd", "myarg"]) 
 281 os.spawnvp(os.P_NOWAIT, path, args) 
 283 Popen([path] + args[1:]) 
 288 os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env) 
 290 Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"}) 
 295 pipe = os.popen(cmd, mode='r', bufsize) 
 297 pipe = Popen(cmd, shell=True, bufsize=bufsize, stdout=PIPE).stdout 
 299 pipe = os.popen(cmd, mode='w', bufsize) 
 301 pipe = Popen(cmd, shell=True, bufsize=bufsize, stdin=PIPE).stdin 
 304 (child_stdin, child_stdout) = os.popen2(cmd, mode, bufsize) 
 306 p = Popen(cmd, shell=True, bufsize=bufsize, 
 307           stdin=PIPE, stdout=PIPE, close_fds=True) 
 308 (child_stdin, child_stdout) = (p.stdin, p.stdout) 
 313  child_stderr) = os.popen3(cmd, mode, bufsize) 
 315 p = Popen(cmd, shell=True, bufsize=bufsize, 
 316           stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) 
 319  child_stderr) = (p.stdin, p.stdout, p.stderr) 
 322 (child_stdin, child_stdout_and_stderr) = os.popen4(cmd, mode, bufsize) 
 324 p = Popen(cmd, shell=True, bufsize=bufsize, 
 325           stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) 
 326 (child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout) 
 331 Note: If the cmd argument to popen2 functions is a string, the command 
 332 is executed through /bin/sh.  If it is a list, the command is directly 
 335 (child_stdout, child_stdin) = popen2.popen2("somestring", bufsize, mode) 
 337 p = Popen(["somestring"], shell=True, bufsize=bufsize 
 338           stdin=PIPE, stdout=PIPE, close_fds=True) 
 339 (child_stdout, child_stdin) = (p.stdout, p.stdin) 
 342 (child_stdout, child_stdin) = popen2.popen2(["mycmd", "myarg"], bufsize, mode) 
 344 p = Popen(["mycmd", "myarg"], bufsize=bufsize, 
 345           stdin=PIPE, stdout=PIPE, close_fds=True) 
 346 (child_stdout, child_stdin) = (p.stdout, p.stdin) 
 348 The popen2.Popen3 and popen3.Popen4 basically works as subprocess.Popen, 
 351 * subprocess.Popen raises an exception if the execution fails 
 352 * the capturestderr argument is replaced with the stderr argument. 
 353 * stdin=PIPE and stdout=PIPE must be specified. 
 354 * popen2 closes all filedescriptors by default, but you have to specify 
 355   close_fds=True with subprocess.Popen. 
 361 mswindows 
= (sys
.platform 
== "win32") 
 370     if 0: # <-- change this to use pywin32 instead of the _subprocess driver 
 372         from win32api 
import GetStdHandle
, STD_INPUT_HANDLE
, \
 
 373                              STD_OUTPUT_HANDLE
, STD_ERROR_HANDLE
 
 374         from win32api 
import GetCurrentProcess
, DuplicateHandle
, \
 
 375                              GetModuleFileName
, GetVersion
 
 376         from win32con 
import DUPLICATE_SAME_ACCESS
 
 377         from win32pipe 
import CreatePipe
 
 378         from win32process 
import CreateProcess
, STARTUPINFO
, \
 
 379                                  GetExitCodeProcess
, STARTF_USESTDHANDLES
, \
 
 381         from win32event 
import WaitForSingleObject
, INFINITE
, WAIT_OBJECT_0
 
 383         from _subprocess 
import * 
 397 __all__ 
= ["Popen", "PIPE", "STDOUT", "call"] 
 400     MAXFD 
= os
.sysconf("SC_OPEN_MAX") 
 404 # True/False does not exist on 2.2.0 
 414     for inst 
in _active
[:]: 
 421 def call(*args
, **kwargs
): 
 422     """Run command with arguments.  Wait for command to complete, then 
 423     return the returncode attribute. 
 425     The arguments are the same as for the Popen constructor.  Example: 
 427     retcode = call(["ls", "-l"]) 
 429     return Popen(*args
, **kwargs
).wait() 
 432 def list2cmdline(seq
): 
 434     Translate a sequence of arguments into a command line 
 435     string, using the same rules as the MS C runtime: 
 437     1) Arguments are delimited by white space, which is either a 
 440     2) A string surrounded by double quotation marks is 
 441        interpreted as a single argument, regardless of white space 
 442        contained within.  A quoted string can be embedded in an 
 445     3) A double quotation mark preceded by a backslash is 
 446        interpreted as a literal double quotation mark. 
 448     4) Backslashes are interpreted literally, unless they 
 449        immediately precede a double quotation mark. 
 451     5) If backslashes immediately precede a double quotation mark, 
 452        every pair of backslashes is interpreted as a literal 
 453        backslash.  If the number of backslashes is odd, the last 
 454        backslash escapes the next double quotation mark as 
 459     # http://msdn.microsoft.com/library/en-us/vccelng/htm/progs_12.asp 
 465         # Add a space to separate this argument from the others 
 469         needquote 
= (" " in arg
) or ("\t" in arg
) 
 475                 # Don't know if we need to double yet. 
 479                 result
.append('\\' * len(bs_buf
)*2) 
 485                     result
.extend(bs_buf
) 
 489         # Add remaining backspaces, if any. 
 491             result
.extend(bs_buf
) 
 496     return ''.join(result
) 
 500     def __init__(self
, args
, bufsize
=0, executable
=None, 
 501                  stdin
=None, stdout
=None, stderr
=None, 
 502                  preexec_fn
=None, close_fds
=False, shell
=False, 
 503                  cwd
=None, env
=None, universal_newlines
=False, 
 504                  startupinfo
=None, creationflags
=0): 
 505         """Create new Popen instance.""" 
 509             if preexec_fn 
is not None: 
 510                 raise ValueError("preexec_fn is not supported on Windows " 
 513                 raise ValueError("close_fds is not supported on Windows " 
 517             if startupinfo 
is not None: 
 518                 raise ValueError("startupinfo is only supported on Windows " 
 520             if creationflags 
!= 0: 
 521                 raise ValueError("creationflags is only supported on Windows " 
 528         self
.returncode 
= None 
 529         self
.universal_newlines 
= universal_newlines
 
 531         # Input and output objects. The general principle is like 
 536         # p2cwrite   ---stdin--->  p2cread 
 537         # c2pread    <--stdout---  c2pwrite 
 538         # errread    <--stderr---  errwrite 
 540         # On POSIX, the child objects are file descriptors.  On 
 541         # Windows, these are Windows file handles.  The parent objects 
 542         # are file descriptors on both platforms.  The parent objects 
 543         # are None when not using PIPEs. The child objects are None 
 544         # when not redirecting. 
 548          errread
, errwrite
) = self
._get
_handles
(stdin
, stdout
, stderr
) 
 550         self
._execute
_child
(args
, executable
, preexec_fn
, close_fds
, 
 551                             cwd
, env
, universal_newlines
, 
 552                             startupinfo
, creationflags
, shell
, 
 558             self
.stdin 
= os
.fdopen(p2cwrite
, 'wb', bufsize
) 
 560             if universal_newlines
: 
 561                 self
.stdout 
= os
.fdopen(c2pread
, 'rU', bufsize
) 
 563                 self
.stdout 
= os
.fdopen(c2pread
, 'rb', bufsize
) 
 565             if universal_newlines
: 
 566                 self
.stderr 
= os
.fdopen(errread
, 'rU', bufsize
) 
 568                 self
.stderr 
= os
.fdopen(errread
, 'rb', bufsize
) 
 573     def _translate_newlines(self
, data
): 
 574         data 
= data
.replace("\r\n", "\n") 
 575         data 
= data
.replace("\r", "\n") 
 583         def _get_handles(self
, stdin
, stdout
, stderr
): 
 584             """Construct and return tupel with IO objects: 
 585             p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite 
 587             if stdin 
== None and stdout 
== None and stderr 
== None: 
 588                 return (None, None, None, None, None, None) 
 590             p2cread
, p2cwrite 
= None, None 
 591             c2pread
, c2pwrite 
= None, None 
 592             errread
, errwrite 
= None, None 
 595                 p2cread 
= GetStdHandle(STD_INPUT_HANDLE
) 
 597                 p2cread
, p2cwrite 
= CreatePipe(None, 0) 
 598                 # Detach and turn into fd 
 599                 p2cwrite 
= p2cwrite
.Detach() 
 600                 p2cwrite 
= msvcrt
.open_osfhandle(p2cwrite
, 0) 
 601             elif type(stdin
) == types
.IntType
: 
 602                 p2cread 
= msvcrt
.get_osfhandle(stdin
) 
 604                 # Assuming file-like object 
 605                 p2cread 
= msvcrt
.get_osfhandle(stdin
.fileno()) 
 606             p2cread 
= self
._make
_inheritable
(p2cread
) 
 609                 c2pwrite 
= GetStdHandle(STD_OUTPUT_HANDLE
) 
 611                 c2pread
, c2pwrite 
= CreatePipe(None, 0) 
 612                 # Detach and turn into fd 
 613                 c2pread 
= c2pread
.Detach() 
 614                 c2pread 
= msvcrt
.open_osfhandle(c2pread
, 0) 
 615             elif type(stdout
) == types
.IntType
: 
 616                 c2pwrite 
= msvcrt
.get_osfhandle(stdout
) 
 618                 # Assuming file-like object 
 619                 c2pwrite 
= msvcrt
.get_osfhandle(stdout
.fileno()) 
 620             c2pwrite 
= self
._make
_inheritable
(c2pwrite
) 
 623                 errwrite 
= GetStdHandle(STD_ERROR_HANDLE
) 
 625                 errread
, errwrite 
= CreatePipe(None, 0) 
 626                 # Detach and turn into fd 
 627                 errread 
= errread
.Detach() 
 628                 errread 
= msvcrt
.open_osfhandle(errread
, 0) 
 629             elif stderr 
== STDOUT
: 
 631             elif type(stderr
) == types
.IntType
: 
 632                 errwrite 
= msvcrt
.get_osfhandle(stderr
) 
 634                 # Assuming file-like object 
 635                 errwrite 
= msvcrt
.get_osfhandle(stderr
.fileno()) 
 636             errwrite 
= self
._make
_inheritable
(errwrite
) 
 638             return (p2cread
, p2cwrite
, 
 643         def _make_inheritable(self
, handle
): 
 644             """Return a duplicate of handle, which is inheritable""" 
 645             return DuplicateHandle(GetCurrentProcess(), handle
, 
 646                                    GetCurrentProcess(), 0, 1, 
 647                                    DUPLICATE_SAME_ACCESS
) 
 650         def _find_w9xpopen(self
): 
 651             """Find and return absolut path to w9xpopen.exe""" 
 652             w9xpopen 
= os
.path
.join(os
.path
.dirname(GetModuleFileName(0)), 
 654             if not os
.path
.exists(w9xpopen
): 
 655                 # Eeek - file-not-found - possibly an embedding 
 656                 # situation - see if we can locate it in sys.exec_prefix 
 657                 w9xpopen 
= os
.path
.join(os
.path
.dirname(sys
.exec_prefix
), 
 659                 if not os
.path
.exists(w9xpopen
): 
 660                     raise RuntimeError("Cannot locate w9xpopen.exe, which is " 
 661                                        "needed for Popen to work with your " 
 662                                        "shell or platform.") 
 666         def _execute_child(self
, args
, executable
, preexec_fn
, close_fds
, 
 667                            cwd
, env
, universal_newlines
, 
 668                            startupinfo
, creationflags
, shell
, 
 672             """Execute program (MS Windows version)""" 
 674             if not isinstance(args
, types
.StringTypes
): 
 675                 args 
= list2cmdline(args
) 
 678                 comspec 
= os
.environ
.get("COMSPEC", "cmd.exe") 
 679                 args 
= comspec 
+ " /c " + args
 
 680                 if (GetVersion() >= 0x80000000L 
or 
 681                         os
.path
.basename(comspec
).lower() == "command.com"): 
 682                     # Win9x, or using command.com on NT. We need to 
 683                     # use the w9xpopen intermediate program. For more 
 684                     # information, see KB Q150956 
 685                     # (http://web.archive.org/web/20011105084002/http://support.microsoft.com/support/kb/articles/Q150/9/56.asp) 
 686                     w9xpopen 
= self
._find
_w
9xpopen
() 
 687                     args 
= '"%s" %s' % (w9xpopen
, args
) 
 688                     # Not passing CREATE_NEW_CONSOLE has been known to 
 689                     # cause random failures on win9x.  Specifically a 
 690                     # dialog: "Your program accessed mem currently in 
 691                     # use at xxx" and a hopeful warning about the 
 692                     # stability of your system.  Cost is Ctrl+C wont 
 694                     creationflags |
= CREATE_NEW_CONSOLE
 
 696             # Process startup details 
 697             if startupinfo 
== None: 
 698                 startupinfo 
= STARTUPINFO() 
 699             if not None in (p2cread
, c2pwrite
, errwrite
): 
 700                 startupinfo
.dwFlags |
= STARTF_USESTDHANDLES
 
 701                 startupinfo
.hStdInput 
= p2cread
 
 702                 startupinfo
.hStdOutput 
= c2pwrite
 
 703                 startupinfo
.hStdError 
= errwrite
 
 707                 hp
, ht
, pid
, tid 
= CreateProcess(executable
, args
, 
 708                                          # no special security 
 710                                          # must inherit handles to pass std 
 717             except pywintypes
.error
, e
: 
 718                 # Translate pywintypes.error to WindowsError, which is 
 719                 # a subclass of OSError.  FIXME: We should really 
 720                 # translate errno using _sys_errlist (or simliar), but 
 721                 # how can this be done from Python? 
 722                 raise WindowsError(*e
.args
) 
 724             # Retain the process handle, but close the thread handle 
 729             # Child is launched. Close the parent's copy of those pipe 
 730             # handles that only the child should have open.  You need 
 731             # to make sure that no handles to the write end of the 
 732             # output pipe are maintained in this process or else the 
 733             # pipe will not close when the child process exits and the 
 734             # ReadFile will hang. 
 744             """Check if child process has terminated.  Returns returncode 
 746             if self
.returncode 
== None: 
 747                 if WaitForSingleObject(self
._handle
, 0) == WAIT_OBJECT_0
: 
 748                     self
.returncode 
= GetExitCodeProcess(self
._handle
) 
 750             return self
.returncode
 
 754             """Wait for child process to terminate.  Returns returncode 
 756             if self
.returncode 
== None: 
 757                 obj 
= WaitForSingleObject(self
._handle
, INFINITE
) 
 758                 self
.returncode 
= GetExitCodeProcess(self
._handle
) 
 760             return self
.returncode
 
 763         def _readerthread(self
, fh
, buffer): 
 764             buffer.append(fh
.read()) 
 767         def communicate(self
, input=None): 
 768             """Interact with process: Send data to stdin.  Read data from 
 769             stdout and stderr, until end-of-file is reached.  Wait for 
 770             process to terminate.  The optional input argument should be a 
 771             string to be sent to the child process, or None, if no data 
 772             should be sent to the child. 
 774             communicate() returns a tuple (stdout, stderr).""" 
 775             stdout 
= None # Return 
 776             stderr 
= None # Return 
 780                 stdout_thread 
= threading
.Thread(target
=self
._readerthread
, 
 781                                                  args
=(self
.stdout
, stdout
)) 
 782                 stdout_thread
.setDaemon(True) 
 783                 stdout_thread
.start() 
 786                 stderr_thread 
= threading
.Thread(target
=self
._readerthread
, 
 787                                                  args
=(self
.stderr
, stderr
)) 
 788                 stderr_thread
.setDaemon(True) 
 789                 stderr_thread
.start() 
 793                     self
.stdin
.write(input) 
 801             # All data exchanged.  Translate lists into strings. 
 807             # Translate newlines, if requested.  We cannot let the file 
 808             # object do the translation: It is based on stdio, which is 
 809             # impossible to combine with select (unless forcing no 
 811             if self
.universal_newlines 
and hasattr(open, 'newlines'): 
 813                     stdout 
= self
._translate
_newlines
(stdout
) 
 815                     stderr 
= self
._translate
_newlines
(stderr
) 
 818             return (stdout
, stderr
) 
 824         def _get_handles(self
, stdin
, stdout
, stderr
): 
 825             """Construct and return tupel with IO objects: 
 826             p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite 
 828             p2cread
, p2cwrite 
= None, None 
 829             c2pread
, c2pwrite 
= None, None 
 830             errread
, errwrite 
= None, None 
 835                 p2cread
, p2cwrite 
= os
.pipe() 
 836             elif type(stdin
) == types
.IntType
: 
 839                 # Assuming file-like object 
 840                 p2cread 
= stdin
.fileno() 
 845                 c2pread
, c2pwrite 
= os
.pipe() 
 846             elif type(stdout
) == types
.IntType
: 
 849                 # Assuming file-like object 
 850                 c2pwrite 
= stdout
.fileno() 
 855                 errread
, errwrite 
= os
.pipe() 
 856             elif stderr 
== STDOUT
: 
 858             elif type(stderr
) == types
.IntType
: 
 861                 # Assuming file-like object 
 862                 errwrite 
= stderr
.fileno() 
 864             return (p2cread
, p2cwrite
, 
 869         def _set_cloexec_flag(self
, fd
): 
 871                 cloexec_flag 
= fcntl
.FD_CLOEXEC
 
 872             except AttributeError: 
 875             old 
= fcntl
.fcntl(fd
, fcntl
.F_GETFD
) 
 876             fcntl
.fcntl(fd
, fcntl
.F_SETFD
, old | cloexec_flag
) 
 879         def _close_fds(self
, but
): 
 880             for i 
in range(3, MAXFD
): 
 889         def _execute_child(self
, args
, executable
, preexec_fn
, close_fds
, 
 890                            cwd
, env
, universal_newlines
, 
 891                            startupinfo
, creationflags
, shell
, 
 895             """Execute program (POSIX version)""" 
 897             if isinstance(args
, types
.StringTypes
): 
 901                 args 
= ["/bin/sh", "-c"] + args
 
 903             if executable 
== None: 
 906             # For transferring possible exec failure from child to parent 
 907             # The first char specifies the exception type: 0 means 
 908             # OSError, 1 means some other error. 
 909             errpipe_read
, errpipe_write 
= os
.pipe() 
 910             self
._set
_cloexec
_flag
(errpipe_write
) 
 916                     # Close parent's pipe ends 
 923                     os
.close(errpipe_read
) 
 933                     # Close pipe fds.  Make sure we doesn't close the same 
 937                     if c2pwrite 
and c2pwrite 
not in (p2cread
,): 
 939                     if errwrite 
and errwrite 
not in (p2cread
, c2pwrite
): 
 942                     # Close all other fds, if asked for 
 944                         self
._close
_fds
(but
=errpipe_write
) 
 953                         os
.execvp(executable
, args
) 
 955                         os
.execvpe(executable
, args
, env
) 
 958                     exc_type
, exc_value
, tb 
= sys
.exc_info() 
 959                     # Save the traceback and attach it to the exception object 
 960                     exc_lines 
= traceback
.format_exception(exc_type
, 
 963                     exc_value
.child_traceback 
= ''.join(exc_lines
) 
 964                     os
.write(errpipe_write
, pickle
.dumps(exc_value
)) 
 966                 # This exitcode won't be reported to applications, so it 
 967                 # really doesn't matter what we return. 
 971             os
.close(errpipe_write
) 
 972             if p2cread 
and p2cwrite
: 
 974             if c2pwrite 
and c2pread
: 
 976             if errwrite 
and errread
: 
 979             # Wait for exec to fail or succeed; possibly raising exception 
 980             data 
= os
.read(errpipe_read
, 1048576) # Exceptions limited to 1 MB 
 981             os
.close(errpipe_read
) 
 983                 child_exception 
= pickle
.loads(data
) 
 984                 raise child_exception
 
 987         def _handle_exitstatus(self
, sts
): 
 988             if os
.WIFSIGNALED(sts
): 
 989                 self
.returncode 
= -os
.WTERMSIG(sts
) 
 990             elif os
.WIFEXITED(sts
): 
 991                 self
.returncode 
= os
.WEXITSTATUS(sts
) 
 993                 # Should never happen 
 994                 raise RuntimeError("Unknown child exit status!") 
1000             """Check if child process has terminated.  Returns returncode 
1002             if self
.returncode 
== None: 
1004                     pid
, sts 
= os
.waitpid(self
.pid
, os
.WNOHANG
) 
1006                         self
._handle
_exitstatus
(sts
) 
1009             return self
.returncode
 
1013             """Wait for child process to terminate.  Returns returncode 
1015             if self
.returncode 
== None: 
1016                 pid
, sts 
= os
.waitpid(self
.pid
, 0) 
1017                 self
._handle
_exitstatus
(sts
) 
1018             return self
.returncode
 
1021         def communicate(self
, input=None): 
1022             """Interact with process: Send data to stdin.  Read data from 
1023             stdout and stderr, until end-of-file is reached.  Wait for 
1024             process to terminate.  The optional input argument should be a 
1025             string to be sent to the child process, or None, if no data 
1026             should be sent to the child. 
1028             communicate() returns a tuple (stdout, stderr).""" 
1031             stdout 
= None # Return 
1032             stderr 
= None # Return 
1035                 # Flush stdio buffer.  This might block, if the user has 
1036                 # been writing to .stdin in an uncontrolled fashion. 
1039                     write_set
.append(self
.stdin
) 
1043                 read_set
.append(self
.stdout
) 
1046                 read_set
.append(self
.stderr
) 
1049             while read_set 
or write_set
: 
1050                 rlist
, wlist
, xlist 
= select
.select(read_set
, write_set
, []) 
1052                 if self
.stdin 
in wlist
: 
1053                     # When select has indicated that the file is writable, 
1054                     # we can write up to PIPE_BUF bytes without risk 
1055                     # blocking.  POSIX defines PIPE_BUF >= 512 
1056                     bytes_written 
= os
.write(self
.stdin
.fileno(), input[:512]) 
1057                     input = input[bytes_written
:] 
1060                         write_set
.remove(self
.stdin
) 
1062                 if self
.stdout 
in rlist
: 
1063                     data 
= os
.read(self
.stdout
.fileno(), 1024) 
1066                         read_set
.remove(self
.stdout
) 
1069                 if self
.stderr 
in rlist
: 
1070                     data 
= os
.read(self
.stderr
.fileno(), 1024) 
1073                         read_set
.remove(self
.stderr
) 
1076             # All data exchanged.  Translate lists into strings. 
1078                 stdout 
= ''.join(stdout
) 
1080                 stderr 
= ''.join(stderr
) 
1082             # Translate newlines, if requested.  We cannot let the file 
1083             # object do the translation: It is based on stdio, which is 
1084             # impossible to combine with select (unless forcing no 
1086             if self
.universal_newlines 
and hasattr(open, 'newlines'): 
1088                     stdout 
= self
._translate
_newlines
(stdout
) 
1090                     stderr 
= self
._translate
_newlines
(stderr
) 
1093             return (stdout
, stderr
) 
1098     # Example 1: Simple redirection: Get process list 
1100     plist 
= Popen(["ps"], stdout
=PIPE
).communicate()[0] 
1101     print "Process list:" 
1105     # Example 2: Change uid before executing child 
1107     if os
.getuid() == 0: 
1108         p 
= Popen(["id"], preexec_fn
=lambda: os
.setuid(100)) 
1112     # Example 3: Connecting several subprocesses 
1114     print "Looking for 'hda'..." 
1115     p1 
= Popen(["dmesg"], stdout
=PIPE
) 
1116     p2 
= Popen(["grep", "hda"], stdin
=p1
.stdout
, stdout
=PIPE
) 
1117     print repr(p2
.communicate()[0]) 
1120     # Example 4: Catch execution error 
1123     print "Trying a weird file..." 
1125         print Popen(["/this/path/does/not/exist"]).communicate() 
1127         if e
.errno 
== errno
.ENOENT
: 
1128             print "The file didn't exist.  I thought so..." 
1129             print "Child traceback:" 
1130             print e
.child_traceback
 
1132             print "Error", e
.errno
 
1134         print >>sys
.stderr
, "Gosh.  No error." 
1137 def _demo_windows(): 
1139     # Example 1: Connecting several subprocesses 
1141     print "Looking for 'PROMPT' in set output..." 
1142     p1 
= Popen("set", stdout
=PIPE
, shell
=True) 
1143     p2 
= Popen('find "PROMPT"', stdin
=p1
.stdout
, stdout
=PIPE
) 
1144     print repr(p2
.communicate()[0]) 
1147     # Example 2: Simple execution of program 
1149     print "Executing calc..." 
1154 if __name__ 
== "__main__":