Back to index

python3.2  3.2.2
subprocess.py
Go to the documentation of this file.
00001 # subprocess - Subprocesses with accessible I/O streams
00002 #
00003 # For more information about this module, see PEP 324.
00004 #
00005 # Copyright (c) 2003-2005 by Peter Astrand <astrand@lysator.liu.se>
00006 #
00007 # Licensed to PSF under a Contributor Agreement.
00008 # See http://www.python.org/2.4/license for licensing details.
00009 
00010 r"""subprocess - Subprocesses with accessible I/O streams
00011 
00012 This module allows you to spawn processes, connect to their
00013 input/output/error pipes, and obtain their return codes.  This module
00014 intends to replace several other, older modules and functions, like:
00015 
00016 os.system
00017 os.spawn*
00018 
00019 Information about how the subprocess module can be used to replace these
00020 modules and functions can be found below.
00021 
00022 
00023 
00024 Using the subprocess module
00025 ===========================
00026 This module defines one class called Popen:
00027 
00028 class Popen(args, bufsize=0, executable=None,
00029             stdin=None, stdout=None, stderr=None,
00030             preexec_fn=None, close_fds=True, shell=False,
00031             cwd=None, env=None, universal_newlines=False,
00032             startupinfo=None, creationflags=0,
00033             restore_signals=True, start_new_session=False, pass_fds=()):
00034 
00035 
00036 Arguments are:
00037 
00038 args should be a string, or a sequence of program arguments.  The
00039 program to execute is normally the first item in the args sequence or
00040 string, but can be explicitly set by using the executable argument.
00041 
00042 On POSIX, with shell=False (default): In this case, the Popen class
00043 uses os.execvp() to execute the child program.  args should normally
00044 be a sequence.  A string will be treated as a sequence with the string
00045 as the only item (the program to execute).
00046 
00047 On POSIX, with shell=True: If args is a string, it specifies the
00048 command string to execute through the shell.  If args is a sequence,
00049 the first item specifies the command string, and any additional items
00050 will be treated as additional shell arguments.
00051 
00052 On Windows: the Popen class uses CreateProcess() to execute the child
00053 program, which operates on strings.  If args is a sequence, it will be
00054 converted to a string using the list2cmdline method.  Please note that
00055 not all MS Windows applications interpret the command line the same
00056 way: The list2cmdline is designed for applications using the same
00057 rules as the MS C runtime.
00058 
00059 bufsize, if given, has the same meaning as the corresponding argument
00060 to the built-in open() function: 0 means unbuffered, 1 means line
00061 buffered, any other positive value means use a buffer of
00062 (approximately) that size.  A negative bufsize means to use the system
00063 default, which usually means fully buffered.  The default value for
00064 bufsize is 0 (unbuffered).
00065 
00066 stdin, stdout and stderr specify the executed programs' standard
00067 input, standard output and standard error file handles, respectively.
00068 Valid values are PIPE, an existing file descriptor (a positive
00069 integer), an existing file object, and None.  PIPE indicates that a
00070 new pipe to the child should be created.  With None, no redirection
00071 will occur; the child's file handles will be inherited from the
00072 parent.  Additionally, stderr can be STDOUT, which indicates that the
00073 stderr data from the applications should be captured into the same
00074 file handle as for stdout.
00075 
00076 On POSIX, if preexec_fn is set to a callable object, this object will be
00077 called in the child process just before the child is executed.  The use
00078 of preexec_fn is not thread safe, using it in the presence of threads
00079 could lead to a deadlock in the child process before the new executable
00080 is executed.
00081 
00082 If close_fds is true, all file descriptors except 0, 1 and 2 will be
00083 closed before the child process is executed.  The default for close_fds
00084 varies by platform:  Always true on POSIX.  True when stdin/stdout/stderr
00085 are None on Windows, false otherwise.
00086 
00087 pass_fds is an optional sequence of file descriptors to keep open between the
00088 parent and child.  Providing any pass_fds implicitly sets close_fds to true.
00089 
00090 if shell is true, the specified command will be executed through the
00091 shell.
00092 
00093 If cwd is not None, the current directory will be changed to cwd
00094 before the child is executed.
00095 
00096 On POSIX, if restore_signals is True all signals that Python sets to
00097 SIG_IGN are restored to SIG_DFL in the child process before the exec.
00098 Currently this includes the SIGPIPE, SIGXFZ and SIGXFSZ signals.  This
00099 parameter does nothing on Windows.
00100 
00101 On POSIX, if start_new_session is True, the setsid() system call will be made
00102 in the child process prior to executing the command.
00103 
00104 If env is not None, it defines the environment variables for the new
00105 process.
00106 
00107 If universal_newlines is true, the file objects stdout and stderr are
00108 opened as a text files, but lines may be terminated by any of '\n',
00109 the Unix end-of-line convention, '\r', the old Macintosh convention or
00110 '\r\n', the Windows convention.  All of these external representations
00111 are seen as '\n' by the Python program.  Note: This feature is only
00112 available if Python is built with universal newline support (the
00113 default).  Also, the newlines attribute of the file objects stdout,
00114 stdin and stderr are not updated by the communicate() method.
00115 
00116 The startupinfo and creationflags, if given, will be passed to the
00117 underlying CreateProcess() function.  They can specify things such as
00118 appearance of the main window and priority for the new process.
00119 (Windows only)
00120 
00121 
00122 This module also defines some shortcut functions:
00123 
00124 call(*popenargs, **kwargs):
00125     Run command with arguments.  Wait for command to complete, then
00126     return the returncode attribute.
00127 
00128     The arguments are the same as for the Popen constructor.  Example:
00129 
00130     >>> retcode = subprocess.call(["ls", "-l"])
00131 
00132 check_call(*popenargs, **kwargs):
00133     Run command with arguments.  Wait for command to complete.  If the
00134     exit code was zero then return, otherwise raise
00135     CalledProcessError.  The CalledProcessError object will have the
00136     return code in the returncode attribute.
00137 
00138     The arguments are the same as for the Popen constructor.  Example:
00139 
00140     >>> subprocess.check_call(["ls", "-l"])
00141     0
00142 
00143 getstatusoutput(cmd):
00144     Return (status, output) of executing cmd in a shell.
00145 
00146     Execute the string 'cmd' in a shell with os.popen() and return a 2-tuple
00147     (status, output).  cmd is actually run as '{ cmd ; } 2>&1', so that the
00148     returned output will contain output or error messages. A trailing newline
00149     is stripped from the output. The exit status for the command can be
00150     interpreted according to the rules for the C function wait().  Example:
00151 
00152     >>> subprocess.getstatusoutput('ls /bin/ls')
00153     (0, '/bin/ls')
00154     >>> subprocess.getstatusoutput('cat /bin/junk')
00155     (256, 'cat: /bin/junk: No such file or directory')
00156     >>> subprocess.getstatusoutput('/bin/junk')
00157     (256, 'sh: /bin/junk: not found')
00158 
00159 getoutput(cmd):
00160     Return output (stdout or stderr) of executing cmd in a shell.
00161 
00162     Like getstatusoutput(), except the exit status is ignored and the return
00163     value is a string containing the command's output.  Example:
00164 
00165     >>> subprocess.getoutput('ls /bin/ls')
00166     '/bin/ls'
00167 
00168 check_output(*popenargs, **kwargs):
00169     Run command with arguments and return its output as a byte string.
00170 
00171     If the exit code was non-zero it raises a CalledProcessError.  The
00172     CalledProcessError object will have the return code in the returncode
00173     attribute and output in the output attribute.
00174 
00175     The arguments are the same as for the Popen constructor.  Example:
00176 
00177     >>> output = subprocess.check_output(["ls", "-l", "/dev/null"])
00178 
00179 
00180 Exceptions
00181 ----------
00182 Exceptions raised in the child process, before the new program has
00183 started to execute, will be re-raised in the parent.  Additionally,
00184 the exception object will have one extra attribute called
00185 'child_traceback', which is a string containing traceback information
00186 from the childs point of view.
00187 
00188 The most common exception raised is OSError.  This occurs, for
00189 example, when trying to execute a non-existent file.  Applications
00190 should prepare for OSErrors.
00191 
00192 A ValueError will be raised if Popen is called with invalid arguments.
00193 
00194 check_call() and check_output() will raise CalledProcessError, if the
00195 called process returns a non-zero return code.
00196 
00197 
00198 Security
00199 --------
00200 Unlike some other popen functions, this implementation will never call
00201 /bin/sh implicitly.  This means that all characters, including shell
00202 metacharacters, can safely be passed to child processes.
00203 
00204 
00205 Popen objects
00206 =============
00207 Instances of the Popen class have the following methods:
00208 
00209 poll()
00210     Check if child process has terminated.  Returns returncode
00211     attribute.
00212 
00213 wait()
00214     Wait for child process to terminate.  Returns returncode attribute.
00215 
00216 communicate(input=None)
00217     Interact with process: Send data to stdin.  Read data from stdout
00218     and stderr, until end-of-file is reached.  Wait for process to
00219     terminate.  The optional input argument should be a string to be
00220     sent to the child process, or None, if no data should be sent to
00221     the child.
00222 
00223     communicate() returns a tuple (stdout, stderr).
00224 
00225     Note: The data read is buffered in memory, so do not use this
00226     method if the data size is large or unlimited.
00227 
00228 The following attributes are also available:
00229 
00230 stdin
00231     If the stdin argument is PIPE, this attribute is a file object
00232     that provides input to the child process.  Otherwise, it is None.
00233 
00234 stdout
00235     If the stdout argument is PIPE, this attribute is a file object
00236     that provides output from the child process.  Otherwise, it is
00237     None.
00238 
00239 stderr
00240     If the stderr argument is PIPE, this attribute is file object that
00241     provides error output from the child process.  Otherwise, it is
00242     None.
00243 
00244 pid
00245     The process ID of the child process.
00246 
00247 returncode
00248     The child return code.  A None value indicates that the process
00249     hasn't terminated yet.  A negative value -N indicates that the
00250     child was terminated by signal N (POSIX only).
00251 
00252 
00253 Replacing older functions with the subprocess module
00254 ====================================================
00255 In this section, "a ==> b" means that b can be used as a replacement
00256 for a.
00257 
00258 Note: All functions in this section fail (more or less) silently if
00259 the executed program cannot be found; this module raises an OSError
00260 exception.
00261 
00262 In the following examples, we assume that the subprocess module is
00263 imported with "from subprocess import *".
00264 
00265 
00266 Replacing /bin/sh shell backquote
00267 ---------------------------------
00268 output=`mycmd myarg`
00269 ==>
00270 output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]
00271 
00272 
00273 Replacing shell pipe line
00274 -------------------------
00275 output=`dmesg | grep hda`
00276 ==>
00277 p1 = Popen(["dmesg"], stdout=PIPE)
00278 p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
00279 output = p2.communicate()[0]
00280 
00281 
00282 Replacing os.system()
00283 ---------------------
00284 sts = os.system("mycmd" + " myarg")
00285 ==>
00286 p = Popen("mycmd" + " myarg", shell=True)
00287 pid, sts = os.waitpid(p.pid, 0)
00288 
00289 Note:
00290 
00291 * Calling the program through the shell is usually not required.
00292 
00293 * It's easier to look at the returncode attribute than the
00294   exitstatus.
00295 
00296 A more real-world example would look like this:
00297 
00298 try:
00299     retcode = call("mycmd" + " myarg", shell=True)
00300     if retcode < 0:
00301         print("Child was terminated by signal", -retcode, file=sys.stderr)
00302     else:
00303         print("Child returned", retcode, file=sys.stderr)
00304 except OSError as e:
00305     print("Execution failed:", e, file=sys.stderr)
00306 
00307 
00308 Replacing os.spawn*
00309 -------------------
00310 P_NOWAIT example:
00311 
00312 pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
00313 ==>
00314 pid = Popen(["/bin/mycmd", "myarg"]).pid
00315 
00316 
00317 P_WAIT example:
00318 
00319 retcode = os.spawnlp(os.P_WAIT, "/bin/mycmd", "mycmd", "myarg")
00320 ==>
00321 retcode = call(["/bin/mycmd", "myarg"])
00322 
00323 
00324 Vector example:
00325 
00326 os.spawnvp(os.P_NOWAIT, path, args)
00327 ==>
00328 Popen([path] + args[1:])
00329 
00330 
00331 Environment example:
00332 
00333 os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env)
00334 ==>
00335 Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})
00336 """
00337 
00338 import sys
00339 mswindows = (sys.platform == "win32")
00340 
00341 import io
00342 import os
00343 import traceback
00344 import gc
00345 import signal
00346 import builtins
00347 import warnings
00348 import errno
00349 
00350 # Exception classes used by this module.
00351 class CalledProcessError(Exception):
00352     """This exception is raised when a process run by check_call() or
00353     check_output() returns a non-zero exit status.
00354     The exit status will be stored in the returncode attribute;
00355     check_output() will also store the output in the output attribute.
00356     """
00357     def __init__(self, returncode, cmd, output=None):
00358         self.returncode = returncode
00359         self.cmd = cmd
00360         self.output = output
00361     def __str__(self):
00362         return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
00363 
00364 
00365 if mswindows:
00366     import threading
00367     import msvcrt
00368     import _subprocess
00369     class STARTUPINFO:
00370         dwFlags = 0
00371         hStdInput = None
00372         hStdOutput = None
00373         hStdError = None
00374         wShowWindow = 0
00375     class pywintypes:
00376         error = IOError
00377 else:
00378     import select
00379     _has_poll = hasattr(select, 'poll')
00380     import fcntl
00381     import pickle
00382 
00383     try:
00384         import _posixsubprocess
00385     except ImportError:
00386         _posixsubprocess = None
00387         warnings.warn("The _posixsubprocess module is not being used. "
00388                       "Child process reliability may suffer if your "
00389                       "program uses threads.", RuntimeWarning)
00390 
00391     # When select or poll has indicated that the file is writable,
00392     # we can write up to _PIPE_BUF bytes without risk of blocking.
00393     # POSIX defines PIPE_BUF as >= 512.
00394     _PIPE_BUF = getattr(select, 'PIPE_BUF', 512)
00395 
00396     _FD_CLOEXEC = getattr(fcntl, 'FD_CLOEXEC', 1)
00397 
00398     def _set_cloexec(fd, cloexec):
00399         old = fcntl.fcntl(fd, fcntl.F_GETFD)
00400         if cloexec:
00401             fcntl.fcntl(fd, fcntl.F_SETFD, old | _FD_CLOEXEC)
00402         else:
00403             fcntl.fcntl(fd, fcntl.F_SETFD, old & ~_FD_CLOEXEC)
00404 
00405     if _posixsubprocess:
00406         _create_pipe = _posixsubprocess.cloexec_pipe
00407     else:
00408         def _create_pipe():
00409             fds = os.pipe()
00410             _set_cloexec(fds[0], True)
00411             _set_cloexec(fds[1], True)
00412             return fds
00413 
00414 __all__ = ["Popen", "PIPE", "STDOUT", "call", "check_call", "getstatusoutput",
00415            "getoutput", "check_output", "CalledProcessError"]
00416 
00417 if mswindows:
00418     from _subprocess import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP,
00419                              STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
00420                              STD_ERROR_HANDLE, SW_HIDE,
00421                              STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW)
00422 
00423     __all__.extend(["CREATE_NEW_CONSOLE", "CREATE_NEW_PROCESS_GROUP",
00424                     "STD_INPUT_HANDLE", "STD_OUTPUT_HANDLE",
00425                     "STD_ERROR_HANDLE", "SW_HIDE",
00426                     "STARTF_USESTDHANDLES", "STARTF_USESHOWWINDOW"])
00427 try:
00428     MAXFD = os.sysconf("SC_OPEN_MAX")
00429 except:
00430     MAXFD = 256
00431 
00432 _active = []
00433 
00434 def _cleanup():
00435     for inst in _active[:]:
00436         res = inst._internal_poll(_deadstate=sys.maxsize)
00437         if res is not None and res >= 0:
00438             try:
00439                 _active.remove(inst)
00440             except ValueError:
00441                 # This can happen if two threads create a new Popen instance.
00442                 # It's harmless that it was already removed, so ignore.
00443                 pass
00444 
00445 PIPE = -1
00446 STDOUT = -2
00447 
00448 
00449 def _eintr_retry_call(func, *args):
00450     while True:
00451         try:
00452             return func(*args)
00453         except (OSError, IOError) as e:
00454             if e.errno == errno.EINTR:
00455                 continue
00456             raise
00457 
00458 
00459 def call(*popenargs, **kwargs):
00460     """Run command with arguments.  Wait for command to complete, then
00461     return the returncode attribute.
00462 
00463     The arguments are the same as for the Popen constructor.  Example:
00464 
00465     retcode = call(["ls", "-l"])
00466     """
00467     return Popen(*popenargs, **kwargs).wait()
00468 
00469 
00470 def check_call(*popenargs, **kwargs):
00471     """Run command with arguments.  Wait for command to complete.  If
00472     the exit code was zero then return, otherwise raise
00473     CalledProcessError.  The CalledProcessError object will have the
00474     return code in the returncode attribute.
00475 
00476     The arguments are the same as for the Popen constructor.  Example:
00477 
00478     check_call(["ls", "-l"])
00479     """
00480     retcode = call(*popenargs, **kwargs)
00481     if retcode:
00482         cmd = kwargs.get("args")
00483         if cmd is None:
00484             cmd = popenargs[0]
00485         raise CalledProcessError(retcode, cmd)
00486     return 0
00487 
00488 
00489 def check_output(*popenargs, **kwargs):
00490     r"""Run command with arguments and return its output as a byte string.
00491 
00492     If the exit code was non-zero it raises a CalledProcessError.  The
00493     CalledProcessError object will have the return code in the returncode
00494     attribute and output in the output attribute.
00495 
00496     The arguments are the same as for the Popen constructor.  Example:
00497 
00498     >>> check_output(["ls", "-l", "/dev/null"])
00499     b'crw-rw-rw- 1 root root 1, 3 Oct 18  2007 /dev/null\n'
00500 
00501     The stdout argument is not allowed as it is used internally.
00502     To capture standard error in the result, use stderr=STDOUT.
00503 
00504     >>> check_output(["/bin/sh", "-c",
00505     ...               "ls -l non_existent_file ; exit 0"],
00506     ...              stderr=STDOUT)
00507     b'ls: non_existent_file: No such file or directory\n'
00508     """
00509     if 'stdout' in kwargs:
00510         raise ValueError('stdout argument not allowed, it will be overridden.')
00511     process = Popen(*popenargs, stdout=PIPE, **kwargs)
00512     output, unused_err = process.communicate()
00513     retcode = process.poll()
00514     if retcode:
00515         cmd = kwargs.get("args")
00516         if cmd is None:
00517             cmd = popenargs[0]
00518         raise CalledProcessError(retcode, cmd, output=output)
00519     return output
00520 
00521 
00522 def list2cmdline(seq):
00523     """
00524     Translate a sequence of arguments into a command line
00525     string, using the same rules as the MS C runtime:
00526 
00527     1) Arguments are delimited by white space, which is either a
00528        space or a tab.
00529 
00530     2) A string surrounded by double quotation marks is
00531        interpreted as a single argument, regardless of white space
00532        contained within.  A quoted string can be embedded in an
00533        argument.
00534 
00535     3) A double quotation mark preceded by a backslash is
00536        interpreted as a literal double quotation mark.
00537 
00538     4) Backslashes are interpreted literally, unless they
00539        immediately precede a double quotation mark.
00540 
00541     5) If backslashes immediately precede a double quotation mark,
00542        every pair of backslashes is interpreted as a literal
00543        backslash.  If the number of backslashes is odd, the last
00544        backslash escapes the next double quotation mark as
00545        described in rule 3.
00546     """
00547 
00548     # See
00549     # http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
00550     # or search http://msdn.microsoft.com for
00551     # "Parsing C++ Command-Line Arguments"
00552     result = []
00553     needquote = False
00554     for arg in seq:
00555         bs_buf = []
00556 
00557         # Add a space to separate this argument from the others
00558         if result:
00559             result.append(' ')
00560 
00561         needquote = (" " in arg) or ("\t" in arg) or not arg
00562         if needquote:
00563             result.append('"')
00564 
00565         for c in arg:
00566             if c == '\\':
00567                 # Don't know if we need to double yet.
00568                 bs_buf.append(c)
00569             elif c == '"':
00570                 # Double backslashes.
00571                 result.append('\\' * len(bs_buf)*2)
00572                 bs_buf = []
00573                 result.append('\\"')
00574             else:
00575                 # Normal char
00576                 if bs_buf:
00577                     result.extend(bs_buf)
00578                     bs_buf = []
00579                 result.append(c)
00580 
00581         # Add remaining backslashes, if any.
00582         if bs_buf:
00583             result.extend(bs_buf)
00584 
00585         if needquote:
00586             result.extend(bs_buf)
00587             result.append('"')
00588 
00589     return ''.join(result)
00590 
00591 
00592 # Various tools for executing commands and looking at their output and status.
00593 #
00594 # NB This only works (and is only relevant) for POSIX.
00595 
00596 def getstatusoutput(cmd):
00597     """Return (status, output) of executing cmd in a shell.
00598 
00599     Execute the string 'cmd' in a shell with os.popen() and return a 2-tuple
00600     (status, output).  cmd is actually run as '{ cmd ; } 2>&1', so that the
00601     returned output will contain output or error messages.  A trailing newline
00602     is stripped from the output.  The exit status for the command can be
00603     interpreted according to the rules for the C function wait().  Example:
00604 
00605     >>> import subprocess
00606     >>> subprocess.getstatusoutput('ls /bin/ls')
00607     (0, '/bin/ls')
00608     >>> subprocess.getstatusoutput('cat /bin/junk')
00609     (256, 'cat: /bin/junk: No such file or directory')
00610     >>> subprocess.getstatusoutput('/bin/junk')
00611     (256, 'sh: /bin/junk: not found')
00612     """
00613     pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
00614     text = pipe.read()
00615     sts = pipe.close()
00616     if sts is None: sts = 0
00617     if text[-1:] == '\n': text = text[:-1]
00618     return sts, text
00619 
00620 
00621 def getoutput(cmd):
00622     """Return output (stdout or stderr) of executing cmd in a shell.
00623 
00624     Like getstatusoutput(), except the exit status is ignored and the return
00625     value is a string containing the command's output.  Example:
00626 
00627     >>> import subprocess
00628     >>> subprocess.getoutput('ls /bin/ls')
00629     '/bin/ls'
00630     """
00631     return getstatusoutput(cmd)[1]
00632 
00633 
00634 _PLATFORM_DEFAULT_CLOSE_FDS = object()
00635 
00636 
00637 class Popen(object):
00638     def __init__(self, args, bufsize=0, executable=None,
00639                  stdin=None, stdout=None, stderr=None,
00640                  preexec_fn=None, close_fds=_PLATFORM_DEFAULT_CLOSE_FDS,
00641                  shell=False, cwd=None, env=None, universal_newlines=False,
00642                  startupinfo=None, creationflags=0,
00643                  restore_signals=True, start_new_session=False,
00644                  pass_fds=()):
00645         """Create new Popen instance."""
00646         _cleanup()
00647 
00648         self._child_created = False
00649         if bufsize is None:
00650             bufsize = 0  # Restore default
00651         if not isinstance(bufsize, int):
00652             raise TypeError("bufsize must be an integer")
00653 
00654         if mswindows:
00655             if preexec_fn is not None:
00656                 raise ValueError("preexec_fn is not supported on Windows "
00657                                  "platforms")
00658             any_stdio_set = (stdin is not None or stdout is not None or
00659                              stderr is not None)
00660             if close_fds is _PLATFORM_DEFAULT_CLOSE_FDS:
00661                 if any_stdio_set:
00662                     close_fds = False
00663                 else:
00664                     close_fds = True
00665             elif close_fds and any_stdio_set:
00666                 raise ValueError(
00667                         "close_fds is not supported on Windows platforms"
00668                         " if you redirect stdin/stdout/stderr")
00669         else:
00670             # POSIX
00671             if close_fds is _PLATFORM_DEFAULT_CLOSE_FDS:
00672                 close_fds = True
00673             if pass_fds and not close_fds:
00674                 warnings.warn("pass_fds overriding close_fds.", RuntimeWarning)
00675                 close_fds = True
00676             if startupinfo is not None:
00677                 raise ValueError("startupinfo is only supported on Windows "
00678                                  "platforms")
00679             if creationflags != 0:
00680                 raise ValueError("creationflags is only supported on Windows "
00681                                  "platforms")
00682 
00683         self.stdin = None
00684         self.stdout = None
00685         self.stderr = None
00686         self.pid = None
00687         self.returncode = None
00688         self.universal_newlines = universal_newlines
00689 
00690         # Input and output objects. The general principle is like
00691         # this:
00692         #
00693         # Parent                   Child
00694         # ------                   -----
00695         # p2cwrite   ---stdin--->  p2cread
00696         # c2pread    <--stdout---  c2pwrite
00697         # errread    <--stderr---  errwrite
00698         #
00699         # On POSIX, the child objects are file descriptors.  On
00700         # Windows, these are Windows file handles.  The parent objects
00701         # are file descriptors on both platforms.  The parent objects
00702         # are -1 when not using PIPEs. The child objects are -1
00703         # when not redirecting.
00704 
00705         (p2cread, p2cwrite,
00706          c2pread, c2pwrite,
00707          errread, errwrite) = self._get_handles(stdin, stdout, stderr)
00708 
00709         # We wrap OS handles *before* launching the child, otherwise a
00710         # quickly terminating child could make our fds unwrappable
00711         # (see #8458).
00712 
00713         if mswindows:
00714             if p2cwrite != -1:
00715                 p2cwrite = msvcrt.open_osfhandle(p2cwrite.Detach(), 0)
00716             if c2pread != -1:
00717                 c2pread = msvcrt.open_osfhandle(c2pread.Detach(), 0)
00718             if errread != -1:
00719                 errread = msvcrt.open_osfhandle(errread.Detach(), 0)
00720 
00721         if p2cwrite != -1:
00722             self.stdin = io.open(p2cwrite, 'wb', bufsize)
00723             if self.universal_newlines:
00724                 self.stdin = io.TextIOWrapper(self.stdin, write_through=True)
00725         if c2pread != -1:
00726             self.stdout = io.open(c2pread, 'rb', bufsize)
00727             if universal_newlines:
00728                 self.stdout = io.TextIOWrapper(self.stdout)
00729         if errread != -1:
00730             self.stderr = io.open(errread, 'rb', bufsize)
00731             if universal_newlines:
00732                 self.stderr = io.TextIOWrapper(self.stderr)
00733 
00734         try:
00735             self._execute_child(args, executable, preexec_fn, close_fds,
00736                                 pass_fds, cwd, env, universal_newlines,
00737                                 startupinfo, creationflags, shell,
00738                                 p2cread, p2cwrite,
00739                                 c2pread, c2pwrite,
00740                                 errread, errwrite,
00741                                 restore_signals, start_new_session)
00742         except:
00743             # Cleanup if the child failed starting
00744             for f in filter(None, [self.stdin, self.stdout, self.stderr]):
00745                 try:
00746                     f.close()
00747                 except EnvironmentError:
00748                     # Ignore EBADF or other errors
00749                     pass
00750             raise
00751 
00752 
00753     def _translate_newlines(self, data, encoding):
00754         data = data.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
00755         return data.decode(encoding)
00756 
00757     def __enter__(self):
00758         return self
00759 
00760     def __exit__(self, type, value, traceback):
00761         if self.stdout:
00762             self.stdout.close()
00763         if self.stderr:
00764             self.stderr.close()
00765         if self.stdin:
00766             self.stdin.close()
00767         # Wait for the process to terminate, to avoid zombies.
00768         self.wait()
00769 
00770     def __del__(self, _maxsize=sys.maxsize, _active=_active):
00771         # If __init__ hasn't had a chance to execute (e.g. if it
00772         # was passed an undeclared keyword argument), we don't
00773         # have a _child_created attribute at all.
00774         if not getattr(self, '_child_created', False):
00775             # We didn't get to successfully create a child process.
00776             return
00777         # In case the child hasn't been waited on, check if it's done.
00778         self._internal_poll(_deadstate=_maxsize)
00779         if self.returncode is None and _active is not None:
00780             # Child is still running, keep us alive until we can wait on it.
00781             _active.append(self)
00782 
00783 
00784     def communicate(self, input=None):
00785         """Interact with process: Send data to stdin.  Read data from
00786         stdout and stderr, until end-of-file is reached.  Wait for
00787         process to terminate.  The optional input argument should be a
00788         string to be sent to the child process, or None, if no data
00789         should be sent to the child.
00790 
00791         communicate() returns a tuple (stdout, stderr)."""
00792 
00793         # Optimization: If we are only using one pipe, or no pipe at
00794         # all, using select() or threads is unnecessary.
00795         if [self.stdin, self.stdout, self.stderr].count(None) >= 2:
00796             stdout = None
00797             stderr = None
00798             if self.stdin:
00799                 if input:
00800                     try:
00801                         self.stdin.write(input)
00802                     except IOError as e:
00803                         if e.errno != errno.EPIPE and e.errno != errno.EINVAL:
00804                             raise
00805                 self.stdin.close()
00806             elif self.stdout:
00807                 stdout = _eintr_retry_call(self.stdout.read)
00808                 self.stdout.close()
00809             elif self.stderr:
00810                 stderr = _eintr_retry_call(self.stderr.read)
00811                 self.stderr.close()
00812             self.wait()
00813             return (stdout, stderr)
00814 
00815         return self._communicate(input)
00816 
00817 
00818     def poll(self):
00819         return self._internal_poll()
00820 
00821 
00822     if mswindows:
00823         #
00824         # Windows methods
00825         #
00826         def _get_handles(self, stdin, stdout, stderr):
00827             """Construct and return tuple with IO objects:
00828             p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
00829             """
00830             if stdin is None and stdout is None and stderr is None:
00831                 return (-1, -1, -1, -1, -1, -1)
00832 
00833             p2cread, p2cwrite = -1, -1
00834             c2pread, c2pwrite = -1, -1
00835             errread, errwrite = -1, -1
00836 
00837             if stdin is None:
00838                 p2cread = _subprocess.GetStdHandle(_subprocess.STD_INPUT_HANDLE)
00839                 if p2cread is None:
00840                     p2cread, _ = _subprocess.CreatePipe(None, 0)
00841             elif stdin == PIPE:
00842                 p2cread, p2cwrite = _subprocess.CreatePipe(None, 0)
00843             elif isinstance(stdin, int):
00844                 p2cread = msvcrt.get_osfhandle(stdin)
00845             else:
00846                 # Assuming file-like object
00847                 p2cread = msvcrt.get_osfhandle(stdin.fileno())
00848             p2cread = self._make_inheritable(p2cread)
00849 
00850             if stdout is None:
00851                 c2pwrite = _subprocess.GetStdHandle(_subprocess.STD_OUTPUT_HANDLE)
00852                 if c2pwrite is None:
00853                     _, c2pwrite = _subprocess.CreatePipe(None, 0)
00854             elif stdout == PIPE:
00855                 c2pread, c2pwrite = _subprocess.CreatePipe(None, 0)
00856             elif isinstance(stdout, int):
00857                 c2pwrite = msvcrt.get_osfhandle(stdout)
00858             else:
00859                 # Assuming file-like object
00860                 c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
00861             c2pwrite = self._make_inheritable(c2pwrite)
00862 
00863             if stderr is None:
00864                 errwrite = _subprocess.GetStdHandle(_subprocess.STD_ERROR_HANDLE)
00865                 if errwrite is None:
00866                     _, errwrite = _subprocess.CreatePipe(None, 0)
00867             elif stderr == PIPE:
00868                 errread, errwrite = _subprocess.CreatePipe(None, 0)
00869             elif stderr == STDOUT:
00870                 errwrite = c2pwrite
00871             elif isinstance(stderr, int):
00872                 errwrite = msvcrt.get_osfhandle(stderr)
00873             else:
00874                 # Assuming file-like object
00875                 errwrite = msvcrt.get_osfhandle(stderr.fileno())
00876             errwrite = self._make_inheritable(errwrite)
00877 
00878             return (p2cread, p2cwrite,
00879                     c2pread, c2pwrite,
00880                     errread, errwrite)
00881 
00882 
00883         def _make_inheritable(self, handle):
00884             """Return a duplicate of handle, which is inheritable"""
00885             return _subprocess.DuplicateHandle(_subprocess.GetCurrentProcess(),
00886                                 handle, _subprocess.GetCurrentProcess(), 0, 1,
00887                                 _subprocess.DUPLICATE_SAME_ACCESS)
00888 
00889 
00890         def _find_w9xpopen(self):
00891             """Find and return absolut path to w9xpopen.exe"""
00892             w9xpopen = os.path.join(
00893                             os.path.dirname(_subprocess.GetModuleFileName(0)),
00894                                     "w9xpopen.exe")
00895             if not os.path.exists(w9xpopen):
00896                 # Eeek - file-not-found - possibly an embedding
00897                 # situation - see if we can locate it in sys.exec_prefix
00898                 w9xpopen = os.path.join(os.path.dirname(sys.exec_prefix),
00899                                         "w9xpopen.exe")
00900                 if not os.path.exists(w9xpopen):
00901                     raise RuntimeError("Cannot locate w9xpopen.exe, which is "
00902                                        "needed for Popen to work with your "
00903                                        "shell or platform.")
00904             return w9xpopen
00905 
00906 
00907         def _execute_child(self, args, executable, preexec_fn, close_fds,
00908                            pass_fds, cwd, env, universal_newlines,
00909                            startupinfo, creationflags, shell,
00910                            p2cread, p2cwrite,
00911                            c2pread, c2pwrite,
00912                            errread, errwrite,
00913                            unused_restore_signals, unused_start_new_session):
00914             """Execute program (MS Windows version)"""
00915 
00916             assert not pass_fds, "pass_fds not supported on Windows."
00917 
00918             if not isinstance(args, str):
00919                 args = list2cmdline(args)
00920 
00921             # Process startup details
00922             if startupinfo is None:
00923                 startupinfo = STARTUPINFO()
00924             if -1 not in (p2cread, c2pwrite, errwrite):
00925                 startupinfo.dwFlags |= _subprocess.STARTF_USESTDHANDLES
00926                 startupinfo.hStdInput = p2cread
00927                 startupinfo.hStdOutput = c2pwrite
00928                 startupinfo.hStdError = errwrite
00929 
00930             if shell:
00931                 startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
00932                 startupinfo.wShowWindow = _subprocess.SW_HIDE
00933                 comspec = os.environ.get("COMSPEC", "cmd.exe")
00934                 args = '{} /c "{}"'.format (comspec, args)
00935                 if (_subprocess.GetVersion() >= 0x80000000 or
00936                         os.path.basename(comspec).lower() == "command.com"):
00937                     # Win9x, or using command.com on NT. We need to
00938                     # use the w9xpopen intermediate program. For more
00939                     # information, see KB Q150956
00940                     # (http://web.archive.org/web/20011105084002/http://support.microsoft.com/support/kb/articles/Q150/9/56.asp)
00941                     w9xpopen = self._find_w9xpopen()
00942                     args = '"%s" %s' % (w9xpopen, args)
00943                     # Not passing CREATE_NEW_CONSOLE has been known to
00944                     # cause random failures on win9x.  Specifically a
00945                     # dialog: "Your program accessed mem currently in
00946                     # use at xxx" and a hopeful warning about the
00947                     # stability of your system.  Cost is Ctrl+C won't
00948                     # kill children.
00949                     creationflags |= _subprocess.CREATE_NEW_CONSOLE
00950 
00951             # Start the process
00952             try:
00953                 hp, ht, pid, tid = _subprocess.CreateProcess(executable, args,
00954                                          # no special security
00955                                          None, None,
00956                                          int(not close_fds),
00957                                          creationflags,
00958                                          env,
00959                                          cwd,
00960                                          startupinfo)
00961             except pywintypes.error as e:
00962                 # Translate pywintypes.error to WindowsError, which is
00963                 # a subclass of OSError.  FIXME: We should really
00964                 # translate errno using _sys_errlist (or similar), but
00965                 # how can this be done from Python?
00966                 raise WindowsError(*e.args)
00967             finally:
00968                 # Child is launched. Close the parent's copy of those pipe
00969                 # handles that only the child should have open.  You need
00970                 # to make sure that no handles to the write end of the
00971                 # output pipe are maintained in this process or else the
00972                 # pipe will not close when the child process exits and the
00973                 # ReadFile will hang.
00974                 if p2cread != -1:
00975                     p2cread.Close()
00976                 if c2pwrite != -1:
00977                     c2pwrite.Close()
00978                 if errwrite != -1:
00979                     errwrite.Close()
00980 
00981             # Retain the process handle, but close the thread handle
00982             self._child_created = True
00983             self._handle = hp
00984             self.pid = pid
00985             ht.Close()
00986 
00987         def _internal_poll(self, _deadstate=None,
00988                 _WaitForSingleObject=_subprocess.WaitForSingleObject,
00989                 _WAIT_OBJECT_0=_subprocess.WAIT_OBJECT_0,
00990                 _GetExitCodeProcess=_subprocess.GetExitCodeProcess):
00991             """Check if child process has terminated.  Returns returncode
00992             attribute.
00993 
00994             This method is called by __del__, so it can only refer to objects
00995             in its local scope.
00996 
00997             """
00998             if self.returncode is None:
00999                 if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
01000                     self.returncode = _GetExitCodeProcess(self._handle)
01001             return self.returncode
01002 
01003 
01004         def wait(self):
01005             """Wait for child process to terminate.  Returns returncode
01006             attribute."""
01007             if self.returncode is None:
01008                 _subprocess.WaitForSingleObject(self._handle,
01009                                                 _subprocess.INFINITE)
01010                 self.returncode = _subprocess.GetExitCodeProcess(self._handle)
01011             return self.returncode
01012 
01013 
01014         def _readerthread(self, fh, buffer):
01015             buffer.append(fh.read())
01016             fh.close()
01017 
01018 
01019         def _communicate(self, input):
01020             stdout = None # Return
01021             stderr = None # Return
01022 
01023             if self.stdout:
01024                 stdout = []
01025                 stdout_thread = threading.Thread(target=self._readerthread,
01026                                                  args=(self.stdout, stdout))
01027                 stdout_thread.daemon = True
01028                 stdout_thread.start()
01029             if self.stderr:
01030                 stderr = []
01031                 stderr_thread = threading.Thread(target=self._readerthread,
01032                                                  args=(self.stderr, stderr))
01033                 stderr_thread.daemon = True
01034                 stderr_thread.start()
01035 
01036             if self.stdin:
01037                 if input is not None:
01038                     try:
01039                         self.stdin.write(input)
01040                     except IOError as e:
01041                         if e.errno != errno.EPIPE:
01042                             raise
01043                 self.stdin.close()
01044 
01045             if self.stdout:
01046                 stdout_thread.join()
01047             if self.stderr:
01048                 stderr_thread.join()
01049 
01050             # All data exchanged.  Translate lists into strings.
01051             if stdout is not None:
01052                 stdout = stdout[0]
01053             if stderr is not None:
01054                 stderr = stderr[0]
01055 
01056             self.wait()
01057             return (stdout, stderr)
01058 
01059         def send_signal(self, sig):
01060             """Send a signal to the process
01061             """
01062             if sig == signal.SIGTERM:
01063                 self.terminate()
01064             elif sig == signal.CTRL_C_EVENT:
01065                 os.kill(self.pid, signal.CTRL_C_EVENT)
01066             elif sig == signal.CTRL_BREAK_EVENT:
01067                 os.kill(self.pid, signal.CTRL_BREAK_EVENT)
01068             else:
01069                 raise ValueError("Unsupported signal: {}".format(sig))
01070 
01071         def terminate(self):
01072             """Terminates the process
01073             """
01074             _subprocess.TerminateProcess(self._handle, 1)
01075 
01076         kill = terminate
01077 
01078     else:
01079         #
01080         # POSIX methods
01081         #
01082         def _get_handles(self, stdin, stdout, stderr):
01083             """Construct and return tuple with IO objects:
01084             p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
01085             """
01086             p2cread, p2cwrite = -1, -1
01087             c2pread, c2pwrite = -1, -1
01088             errread, errwrite = -1, -1
01089 
01090             if stdin is None:
01091                 pass
01092             elif stdin == PIPE:
01093                 p2cread, p2cwrite = _create_pipe()
01094             elif isinstance(stdin, int):
01095                 p2cread = stdin
01096             else:
01097                 # Assuming file-like object
01098                 p2cread = stdin.fileno()
01099 
01100             if stdout is None:
01101                 pass
01102             elif stdout == PIPE:
01103                 c2pread, c2pwrite = _create_pipe()
01104             elif isinstance(stdout, int):
01105                 c2pwrite = stdout
01106             else:
01107                 # Assuming file-like object
01108                 c2pwrite = stdout.fileno()
01109 
01110             if stderr is None:
01111                 pass
01112             elif stderr == PIPE:
01113                 errread, errwrite = _create_pipe()
01114             elif stderr == STDOUT:
01115                 errwrite = c2pwrite
01116             elif isinstance(stderr, int):
01117                 errwrite = stderr
01118             else:
01119                 # Assuming file-like object
01120                 errwrite = stderr.fileno()
01121 
01122             return (p2cread, p2cwrite,
01123                     c2pread, c2pwrite,
01124                     errread, errwrite)
01125 
01126 
01127         def _close_fds(self, fds_to_keep):
01128             start_fd = 3
01129             for fd in sorted(fds_to_keep):
01130                 if fd >= start_fd:
01131                     os.closerange(start_fd, fd)
01132                     start_fd = fd + 1
01133             if start_fd <= MAXFD:
01134                 os.closerange(start_fd, MAXFD)
01135 
01136 
01137         def _execute_child(self, args, executable, preexec_fn, close_fds,
01138                            pass_fds, cwd, env, universal_newlines,
01139                            startupinfo, creationflags, shell,
01140                            p2cread, p2cwrite,
01141                            c2pread, c2pwrite,
01142                            errread, errwrite,
01143                            restore_signals, start_new_session):
01144             """Execute program (POSIX version)"""
01145 
01146             if isinstance(args, str):
01147                 args = [args]
01148             else:
01149                 args = list(args)
01150 
01151             if shell:
01152                 args = ["/bin/sh", "-c"] + args
01153                 if executable:
01154                     args[0] = executable
01155 
01156             if executable is None:
01157                 executable = args[0]
01158 
01159             # For transferring possible exec failure from child to parent.
01160             # Data format: "exception name:hex errno:description"
01161             # Pickle is not used; it is complex and involves memory allocation.
01162             errpipe_read, errpipe_write = _create_pipe()
01163             try:
01164                 try:
01165 
01166                     if _posixsubprocess:
01167                         # We must avoid complex work that could involve
01168                         # malloc or free in the child process to avoid
01169                         # potential deadlocks, thus we do all this here.
01170                         # and pass it to fork_exec()
01171 
01172                         if env is not None:
01173                             env_list = [os.fsencode(k) + b'=' + os.fsencode(v)
01174                                         for k, v in env.items()]
01175                         else:
01176                             env_list = None  # Use execv instead of execve.
01177                         executable = os.fsencode(executable)
01178                         if os.path.dirname(executable):
01179                             executable_list = (executable,)
01180                         else:
01181                             # This matches the behavior of os._execvpe().
01182                             executable_list = tuple(
01183                                 os.path.join(os.fsencode(dir), executable)
01184                                 for dir in os.get_exec_path(env))
01185                         fds_to_keep = set(pass_fds)
01186                         fds_to_keep.add(errpipe_write)
01187                         self.pid = _posixsubprocess.fork_exec(
01188                                 args, executable_list,
01189                                 close_fds, sorted(fds_to_keep), cwd, env_list,
01190                                 p2cread, p2cwrite, c2pread, c2pwrite,
01191                                 errread, errwrite,
01192                                 errpipe_read, errpipe_write,
01193                                 restore_signals, start_new_session, preexec_fn)
01194                     else:
01195                         # Pure Python implementation: It is not thread safe.
01196                         # This implementation may deadlock in the child if your
01197                         # parent process has any other threads running.
01198 
01199                         gc_was_enabled = gc.isenabled()
01200                         # Disable gc to avoid bug where gc -> file_dealloc ->
01201                         # write to stderr -> hang.  See issue1336
01202                         gc.disable()
01203                         try:
01204                             self.pid = os.fork()
01205                         except:
01206                             if gc_was_enabled:
01207                                 gc.enable()
01208                             raise
01209                         self._child_created = True
01210                         if self.pid == 0:
01211                             # Child
01212                             try:
01213                                 # Close parent's pipe ends
01214                                 if p2cwrite != -1:
01215                                     os.close(p2cwrite)
01216                                 if c2pread != -1:
01217                                     os.close(c2pread)
01218                                 if errread != -1:
01219                                     os.close(errread)
01220                                 os.close(errpipe_read)
01221 
01222                                 # When duping fds, if there arises a situation
01223                                 # where one of the fds is either 0, 1 or 2, it
01224                                 # is possible that it is overwritten (#12607).
01225                                 if c2pwrite == 0:
01226                                     c2pwrite = os.dup(c2pwrite)
01227                                 if errwrite == 0 or errwrite == 1:
01228                                     errwrite = os.dup(errwrite)
01229 
01230                                 # Dup fds for child
01231                                 def _dup2(a, b):
01232                                     # dup2() removes the CLOEXEC flag but
01233                                     # we must do it ourselves if dup2()
01234                                     # would be a no-op (issue #10806).
01235                                     if a == b:
01236                                         _set_cloexec(a, False)
01237                                     elif a != -1:
01238                                         os.dup2(a, b)
01239                                 _dup2(p2cread, 0)
01240                                 _dup2(c2pwrite, 1)
01241                                 _dup2(errwrite, 2)
01242 
01243                                 # Close pipe fds.  Make sure we don't close the
01244                                 # same fd more than once, or standard fds.
01245                                 closed = set()
01246                                 for fd in [p2cread, c2pwrite, errwrite]:
01247                                     if fd > 2 and fd not in closed:
01248                                         os.close(fd)
01249                                         closed.add(fd)
01250 
01251                                 # Close all other fds, if asked for
01252                                 if close_fds:
01253                                     fds_to_keep = set(pass_fds)
01254                                     fds_to_keep.add(errpipe_write)
01255                                     self._close_fds(fds_to_keep)
01256 
01257 
01258                                 if cwd is not None:
01259                                     os.chdir(cwd)
01260 
01261                                 # This is a copy of Python/pythonrun.c
01262                                 # _Py_RestoreSignals().  If that were exposed
01263                                 # as a sys._py_restoresignals func it would be
01264                                 # better.. but this pure python implementation
01265                                 # isn't likely to be used much anymore.
01266                                 if restore_signals:
01267                                     signals = ('SIGPIPE', 'SIGXFZ', 'SIGXFSZ')
01268                                     for sig in signals:
01269                                         if hasattr(signal, sig):
01270                                             signal.signal(getattr(signal, sig),
01271                                                           signal.SIG_DFL)
01272 
01273                                 if start_new_session and hasattr(os, 'setsid'):
01274                                     os.setsid()
01275 
01276                                 if preexec_fn:
01277                                     preexec_fn()
01278 
01279                                 if env is None:
01280                                     os.execvp(executable, args)
01281                                 else:
01282                                     os.execvpe(executable, args, env)
01283 
01284                             except:
01285                                 try:
01286                                     exc_type, exc_value = sys.exc_info()[:2]
01287                                     if isinstance(exc_value, OSError):
01288                                         errno_num = exc_value.errno
01289                                     else:
01290                                         errno_num = 0
01291                                     message = '%s:%x:%s' % (exc_type.__name__,
01292                                                             errno_num, exc_value)
01293                                     message = message.encode(errors="surrogatepass")
01294                                     os.write(errpipe_write, message)
01295                                 except Exception:
01296                                     # We MUST not allow anything odd happening
01297                                     # above to prevent us from exiting below.
01298                                     pass
01299 
01300                             # This exitcode won't be reported to applications
01301                             # so it really doesn't matter what we return.
01302                             os._exit(255)
01303 
01304                         # Parent
01305                         if gc_was_enabled:
01306                             gc.enable()
01307                 finally:
01308                     # be sure the FD is closed no matter what
01309                     os.close(errpipe_write)
01310 
01311                 if p2cread != -1 and p2cwrite != -1:
01312                     os.close(p2cread)
01313                 if c2pwrite != -1 and c2pread != -1:
01314                     os.close(c2pwrite)
01315                 if errwrite != -1 and errread != -1:
01316                     os.close(errwrite)
01317 
01318                 # Wait for exec to fail or succeed; possibly raising an
01319                 # exception (limited in size)
01320                 data = bytearray()
01321                 while True:
01322                     part = _eintr_retry_call(os.read, errpipe_read, 50000)
01323                     data += part
01324                     if not part or len(data) > 50000:
01325                         break
01326             finally:
01327                 # be sure the FD is closed no matter what
01328                 os.close(errpipe_read)
01329 
01330             if data:
01331                 try:
01332                     _eintr_retry_call(os.waitpid, self.pid, 0)
01333                 except OSError as e:
01334                     if e.errno != errno.ECHILD:
01335                         raise
01336                 try:
01337                     exception_name, hex_errno, err_msg = data.split(b':', 2)
01338                 except ValueError:
01339                     print('Bad exception data:', repr(data))
01340                     exception_name = b'RuntimeError'
01341                     hex_errno = b'0'
01342                     err_msg = b'Unknown'
01343                 child_exception_type = getattr(
01344                         builtins, exception_name.decode('ascii'),
01345                         RuntimeError)
01346                 for fd in (p2cwrite, c2pread, errread):
01347                     if fd != -1:
01348                         os.close(fd)
01349                 err_msg = err_msg.decode(errors="surrogatepass")
01350                 if issubclass(child_exception_type, OSError) and hex_errno:
01351                     errno_num = int(hex_errno, 16)
01352                     if errno_num != 0:
01353                         err_msg = os.strerror(errno_num)
01354                         if errno_num == errno.ENOENT:
01355                             err_msg += ': ' + repr(args[0])
01356                     raise child_exception_type(errno_num, err_msg)
01357                 raise child_exception_type(err_msg)
01358 
01359 
01360         def _handle_exitstatus(self, sts, _WIFSIGNALED=os.WIFSIGNALED,
01361                 _WTERMSIG=os.WTERMSIG, _WIFEXITED=os.WIFEXITED,
01362                 _WEXITSTATUS=os.WEXITSTATUS):
01363             # This method is called (indirectly) by __del__, so it cannot
01364             # refer to anything outside of its local scope."""
01365             if _WIFSIGNALED(sts):
01366                 self.returncode = -_WTERMSIG(sts)
01367             elif _WIFEXITED(sts):
01368                 self.returncode = _WEXITSTATUS(sts)
01369             else:
01370                 # Should never happen
01371                 raise RuntimeError("Unknown child exit status!")
01372 
01373 
01374         def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid,
01375                 _WNOHANG=os.WNOHANG, _os_error=os.error):
01376             """Check if child process has terminated.  Returns returncode
01377             attribute.
01378 
01379             This method is called by __del__, so it cannot reference anything
01380             outside of the local scope (nor can any methods it calls).
01381 
01382             """
01383             if self.returncode is None:
01384                 try:
01385                     pid, sts = _waitpid(self.pid, _WNOHANG)
01386                     if pid == self.pid:
01387                         self._handle_exitstatus(sts)
01388                 except _os_error:
01389                     if _deadstate is not None:
01390                         self.returncode = _deadstate
01391             return self.returncode
01392 
01393 
01394         def wait(self):
01395             """Wait for child process to terminate.  Returns returncode
01396             attribute."""
01397             if self.returncode is None:
01398                 try:
01399                     pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0)
01400                 except OSError as e:
01401                     if e.errno != errno.ECHILD:
01402                         raise
01403                     # This happens if SIGCLD is set to be ignored or waiting
01404                     # for child processes has otherwise been disabled for our
01405                     # process.  This child is dead, we can't get the status.
01406                     sts = 0
01407                 self._handle_exitstatus(sts)
01408             return self.returncode
01409 
01410 
01411         def _communicate(self, input):
01412             if self.stdin:
01413                 # Flush stdio buffer.  This might block, if the user has
01414                 # been writing to .stdin in an uncontrolled fashion.
01415                 self.stdin.flush()
01416                 if not input:
01417                     self.stdin.close()
01418 
01419             if _has_poll:
01420                 stdout, stderr = self._communicate_with_poll(input)
01421             else:
01422                 stdout, stderr = self._communicate_with_select(input)
01423 
01424             # All data exchanged.  Translate lists into strings.
01425             if stdout is not None:
01426                 stdout = b''.join(stdout)
01427             if stderr is not None:
01428                 stderr = b''.join(stderr)
01429 
01430             # Translate newlines, if requested.
01431             # This also turns bytes into strings.
01432             if self.universal_newlines:
01433                 if stdout is not None:
01434                     stdout = self._translate_newlines(stdout,
01435                                                       self.stdout.encoding)
01436                 if stderr is not None:
01437                     stderr = self._translate_newlines(stderr,
01438                                                       self.stderr.encoding)
01439 
01440             self.wait()
01441             return (stdout, stderr)
01442 
01443 
01444         def _communicate_with_poll(self, input):
01445             stdout = None # Return
01446             stderr = None # Return
01447             fd2file = {}
01448             fd2output = {}
01449 
01450             poller = select.poll()
01451             def register_and_append(file_obj, eventmask):
01452                 poller.register(file_obj.fileno(), eventmask)
01453                 fd2file[file_obj.fileno()] = file_obj
01454 
01455             def close_unregister_and_remove(fd):
01456                 poller.unregister(fd)
01457                 fd2file[fd].close()
01458                 fd2file.pop(fd)
01459 
01460             if self.stdin and input:
01461                 register_and_append(self.stdin, select.POLLOUT)
01462 
01463             select_POLLIN_POLLPRI = select.POLLIN | select.POLLPRI
01464             if self.stdout:
01465                 register_and_append(self.stdout, select_POLLIN_POLLPRI)
01466                 fd2output[self.stdout.fileno()] = stdout = []
01467             if self.stderr:
01468                 register_and_append(self.stderr, select_POLLIN_POLLPRI)
01469                 fd2output[self.stderr.fileno()] = stderr = []
01470 
01471             input_offset = 0
01472             while fd2file:
01473                 try:
01474                     ready = poller.poll()
01475                 except select.error as e:
01476                     if e.args[0] == errno.EINTR:
01477                         continue
01478                     raise
01479 
01480                 # XXX Rewrite these to use non-blocking I/O on the
01481                 # file objects; they are no longer using C stdio!
01482 
01483                 for fd, mode in ready:
01484                     if mode & select.POLLOUT:
01485                         chunk = input[input_offset : input_offset + _PIPE_BUF]
01486                         try:
01487                             input_offset += os.write(fd, chunk)
01488                         except OSError as e:
01489                             if e.errno == errno.EPIPE:
01490                                 close_unregister_and_remove(fd)
01491                             else:
01492                                 raise
01493                         else:
01494                             if input_offset >= len(input):
01495                                 close_unregister_and_remove(fd)
01496                     elif mode & select_POLLIN_POLLPRI:
01497                         data = os.read(fd, 4096)
01498                         if not data:
01499                             close_unregister_and_remove(fd)
01500                         fd2output[fd].append(data)
01501                     else:
01502                         # Ignore hang up or errors.
01503                         close_unregister_and_remove(fd)
01504 
01505             return (stdout, stderr)
01506 
01507 
01508         def _communicate_with_select(self, input):
01509             read_set = []
01510             write_set = []
01511             stdout = None # Return
01512             stderr = None # Return
01513 
01514             if self.stdin and input:
01515                 write_set.append(self.stdin)
01516             if self.stdout:
01517                 read_set.append(self.stdout)
01518                 stdout = []
01519             if self.stderr:
01520                 read_set.append(self.stderr)
01521                 stderr = []
01522 
01523             input_offset = 0
01524             while read_set or write_set:
01525                 try:
01526                     rlist, wlist, xlist = select.select(read_set, write_set, [])
01527                 except select.error as e:
01528                     if e.args[0] == errno.EINTR:
01529                         continue
01530                     raise
01531 
01532                 # XXX Rewrite these to use non-blocking I/O on the
01533                 # file objects; they are no longer using C stdio!
01534 
01535                 if self.stdin in wlist:
01536                     chunk = input[input_offset : input_offset + _PIPE_BUF]
01537                     try:
01538                         bytes_written = os.write(self.stdin.fileno(), chunk)
01539                     except OSError as e:
01540                         if e.errno == errno.EPIPE:
01541                             self.stdin.close()
01542                             write_set.remove(self.stdin)
01543                         else:
01544                             raise
01545                     else:
01546                         input_offset += bytes_written
01547                         if input_offset >= len(input):
01548                             self.stdin.close()
01549                             write_set.remove(self.stdin)
01550 
01551                 if self.stdout in rlist:
01552                     data = os.read(self.stdout.fileno(), 1024)
01553                     if not data:
01554                         self.stdout.close()
01555                         read_set.remove(self.stdout)
01556                     stdout.append(data)
01557 
01558                 if self.stderr in rlist:
01559                     data = os.read(self.stderr.fileno(), 1024)
01560                     if not data:
01561                         self.stderr.close()
01562                         read_set.remove(self.stderr)
01563                     stderr.append(data)
01564 
01565             return (stdout, stderr)
01566 
01567 
01568         def send_signal(self, sig):
01569             """Send a signal to the process
01570             """
01571             os.kill(self.pid, sig)
01572 
01573         def terminate(self):
01574             """Terminate the process with SIGTERM
01575             """
01576             self.send_signal(signal.SIGTERM)
01577 
01578         def kill(self):
01579             """Kill the process with SIGKILL
01580             """
01581             self.send_signal(signal.SIGKILL)
01582 
01583 
01584 def _demo_posix():
01585     #
01586     # Example 1: Simple redirection: Get process list
01587     #
01588     plist = Popen(["ps"], stdout=PIPE).communicate()[0]
01589     print("Process list:")
01590     print(plist)
01591 
01592     #
01593     # Example 2: Change uid before executing child
01594     #
01595     if os.getuid() == 0:
01596         p = Popen(["id"], preexec_fn=lambda: os.setuid(100))
01597         p.wait()
01598 
01599     #
01600     # Example 3: Connecting several subprocesses
01601     #
01602     print("Looking for 'hda'...")
01603     p1 = Popen(["dmesg"], stdout=PIPE)
01604     p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
01605     print(repr(p2.communicate()[0]))
01606 
01607     #
01608     # Example 4: Catch execution error
01609     #
01610     print()
01611     print("Trying a weird file...")
01612     try:
01613         print(Popen(["/this/path/does/not/exist"]).communicate())
01614     except OSError as e:
01615         if e.errno == errno.ENOENT:
01616             print("The file didn't exist.  I thought so...")
01617             print("Child traceback:")
01618             print(e.child_traceback)
01619         else:
01620             print("Error", e.errno)
01621     else:
01622         print("Gosh.  No error.", file=sys.stderr)
01623 
01624 
01625 def _demo_windows():
01626     #
01627     # Example 1: Connecting several subprocesses
01628     #
01629     print("Looking for 'PROMPT' in set output...")
01630     p1 = Popen("set", stdout=PIPE, shell=True)
01631     p2 = Popen('find "PROMPT"', stdin=p1.stdout, stdout=PIPE)
01632     print(repr(p2.communicate()[0]))
01633 
01634     #
01635     # Example 2: Simple execution of program
01636     #
01637     print("Executing calc...")
01638     p = Popen("calc")
01639     p.wait()
01640 
01641 
01642 if __name__ == "__main__":
01643     if mswindows:
01644         _demo_windows()
01645     else:
01646         _demo_posix()