1 # subprocess - Subprocesses with accessible I/O streams
3 # For more information about this module, see PEP 324.
5 # This module should remain compatible with Python 2.2, see PEP 291.
7 # Copyright (c) 2003-2005 by Peter Astrand <astrand@lysator.liu.se>
9 # Licensed to PSF under a Contributor Agreement.
10 # See http://www.python.org/2.4/license for licensing details.
12 r
"""subprocess - Subprocesses with accessible I/O streams
14 This module allows you to spawn processes, connect to their
15 input/output/error pipes, and obtain their return codes. This module
16 intends to replace several other, older modules and functions, like:
24 Information about how the subprocess module can be used to replace these
25 modules and functions can be found below.
29 Using the subprocess module
30 ===========================
31 This module defines one class called Popen:
33 class Popen(args, bufsize=0, executable=None,
34 stdin=None, stdout=None, stderr=None,
35 preexec_fn=None, close_fds=False, shell=False,
36 cwd=None, env=None, universal_newlines=False,
37 startupinfo=None, creationflags=0):
42 args should be a string, or a sequence of program arguments. The
43 program to execute is normally the first item in the args sequence or
44 string, but can be explicitly set by using the executable argument.
46 On UNIX, with shell=False (default): In this case, the Popen class
47 uses os.execvp() to execute the child program. args should normally
48 be a sequence. A string will be treated as a sequence with the string
49 as the only item (the program to execute).
51 On UNIX, with shell=True: If args is a string, it specifies the
52 command string to execute through the shell. If args is a sequence,
53 the first item specifies the command string, and any additional items
54 will be treated as additional shell arguments.
56 On Windows: the Popen class uses CreateProcess() to execute the child
57 program, which operates on strings. If args is a sequence, it will be
58 converted to a string using the list2cmdline method. Please note that
59 not all MS Windows applications interpret the command line the same
60 way: The list2cmdline is designed for applications using the same
61 rules as the MS C runtime.
63 bufsize, if given, has the same meaning as the corresponding argument
64 to the built-in open() function: 0 means unbuffered, 1 means line
65 buffered, any other positive value means use a buffer of
66 (approximately) that size. A negative bufsize means to use the system
67 default, which usually means fully buffered. The default value for
68 bufsize is 0 (unbuffered).
70 stdin, stdout and stderr specify the executed programs' standard
71 input, standard output and standard error file handles, respectively.
72 Valid values are PIPE, an existing file descriptor (a positive
73 integer), an existing file object, and None. PIPE indicates that a
74 new pipe to the child should be created. With None, no redirection
75 will occur; the child's file handles will be inherited from the
76 parent. Additionally, stderr can be STDOUT, which indicates that the
77 stderr data from the applications should be captured into the same
78 file handle as for stdout.
80 If preexec_fn is set to a callable object, this object will be called
81 in the child process just before the child is executed.
83 If close_fds is true, all file descriptors except 0, 1 and 2 will be
84 closed before the child process is executed.
86 if shell is true, the specified command will be executed through the
89 If cwd is not None, the current directory will be changed to cwd
90 before the child is executed.
92 If env is not None, it defines the environment variables for the new
95 If universal_newlines is true, the file objects stdout and stderr are
96 opened as a text files, but lines may be terminated by any of '\n',
97 the Unix end-of-line convention, '\r', the Macintosh convention or
98 '\r\n', the Windows convention. All of these external representations
99 are seen as '\n' by the Python program. Note: This feature is only
100 available if Python is built with universal newline support (the
101 default). Also, the newlines attribute of the file objects stdout,
102 stdin and stderr are not updated by the communicate() method.
104 The startupinfo and creationflags, if given, will be passed to the
105 underlying CreateProcess() function. They can specify things such as
106 appearance of the main window and priority for the new process.
110 This module also defines two shortcut functions:
112 call(*args, **kwargs):
113 Run command with arguments. Wait for command to complete, then
114 return the returncode attribute.
116 The arguments are the same as for the Popen constructor. Example:
118 retcode = call(["ls", "-l"])
123 Exceptions raised in the child process, before the new program has
124 started to execute, will be re-raised in the parent. Additionally,
125 the exception object will have one extra attribute called
126 'child_traceback', which is a string containing traceback information
127 from the childs point of view.
129 The most common exception raised is OSError. This occurs, for
130 example, when trying to execute a non-existent file. Applications
131 should prepare for OSErrors.
133 A ValueError will be raised if Popen is called with invalid arguments.
138 Unlike some other popen functions, this implementation will never call
139 /bin/sh implicitly. This means that all characters, including shell
140 metacharacters, can safely be passed to child processes.
145 Instances of the Popen class have the following methods:
148 Check if child process has terminated. Returns returncode
152 Wait for child process to terminate. Returns returncode attribute.
154 communicate(input=None)
155 Interact with process: Send data to stdin. Read data from stdout
156 and stderr, until end-of-file is reached. Wait for process to
157 terminate. The optional stdin argument should be a string to be
158 sent to the child process, or None, if no data should be sent to
161 communicate() returns a tuple (stdout, stderr).
163 Note: The data read is buffered in memory, so do not use this
164 method if the data size is large or unlimited.
166 The following attributes are also available:
169 If the stdin argument is PIPE, this attribute is a file object
170 that provides input to the child process. Otherwise, it is None.
173 If the stdout argument is PIPE, this attribute is a file object
174 that provides output from the child process. Otherwise, it is
178 If the stderr argument is PIPE, this attribute is file object that
179 provides error output from the child process. Otherwise, it is
183 The process ID of the child process.
186 The child return code. A None value indicates that the process
187 hasn't terminated yet. A negative value -N indicates that the
188 child was terminated by signal N (UNIX only).
191 Replacing older functions with the subprocess module
192 ====================================================
193 In this section, "a ==> b" means that b can be used as a replacement
196 Note: All functions in this section fail (more or less) silently if
197 the executed program cannot be found; this module raises an OSError
200 In the following examples, we assume that the subprocess module is
201 imported with "from subprocess import *".
204 Replacing /bin/sh shell backquote
205 ---------------------------------
208 output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]
211 Replacing shell pipe line
212 -------------------------
213 output=`dmesg | grep hda`
215 p1 = Popen(["dmesg"], stdout=PIPE)
216 p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
217 output = p2.communicate()[0]
220 Replacing os.system()
221 ---------------------
222 sts = os.system("mycmd" + " myarg")
224 p = Popen("mycmd" + " myarg", shell=True)
225 sts = os.waitpid(p.pid, 0)
229 * Calling the program through the shell is usually not required.
231 * It's easier to look at the returncode attribute than the
234 A more real-world example would look like this:
237 retcode = call("mycmd" + " myarg", shell=True)
239 print >>sys.stderr, "Child was terminated by signal", -retcode
241 print >>sys.stderr, "Child returned", retcode
243 print >>sys.stderr, "Execution failed:", e
250 pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
252 pid = Popen(["/bin/mycmd", "myarg"]).pid
257 retcode = os.spawnlp(os.P_WAIT, "/bin/mycmd", "mycmd", "myarg")
259 retcode = call(["/bin/mycmd", "myarg"])
264 os.spawnvp(os.P_NOWAIT, path, args)
266 Popen([path] + args[1:])
271 os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env)
273 Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})
278 pipe = os.popen(cmd, mode='r', bufsize)
280 pipe = Popen(cmd, shell=True, bufsize=bufsize, stdout=PIPE).stdout
282 pipe = os.popen(cmd, mode='w', bufsize)
284 pipe = Popen(cmd, shell=True, bufsize=bufsize, stdin=PIPE).stdin
287 (child_stdin, child_stdout) = os.popen2(cmd, mode, bufsize)
289 p = Popen(cmd, shell=True, bufsize=bufsize,
290 stdin=PIPE, stdout=PIPE, close_fds=True)
291 (child_stdin, child_stdout) = (p.stdin, p.stdout)
296 child_stderr) = os.popen3(cmd, mode, bufsize)
298 p = Popen(cmd, shell=True, bufsize=bufsize,
299 stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
302 child_stderr) = (p.stdin, p.stdout, p.stderr)
305 (child_stdin, child_stdout_and_stderr) = os.popen4(cmd, mode, bufsize)
307 p = Popen(cmd, shell=True, bufsize=bufsize,
308 stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
309 (child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout)
314 Note: If the cmd argument to popen2 functions is a string, the command
315 is executed through /bin/sh. If it is a list, the command is directly
318 (child_stdout, child_stdin) = popen2.popen2("somestring", bufsize, mode)
320 p = Popen(["somestring"], shell=True, bufsize=bufsize
321 stdin=PIPE, stdout=PIPE, close_fds=True)
322 (child_stdout, child_stdin) = (p.stdout, p.stdin)
325 (child_stdout, child_stdin) = popen2.popen2(["mycmd", "myarg"], bufsize, mode)
327 p = Popen(["mycmd", "myarg"], bufsize=bufsize,
328 stdin=PIPE, stdout=PIPE, close_fds=True)
329 (child_stdout, child_stdin) = (p.stdout, p.stdin)
331 The popen2.Popen3 and popen3.Popen4 basically works as subprocess.Popen,
334 * subprocess.Popen raises an exception if the execution fails
335 * the capturestderr argument is replaced with the stderr argument.
336 * stdin=PIPE and stdout=PIPE must be specified.
337 * popen2 closes all filedescriptors by default, but you have to specify
338 close_fds=True with subprocess.Popen.
344 mswindows
= (sys
.platform
== "win32")
353 if 0: # <-- change this to use pywin32 instead of the _subprocess driver
355 from win32api
import GetStdHandle
, STD_INPUT_HANDLE
, \
356 STD_OUTPUT_HANDLE
, STD_ERROR_HANDLE
357 from win32api
import GetCurrentProcess
, DuplicateHandle
, \
358 GetModuleFileName
, GetVersion
359 from win32con
import DUPLICATE_SAME_ACCESS
, SW_HIDE
360 from win32pipe
import CreatePipe
361 from win32process
import CreateProcess
, STARTUPINFO
, \
362 GetExitCodeProcess
, STARTF_USESTDHANDLES
, \
363 STARTF_USESHOWWINDOW
, CREATE_NEW_CONSOLE
364 from win32event
import WaitForSingleObject
, INFINITE
, WAIT_OBJECT_0
366 from _subprocess
import *
380 __all__
= ["Popen", "PIPE", "STDOUT", "call"]
383 MAXFD
= os
.sysconf("SC_OPEN_MAX")
387 # True/False does not exist on 2.2.0
397 for inst
in _active
[:]:
404 def call(*args
, **kwargs
):
405 """Run command with arguments. Wait for command to complete, then
406 return the returncode attribute.
408 The arguments are the same as for the Popen constructor. Example:
410 retcode = call(["ls", "-l"])
412 return Popen(*args
, **kwargs
).wait()
415 def list2cmdline(seq
):
417 Translate a sequence of arguments into a command line
418 string, using the same rules as the MS C runtime:
420 1) Arguments are delimited by white space, which is either a
423 2) A string surrounded by double quotation marks is
424 interpreted as a single argument, regardless of white space
425 contained within. A quoted string can be embedded in an
428 3) A double quotation mark preceded by a backslash is
429 interpreted as a literal double quotation mark.
431 4) Backslashes are interpreted literally, unless they
432 immediately precede a double quotation mark.
434 5) If backslashes immediately precede a double quotation mark,
435 every pair of backslashes is interpreted as a literal
436 backslash. If the number of backslashes is odd, the last
437 backslash escapes the next double quotation mark as
442 # http://msdn.microsoft.com/library/en-us/vccelng/htm/progs_12.asp
448 # Add a space to separate this argument from the others
452 needquote
= (" " in arg
) or ("\t" in arg
)
458 # Don't know if we need to double yet.
462 result
.append('\\' * len(bs_buf
)*2)
468 result
.extend(bs_buf
)
472 # Add remaining backspaces, if any.
474 result
.extend(bs_buf
)
477 result
.extend(bs_buf
)
480 return ''.join(result
)
484 def __init__(self
, args
, bufsize
=0, executable
=None,
485 stdin
=None, stdout
=None, stderr
=None,
486 preexec_fn
=None, close_fds
=False, shell
=False,
487 cwd
=None, env
=None, universal_newlines
=False,
488 startupinfo
=None, creationflags
=0):
489 """Create new Popen instance."""
492 if not isinstance(bufsize
, (int, long)):
493 raise TypeError("bufsize must be an integer")
496 if preexec_fn
is not None:
497 raise ValueError("preexec_fn is not supported on Windows "
500 raise ValueError("close_fds is not supported on Windows "
504 if startupinfo
is not None:
505 raise ValueError("startupinfo is only supported on Windows "
507 if creationflags
!= 0:
508 raise ValueError("creationflags is only supported on Windows "
515 self
.returncode
= None
516 self
.universal_newlines
= universal_newlines
518 # Input and output objects. The general principle is like
523 # p2cwrite ---stdin---> p2cread
524 # c2pread <--stdout--- c2pwrite
525 # errread <--stderr--- errwrite
527 # On POSIX, the child objects are file descriptors. On
528 # Windows, these are Windows file handles. The parent objects
529 # are file descriptors on both platforms. The parent objects
530 # are None when not using PIPEs. The child objects are None
531 # when not redirecting.
535 errread
, errwrite
) = self
._get
_handles
(stdin
, stdout
, stderr
)
537 self
._execute
_child
(args
, executable
, preexec_fn
, close_fds
,
538 cwd
, env
, universal_newlines
,
539 startupinfo
, creationflags
, shell
,
545 self
.stdin
= os
.fdopen(p2cwrite
, 'wb', bufsize
)
547 if universal_newlines
:
548 self
.stdout
= os
.fdopen(c2pread
, 'rU', bufsize
)
550 self
.stdout
= os
.fdopen(c2pread
, 'rb', bufsize
)
552 if universal_newlines
:
553 self
.stderr
= os
.fdopen(errread
, 'rU', bufsize
)
555 self
.stderr
= os
.fdopen(errread
, 'rb', bufsize
)
560 def _translate_newlines(self
, data
):
561 data
= data
.replace("\r\n", "\n")
562 data
= data
.replace("\r", "\n")
570 def _get_handles(self
, stdin
, stdout
, stderr
):
571 """Construct and return tuple with IO objects:
572 p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
574 if stdin
== None and stdout
== None and stderr
== None:
575 return (None, None, None, None, None, None)
577 p2cread
, p2cwrite
= None, None
578 c2pread
, c2pwrite
= None, None
579 errread
, errwrite
= None, None
582 p2cread
= GetStdHandle(STD_INPUT_HANDLE
)
584 p2cread
, p2cwrite
= CreatePipe(None, 0)
585 # Detach and turn into fd
586 p2cwrite
= p2cwrite
.Detach()
587 p2cwrite
= msvcrt
.open_osfhandle(p2cwrite
, 0)
588 elif type(stdin
) == types
.IntType
:
589 p2cread
= msvcrt
.get_osfhandle(stdin
)
591 # Assuming file-like object
592 p2cread
= msvcrt
.get_osfhandle(stdin
.fileno())
593 p2cread
= self
._make
_inheritable
(p2cread
)
596 c2pwrite
= GetStdHandle(STD_OUTPUT_HANDLE
)
598 c2pread
, c2pwrite
= CreatePipe(None, 0)
599 # Detach and turn into fd
600 c2pread
= c2pread
.Detach()
601 c2pread
= msvcrt
.open_osfhandle(c2pread
, 0)
602 elif type(stdout
) == types
.IntType
:
603 c2pwrite
= msvcrt
.get_osfhandle(stdout
)
605 # Assuming file-like object
606 c2pwrite
= msvcrt
.get_osfhandle(stdout
.fileno())
607 c2pwrite
= self
._make
_inheritable
(c2pwrite
)
610 errwrite
= GetStdHandle(STD_ERROR_HANDLE
)
612 errread
, errwrite
= CreatePipe(None, 0)
613 # Detach and turn into fd
614 errread
= errread
.Detach()
615 errread
= msvcrt
.open_osfhandle(errread
, 0)
616 elif stderr
== STDOUT
:
618 elif type(stderr
) == types
.IntType
:
619 errwrite
= msvcrt
.get_osfhandle(stderr
)
621 # Assuming file-like object
622 errwrite
= msvcrt
.get_osfhandle(stderr
.fileno())
623 errwrite
= self
._make
_inheritable
(errwrite
)
625 return (p2cread
, p2cwrite
,
630 def _make_inheritable(self
, handle
):
631 """Return a duplicate of handle, which is inheritable"""
632 return DuplicateHandle(GetCurrentProcess(), handle
,
633 GetCurrentProcess(), 0, 1,
634 DUPLICATE_SAME_ACCESS
)
637 def _find_w9xpopen(self
):
638 """Find and return absolute path to w9xpopen.exe"""
639 w9xpopen
= os
.path
.join(os
.path
.dirname(GetModuleFileName(0)),
641 if not os
.path
.exists(w9xpopen
):
642 # Eeek - file-not-found - possibly an embedding
643 # situation - see if we can locate it in sys.exec_prefix
644 w9xpopen
= os
.path
.join(os
.path
.dirname(sys
.exec_prefix
),
646 if not os
.path
.exists(w9xpopen
):
647 raise RuntimeError("Cannot locate w9xpopen.exe, which is "
648 "needed for Popen to work with your "
649 "shell or platform.")
653 def _execute_child(self
, args
, executable
, preexec_fn
, close_fds
,
654 cwd
, env
, universal_newlines
,
655 startupinfo
, creationflags
, shell
,
659 """Execute program (MS Windows version)"""
661 if not isinstance(args
, types
.StringTypes
):
662 args
= list2cmdline(args
)
664 # Process startup details
665 default_startupinfo
= STARTUPINFO()
666 if startupinfo
== None:
667 startupinfo
= default_startupinfo
668 if not None in (p2cread
, c2pwrite
, errwrite
):
669 startupinfo
.dwFlags |
= STARTF_USESTDHANDLES
670 startupinfo
.hStdInput
= p2cread
671 startupinfo
.hStdOutput
= c2pwrite
672 startupinfo
.hStdError
= errwrite
675 default_startupinfo
.dwFlags |
= STARTF_USESHOWWINDOW
676 default_startupinfo
.wShowWindow
= SW_HIDE
677 comspec
= os
.environ
.get("COMSPEC", "cmd.exe")
678 args
= comspec
+ " /c " + args
679 if (GetVersion() >= 0x80000000L
or
680 os
.path
.basename(comspec
).lower() == "command.com"):
681 # Win9x, or using command.com on NT. We need to
682 # use the w9xpopen intermediate program. For more
683 # information, see KB Q150956
684 # (http://web.archive.org/web/20011105084002/http://support.microsoft.com/support/kb/articles/Q150/9/56.asp)
685 w9xpopen
= self
._find
_w
9xpopen
()
686 args
= '"%s" %s' % (w9xpopen
, args
)
687 # Not passing CREATE_NEW_CONSOLE has been known to
688 # cause random failures on win9x. Specifically a
689 # dialog: "Your program accessed mem currently in
690 # use at xxx" and a hopeful warning about the
691 # stability of your system. Cost is Ctrl+C wont
693 creationflags |
= CREATE_NEW_CONSOLE
697 hp
, ht
, pid
, tid
= CreateProcess(executable
, args
,
698 # no special security
700 # must inherit handles to pass std
707 except pywintypes
.error
, e
:
708 # Translate pywintypes.error to WindowsError, which is
709 # a subclass of OSError. FIXME: We should really
710 # translate errno using _sys_errlist (or simliar), but
711 # how can this be done from Python?
712 raise WindowsError(*e
.args
)
714 # Retain the process handle, but close the thread handle
719 # Child is launched. Close the parent's copy of those pipe
720 # handles that only the child should have open. You need
721 # to make sure that no handles to the write end of the
722 # output pipe are maintained in this process or else the
723 # pipe will not close when the child process exits and the
724 # ReadFile will hang.
734 """Check if child process has terminated. Returns returncode
736 if self
.returncode
== None:
737 if WaitForSingleObject(self
._handle
, 0) == WAIT_OBJECT_0
:
738 self
.returncode
= GetExitCodeProcess(self
._handle
)
740 return self
.returncode
744 """Wait for child process to terminate. Returns returncode
746 if self
.returncode
== None:
747 obj
= WaitForSingleObject(self
._handle
, INFINITE
)
748 self
.returncode
= GetExitCodeProcess(self
._handle
)
750 return self
.returncode
753 def _readerthread(self
, fh
, buffer):
754 buffer.append(fh
.read())
757 def communicate(self
, input=None):
758 """Interact with process: Send data to stdin. Read data from
759 stdout and stderr, until end-of-file is reached. Wait for
760 process to terminate. The optional input argument should be a
761 string to be sent to the child process, or None, if no data
762 should be sent to the child.
764 communicate() returns a tuple (stdout, stderr)."""
765 stdout
= None # Return
766 stderr
= None # Return
770 stdout_thread
= threading
.Thread(target
=self
._readerthread
,
771 args
=(self
.stdout
, stdout
))
772 stdout_thread
.setDaemon(True)
773 stdout_thread
.start()
776 stderr_thread
= threading
.Thread(target
=self
._readerthread
,
777 args
=(self
.stderr
, stderr
))
778 stderr_thread
.setDaemon(True)
779 stderr_thread
.start()
783 self
.stdin
.write(input)
791 # All data exchanged. Translate lists into strings.
797 # Translate newlines, if requested. We cannot let the file
798 # object do the translation: It is based on stdio, which is
799 # impossible to combine with select (unless forcing no
801 if self
.universal_newlines
and hasattr(open, 'newlines'):
803 stdout
= self
._translate
_newlines
(stdout
)
805 stderr
= self
._translate
_newlines
(stderr
)
808 return (stdout
, stderr
)
814 def _get_handles(self
, stdin
, stdout
, stderr
):
815 """Construct and return tuple with IO objects:
816 p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
818 p2cread
, p2cwrite
= None, None
819 c2pread
, c2pwrite
= None, None
820 errread
, errwrite
= None, None
825 p2cread
, p2cwrite
= os
.pipe()
826 elif type(stdin
) == types
.IntType
:
829 # Assuming file-like object
830 p2cread
= stdin
.fileno()
835 c2pread
, c2pwrite
= os
.pipe()
836 elif type(stdout
) == types
.IntType
:
839 # Assuming file-like object
840 c2pwrite
= stdout
.fileno()
845 errread
, errwrite
= os
.pipe()
846 elif stderr
== STDOUT
:
848 elif type(stderr
) == types
.IntType
:
851 # Assuming file-like object
852 errwrite
= stderr
.fileno()
854 return (p2cread
, p2cwrite
,
859 def _set_cloexec_flag(self
, fd
):
861 cloexec_flag
= fcntl
.FD_CLOEXEC
862 except AttributeError:
865 old
= fcntl
.fcntl(fd
, fcntl
.F_GETFD
)
866 fcntl
.fcntl(fd
, fcntl
.F_SETFD
, old | cloexec_flag
)
869 def _close_fds(self
, but
):
870 for i
in range(3, MAXFD
):
879 def _execute_child(self
, args
, executable
, preexec_fn
, close_fds
,
880 cwd
, env
, universal_newlines
,
881 startupinfo
, creationflags
, shell
,
885 """Execute program (POSIX version)"""
887 if isinstance(args
, types
.StringTypes
):
891 args
= ["/bin/sh", "-c"] + args
893 if executable
== None:
896 # For transferring possible exec failure from child to parent
897 # The first char specifies the exception type: 0 means
898 # OSError, 1 means some other error.
899 errpipe_read
, errpipe_write
= os
.pipe()
900 self
._set
_cloexec
_flag
(errpipe_write
)
906 # Close parent's pipe ends
913 os
.close(errpipe_read
)
923 # Close pipe fds. Make sure we doesn't close the same
927 if c2pwrite
and c2pwrite
not in (p2cread
,):
929 if errwrite
and errwrite
not in (p2cread
, c2pwrite
):
932 # Close all other fds, if asked for
934 self
._close
_fds
(but
=errpipe_write
)
943 os
.execvp(executable
, args
)
945 os
.execvpe(executable
, args
, env
)
948 exc_type
, exc_value
, tb
= sys
.exc_info()
949 # Save the traceback and attach it to the exception object
950 exc_lines
= traceback
.format_exception(exc_type
,
953 exc_value
.child_traceback
= ''.join(exc_lines
)
954 os
.write(errpipe_write
, pickle
.dumps(exc_value
))
956 # This exitcode won't be reported to applications, so it
957 # really doesn't matter what we return.
961 os
.close(errpipe_write
)
962 if p2cread
and p2cwrite
:
964 if c2pwrite
and c2pread
:
966 if errwrite
and errread
:
969 # Wait for exec to fail or succeed; possibly raising exception
970 data
= os
.read(errpipe_read
, 1048576) # Exceptions limited to 1 MB
971 os
.close(errpipe_read
)
973 os
.waitpid(self
.pid
, 0)
974 child_exception
= pickle
.loads(data
)
975 raise child_exception
978 def _handle_exitstatus(self
, sts
):
979 if os
.WIFSIGNALED(sts
):
980 self
.returncode
= -os
.WTERMSIG(sts
)
981 elif os
.WIFEXITED(sts
):
982 self
.returncode
= os
.WEXITSTATUS(sts
)
984 # Should never happen
985 raise RuntimeError("Unknown child exit status!")
991 """Check if child process has terminated. Returns returncode
993 if self
.returncode
== None:
995 pid
, sts
= os
.waitpid(self
.pid
, os
.WNOHANG
)
997 self
._handle
_exitstatus
(sts
)
1000 return self
.returncode
1004 """Wait for child process to terminate. Returns returncode
1006 if self
.returncode
== None:
1007 pid
, sts
= os
.waitpid(self
.pid
, 0)
1008 self
._handle
_exitstatus
(sts
)
1009 return self
.returncode
1012 def communicate(self
, input=None):
1013 """Interact with process: Send data to stdin. Read data from
1014 stdout and stderr, until end-of-file is reached. Wait for
1015 process to terminate. The optional input argument should be a
1016 string to be sent to the child process, or None, if no data
1017 should be sent to the child.
1019 communicate() returns a tuple (stdout, stderr)."""
1022 stdout
= None # Return
1023 stderr
= None # Return
1026 # Flush stdio buffer. This might block, if the user has
1027 # been writing to .stdin in an uncontrolled fashion.
1030 write_set
.append(self
.stdin
)
1034 read_set
.append(self
.stdout
)
1037 read_set
.append(self
.stderr
)
1040 while read_set
or write_set
:
1041 rlist
, wlist
, xlist
= select
.select(read_set
, write_set
, [])
1043 if self
.stdin
in wlist
:
1044 # When select has indicated that the file is writable,
1045 # we can write up to PIPE_BUF bytes without risk
1046 # blocking. POSIX defines PIPE_BUF >= 512
1047 bytes_written
= os
.write(self
.stdin
.fileno(), input[:512])
1048 input = input[bytes_written
:]
1051 write_set
.remove(self
.stdin
)
1053 if self
.stdout
in rlist
:
1054 data
= os
.read(self
.stdout
.fileno(), 1024)
1057 read_set
.remove(self
.stdout
)
1060 if self
.stderr
in rlist
:
1061 data
= os
.read(self
.stderr
.fileno(), 1024)
1064 read_set
.remove(self
.stderr
)
1067 # All data exchanged. Translate lists into strings.
1069 stdout
= ''.join(stdout
)
1071 stderr
= ''.join(stderr
)
1073 # Translate newlines, if requested. We cannot let the file
1074 # object do the translation: It is based on stdio, which is
1075 # impossible to combine with select (unless forcing no
1077 if self
.universal_newlines
and hasattr(open, 'newlines'):
1079 stdout
= self
._translate
_newlines
(stdout
)
1081 stderr
= self
._translate
_newlines
(stderr
)
1084 return (stdout
, stderr
)
1089 # Example 1: Simple redirection: Get process list
1091 plist
= Popen(["ps"], stdout
=PIPE
).communicate()[0]
1092 print "Process list:"
1096 # Example 2: Change uid before executing child
1098 if os
.getuid() == 0:
1099 p
= Popen(["id"], preexec_fn
=lambda: os
.setuid(100))
1103 # Example 3: Connecting several subprocesses
1105 print "Looking for 'hda'..."
1106 p1
= Popen(["dmesg"], stdout
=PIPE
)
1107 p2
= Popen(["grep", "hda"], stdin
=p1
.stdout
, stdout
=PIPE
)
1108 print repr(p2
.communicate()[0])
1111 # Example 4: Catch execution error
1114 print "Trying a weird file..."
1116 print Popen(["/this/path/does/not/exist"]).communicate()
1118 if e
.errno
== errno
.ENOENT
:
1119 print "The file didn't exist. I thought so..."
1120 print "Child traceback:"
1121 print e
.child_traceback
1123 print "Error", e
.errno
1125 print >>sys
.stderr
, "Gosh. No error."
1128 def _demo_windows():
1130 # Example 1: Connecting several subprocesses
1132 print "Looking for 'PROMPT' in set output..."
1133 p1
= Popen("set", stdout
=PIPE
, shell
=True)
1134 p2
= Popen('find "PROMPT"', stdin
=p1
.stdout
, stdout
=PIPE
)
1135 print repr(p2
.communicate()[0])
1138 # Example 2: Simple execution of program
1140 print "Executing calc..."
1145 if __name__
== "__main__":