blob: 49137d450379b6c155fdf1084e8f077caeba8572 [file] [log] [blame]
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001# subprocess - Subprocesses with accessible I/O streams
2#
Tim Peterse718f612004-10-12 21:51:32 +00003# For more information about this module, see PEP 324.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00004#
Peter Astrand3a708df2005-09-23 17:37:29 +00005# Copyright (c) 2003-2005 by Peter Astrand <astrand@lysator.liu.se>
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00006#
Peter Astrand69bf13f2005-02-14 08:56:32 +00007# Licensed to PSF under a Contributor Agreement.
Peter Astrand3a708df2005-09-23 17:37:29 +00008# See http://www.python.org/2.4/license for licensing details.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00009
Raymond Hettinger837dd932004-10-17 16:36:53 +000010r"""subprocess - Subprocesses with accessible I/O streams
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000011
Fredrik Lundh15aaacc2004-10-17 14:47:05 +000012This module allows you to spawn processes, connect to their
13input/output/error pipes, and obtain their return codes. This module
14intends to replace several other, older modules and functions, like:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000015
16os.system
17os.spawn*
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000018
19Information about how the subprocess module can be used to replace these
20modules and functions can be found below.
21
22
23
24Using the subprocess module
25===========================
26This module defines one class called Popen:
27
28class Popen(args, bufsize=0, executable=None,
29 stdin=None, stdout=None, stderr=None,
Gregory P. Smith8edd99d2010-12-14 13:43:30 +000030 preexec_fn=None, close_fds=True, shell=False,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000031 cwd=None, env=None, universal_newlines=False,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000032 startupinfo=None, creationflags=0,
Gregory P. Smith8edd99d2010-12-14 13:43:30 +000033 restore_signals=True, start_new_session=False, pass_fds=()):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000034
35
36Arguments are:
37
38args should be a string, or a sequence of program arguments. The
39program to execute is normally the first item in the args sequence or
40string, but can be explicitly set by using the executable argument.
41
Gregory P. Smithf5604852010-12-13 06:45:02 +000042On POSIX, with shell=False (default): In this case, the Popen class
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000043uses os.execvp() to execute the child program. args should normally
44be a sequence. A string will be treated as a sequence with the string
45as the only item (the program to execute).
46
Gregory P. Smithf5604852010-12-13 06:45:02 +000047On POSIX, with shell=True: If args is a string, it specifies the
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000048command string to execute through the shell. If args is a sequence,
49the first item specifies the command string, and any additional items
50will be treated as additional shell arguments.
51
52On Windows: the Popen class uses CreateProcess() to execute the child
53program, which operates on strings. If args is a sequence, it will be
54converted to a string using the list2cmdline method. Please note that
55not all MS Windows applications interpret the command line the same
56way: The list2cmdline is designed for applications using the same
57rules as the MS C runtime.
58
59bufsize, if given, has the same meaning as the corresponding argument
60to the built-in open() function: 0 means unbuffered, 1 means line
61buffered, any other positive value means use a buffer of
62(approximately) that size. A negative bufsize means to use the system
63default, which usually means fully buffered. The default value for
64bufsize is 0 (unbuffered).
65
66stdin, stdout and stderr specify the executed programs' standard
67input, standard output and standard error file handles, respectively.
68Valid values are PIPE, an existing file descriptor (a positive
69integer), an existing file object, and None. PIPE indicates that a
70new pipe to the child should be created. With None, no redirection
71will occur; the child's file handles will be inherited from the
72parent. Additionally, stderr can be STDOUT, which indicates that the
73stderr data from the applications should be captured into the same
74file handle as for stdout.
75
Gregory P. Smithf5604852010-12-13 06:45:02 +000076On POSIX, if preexec_fn is set to a callable object, this object will be
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000077called in the child process just before the child is executed. The use
78of preexec_fn is not thread safe, using it in the presence of threads
79could lead to a deadlock in the child process before the new executable
80is executed.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000081
82If close_fds is true, all file descriptors except 0, 1 and 2 will be
Gregory P. Smithf5604852010-12-13 06:45:02 +000083closed before the child process is executed. The default for close_fds
Gregory P. Smith8edd99d2010-12-14 13:43:30 +000084varies by platform: Always true on POSIX. True when stdin/stdout/stderr
85are None on Windows, false otherwise.
86
87pass_fds is an optional sequence of file descriptors to keep open between the
88parent and child. Providing any pass_fds implicitly sets close_fds to true.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000089
90if shell is true, the specified command will be executed through the
91shell.
92
93If cwd is not None, the current directory will be changed to cwd
94before the child is executed.
95
Gregory P. Smithf5604852010-12-13 06:45:02 +000096On POSIX, if restore_signals is True all signals that Python sets to
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000097SIG_IGN are restored to SIG_DFL in the child process before the exec.
98Currently this includes the SIGPIPE, SIGXFZ and SIGXFSZ signals. This
99parameter does nothing on Windows.
100
Gregory P. Smithf5604852010-12-13 06:45:02 +0000101On POSIX, if start_new_session is True, the setsid() system call will be made
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000102in the child process prior to executing the command.
103
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000104If env is not None, it defines the environment variables for the new
105process.
106
107If universal_newlines is true, the file objects stdout and stderr are
108opened as a text files, but lines may be terminated by any of '\n',
Gregory P. Smithf5604852010-12-13 06:45:02 +0000109the Unix end-of-line convention, '\r', the old Macintosh convention or
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000110'\r\n', the Windows convention. All of these external representations
111are seen as '\n' by the Python program. Note: This feature is only
112available if Python is built with universal newline support (the
113default). Also, the newlines attribute of the file objects stdout,
114stdin and stderr are not updated by the communicate() method.
115
116The startupinfo and creationflags, if given, will be passed to the
117underlying CreateProcess() function. They can specify things such as
118appearance of the main window and priority for the new process.
119(Windows only)
120
121
Georg Brandlf9734072008-12-07 15:30:06 +0000122This module also defines some shortcut functions:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000123
Peter Astrand5f5e1412004-12-05 20:15:36 +0000124call(*popenargs, **kwargs):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000125 Run command with arguments. Wait for command to complete, then
126 return the returncode attribute.
127
128 The arguments are the same as for the Popen constructor. Example:
129
Florent Xicluna4886d242010-03-08 13:27:26 +0000130 >>> retcode = subprocess.call(["ls", "-l"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000131
Peter Astrand454f7672005-01-01 09:36:35 +0000132check_call(*popenargs, **kwargs):
133 Run command with arguments. Wait for command to complete. If the
134 exit code was zero then return, otherwise raise
135 CalledProcessError. The CalledProcessError object will have the
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000136 return code in the returncode attribute.
Peter Astrand454f7672005-01-01 09:36:35 +0000137
138 The arguments are the same as for the Popen constructor. Example:
139
Florent Xicluna4886d242010-03-08 13:27:26 +0000140 >>> subprocess.check_call(["ls", "-l"])
Georg Brandl2708f3a2009-12-20 14:38:23 +0000141 0
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000142
Brett Cannona23810f2008-05-26 19:04:21 +0000143getstatusoutput(cmd):
144 Return (status, output) of executing cmd in a shell.
145
146 Execute the string 'cmd' in a shell with os.popen() and return a 2-tuple
147 (status, output). cmd is actually run as '{ cmd ; } 2>&1', so that the
148 returned output will contain output or error messages. A trailing newline
149 is stripped from the output. The exit status for the command can be
150 interpreted according to the rules for the C function wait(). Example:
151
Brett Cannona23810f2008-05-26 19:04:21 +0000152 >>> subprocess.getstatusoutput('ls /bin/ls')
153 (0, '/bin/ls')
154 >>> subprocess.getstatusoutput('cat /bin/junk')
155 (256, 'cat: /bin/junk: No such file or directory')
156 >>> subprocess.getstatusoutput('/bin/junk')
157 (256, 'sh: /bin/junk: not found')
158
159getoutput(cmd):
160 Return output (stdout or stderr) of executing cmd in a shell.
161
162 Like getstatusoutput(), except the exit status is ignored and the return
163 value is a string containing the command's output. Example:
164
Brett Cannona23810f2008-05-26 19:04:21 +0000165 >>> subprocess.getoutput('ls /bin/ls')
166 '/bin/ls'
167
Georg Brandlf9734072008-12-07 15:30:06 +0000168check_output(*popenargs, **kwargs):
Georg Brandl2708f3a2009-12-20 14:38:23 +0000169 Run command with arguments and return its output as a byte string.
Georg Brandlf9734072008-12-07 15:30:06 +0000170
Georg Brandl2708f3a2009-12-20 14:38:23 +0000171 If the exit code was non-zero it raises a CalledProcessError. The
172 CalledProcessError object will have the return code in the returncode
173 attribute and output in the output attribute.
Georg Brandlf9734072008-12-07 15:30:06 +0000174
Georg Brandl2708f3a2009-12-20 14:38:23 +0000175 The arguments are the same as for the Popen constructor. Example:
Georg Brandlf9734072008-12-07 15:30:06 +0000176
Georg Brandl2708f3a2009-12-20 14:38:23 +0000177 >>> output = subprocess.check_output(["ls", "-l", "/dev/null"])
Georg Brandlf9734072008-12-07 15:30:06 +0000178
Brett Cannona23810f2008-05-26 19:04:21 +0000179
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000180Exceptions
181----------
182Exceptions raised in the child process, before the new program has
183started to execute, will be re-raised in the parent. Additionally,
184the exception object will have one extra attribute called
185'child_traceback', which is a string containing traceback information
186from the childs point of view.
187
188The most common exception raised is OSError. This occurs, for
189example, when trying to execute a non-existent file. Applications
190should prepare for OSErrors.
191
192A ValueError will be raised if Popen is called with invalid arguments.
193
Gregory P. Smith54d412e2011-03-14 14:08:43 -0400194Exceptions defined within this module inherit from SubprocessError.
195check_call() and check_output() will raise CalledProcessError if the
Gregory P. Smithb4039aa2011-03-14 14:16:20 -0400196called process returns a non-zero return code. TimeoutExpired
Gregory P. Smith54d412e2011-03-14 14:08:43 -0400197be raised if a timeout was specified and expired.
Peter Astrand454f7672005-01-01 09:36:35 +0000198
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000199
200Security
201--------
202Unlike some other popen functions, this implementation will never call
203/bin/sh implicitly. This means that all characters, including shell
204metacharacters, can safely be passed to child processes.
205
206
207Popen objects
208=============
209Instances of the Popen class have the following methods:
210
211poll()
212 Check if child process has terminated. Returns returncode
213 attribute.
214
215wait()
216 Wait for child process to terminate. Returns returncode attribute.
217
218communicate(input=None)
219 Interact with process: Send data to stdin. Read data from stdout
220 and stderr, until end-of-file is reached. Wait for process to
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000221 terminate. The optional input argument should be a string to be
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000222 sent to the child process, or None, if no data should be sent to
223 the child.
Tim Peterse718f612004-10-12 21:51:32 +0000224
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000225 communicate() returns a tuple (stdout, stderr).
226
227 Note: The data read is buffered in memory, so do not use this
228 method if the data size is large or unlimited.
229
230The following attributes are also available:
231
232stdin
233 If the stdin argument is PIPE, this attribute is a file object
234 that provides input to the child process. Otherwise, it is None.
235
236stdout
237 If the stdout argument is PIPE, this attribute is a file object
238 that provides output from the child process. Otherwise, it is
239 None.
240
241stderr
242 If the stderr argument is PIPE, this attribute is file object that
243 provides error output from the child process. Otherwise, it is
244 None.
245
246pid
247 The process ID of the child process.
248
249returncode
250 The child return code. A None value indicates that the process
251 hasn't terminated yet. A negative value -N indicates that the
Gregory P. Smithf5604852010-12-13 06:45:02 +0000252 child was terminated by signal N (POSIX only).
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000253
254
255Replacing older functions with the subprocess module
256====================================================
257In this section, "a ==> b" means that b can be used as a replacement
258for a.
259
260Note: All functions in this section fail (more or less) silently if
261the executed program cannot be found; this module raises an OSError
262exception.
263
264In the following examples, we assume that the subprocess module is
265imported with "from subprocess import *".
266
267
268Replacing /bin/sh shell backquote
269---------------------------------
270output=`mycmd myarg`
271==>
272output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]
273
274
275Replacing shell pipe line
276-------------------------
277output=`dmesg | grep hda`
278==>
279p1 = Popen(["dmesg"], stdout=PIPE)
Peter Astrand6fdf3cb2004-11-30 18:06:42 +0000280p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000281output = p2.communicate()[0]
282
283
284Replacing os.system()
285---------------------
286sts = os.system("mycmd" + " myarg")
287==>
288p = Popen("mycmd" + " myarg", shell=True)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000289pid, sts = os.waitpid(p.pid, 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000290
291Note:
292
293* Calling the program through the shell is usually not required.
294
295* It's easier to look at the returncode attribute than the
296 exitstatus.
297
298A more real-world example would look like this:
299
300try:
301 retcode = call("mycmd" + " myarg", shell=True)
302 if retcode < 0:
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000303 print("Child was terminated by signal", -retcode, file=sys.stderr)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000304 else:
Guido van Rossumc2f93dc2007-05-24 00:50:02 +0000305 print("Child returned", retcode, file=sys.stderr)
306except OSError as e:
307 print("Execution failed:", e, file=sys.stderr)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000308
309
310Replacing os.spawn*
311-------------------
Tim Peterse718f612004-10-12 21:51:32 +0000312P_NOWAIT example:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000313
314pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
315==>
316pid = Popen(["/bin/mycmd", "myarg"]).pid
317
318
319P_WAIT example:
320
321retcode = os.spawnlp(os.P_WAIT, "/bin/mycmd", "mycmd", "myarg")
322==>
323retcode = call(["/bin/mycmd", "myarg"])
324
325
Tim Peterse718f612004-10-12 21:51:32 +0000326Vector example:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000327
328os.spawnvp(os.P_NOWAIT, path, args)
329==>
330Popen([path] + args[1:])
331
332
Tim Peterse718f612004-10-12 21:51:32 +0000333Environment example:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000334
335os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env)
336==>
337Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000338"""
339
340import sys
341mswindows = (sys.platform == "win32")
342
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000343import io
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000344import os
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400345import time
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000346import traceback
Christian Heimesfdab48e2008-01-20 09:06:41 +0000347import gc
Christian Heimesa342c012008-04-20 21:01:16 +0000348import signal
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000349import builtins
Gregory P. Smithd23047b2010-12-04 09:10:44 +0000350import warnings
Ross Lagerwall4f61b022011-04-05 15:34:00 +0200351import errno
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000352
Peter Astrand454f7672005-01-01 09:36:35 +0000353# Exception classes used by this module.
Gregory P. Smith54d412e2011-03-14 14:08:43 -0400354class SubprocessError(Exception): pass
355
356
357class CalledProcessError(SubprocessError):
Georg Brandlf9734072008-12-07 15:30:06 +0000358 """This exception is raised when a process run by check_call() or
359 check_output() returns a non-zero exit status.
360 The exit status will be stored in the returncode attribute;
361 check_output() will also store the output in the output attribute.
362 """
363 def __init__(self, returncode, cmd, output=None):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000364 self.returncode = returncode
365 self.cmd = cmd
Georg Brandlf9734072008-12-07 15:30:06 +0000366 self.output = output
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000367 def __str__(self):
368 return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
369
Peter Astrand454f7672005-01-01 09:36:35 +0000370
Gregory P. Smith54d412e2011-03-14 14:08:43 -0400371class TimeoutExpired(SubprocessError):
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400372 """This exception is raised when the timeout expires while waiting for a
373 child process.
374 """
Reid Kleckner2b228f02011-03-16 16:57:54 -0400375 def __init__(self, cmd, timeout, output=None):
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400376 self.cmd = cmd
Reid Kleckner2b228f02011-03-16 16:57:54 -0400377 self.timeout = timeout
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400378 self.output = output
379
380 def __str__(self):
381 return ("Command '%s' timed out after %s seconds" %
382 (self.cmd, self.timeout))
383
384
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000385if mswindows:
386 import threading
387 import msvcrt
Brian Curtin1ce6b582010-04-24 16:19:22 +0000388 import _subprocess
389 class STARTUPINFO:
390 dwFlags = 0
391 hStdInput = None
392 hStdOutput = None
393 hStdError = None
394 wShowWindow = 0
395 class pywintypes:
396 error = IOError
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000397else:
398 import select
Gregory P. Smithd06fa472009-07-04 02:46:54 +0000399 _has_poll = hasattr(select, 'poll')
Gregory P. Smith59fd1bf2011-05-28 09:32:39 -0700400 import _posixsubprocess
401 _create_pipe = _posixsubprocess.cloexec_pipe
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000402
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +0000403 # When select or poll has indicated that the file is writable,
404 # we can write up to _PIPE_BUF bytes without risk of blocking.
405 # POSIX defines PIPE_BUF as >= 512.
406 _PIPE_BUF = getattr(select, 'PIPE_BUF', 512)
407
408
Brett Cannona23810f2008-05-26 19:04:21 +0000409__all__ = ["Popen", "PIPE", "STDOUT", "call", "check_call", "getstatusoutput",
Ross Lagerwallba102ec2011-03-16 18:40:25 +0200410 "getoutput", "check_output", "CalledProcessError", "DEVNULL"]
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000411
Brian Curtin1ce6b582010-04-24 16:19:22 +0000412if mswindows:
Brian Curtin08fd8d92011-04-29 16:11:30 -0500413 from _subprocess import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP,
Brian Curtin8b8e7f42011-04-29 15:48:13 -0500414 STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
415 STD_ERROR_HANDLE, SW_HIDE,
416 STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW)
Brian Curtin5d9deaa2011-04-29 16:24:07 -0500417
Brian Curtin08fd8d92011-04-29 16:11:30 -0500418 __all__.extend(["CREATE_NEW_CONSOLE", "CREATE_NEW_PROCESS_GROUP",
Brian Curtin8b8e7f42011-04-29 15:48:13 -0500419 "STD_INPUT_HANDLE", "STD_OUTPUT_HANDLE",
420 "STD_ERROR_HANDLE", "SW_HIDE",
421 "STARTF_USESTDHANDLES", "STARTF_USESHOWWINDOW"])
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000422try:
423 MAXFD = os.sysconf("SC_OPEN_MAX")
424except:
425 MAXFD = 256
426
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000427_active = []
428
429def _cleanup():
430 for inst in _active[:]:
Georg Brandl6aa2d1f2008-08-12 08:35:52 +0000431 res = inst._internal_poll(_deadstate=sys.maxsize)
Guido van Rossumb5d47ef2006-08-24 02:27:45 +0000432 if res is not None and res >= 0:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000433 try:
434 _active.remove(inst)
435 except ValueError:
436 # This can happen if two threads create a new Popen instance.
437 # It's harmless that it was already removed, so ignore.
438 pass
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000439
440PIPE = -1
441STDOUT = -2
Ross Lagerwallba102ec2011-03-16 18:40:25 +0200442DEVNULL = -3
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000443
444
Gregory P. Smitha59c59f2010-03-01 00:17:40 +0000445def _eintr_retry_call(func, *args):
446 while True:
447 try:
448 return func(*args)
449 except OSError as e:
450 if e.errno == errno.EINTR:
451 continue
452 raise
453
454
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400455def call(*popenargs, timeout=None, **kwargs):
456 """Run command with arguments. Wait for command to complete or
457 timeout, then return the returncode attribute.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000458
459 The arguments are the same as for the Popen constructor. Example:
460
461 retcode = call(["ls", "-l"])
462 """
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400463 p = Popen(*popenargs, **kwargs)
464 try:
465 return p.wait(timeout=timeout)
466 except TimeoutExpired:
467 p.kill()
468 p.wait()
469 raise
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000470
471
Peter Astrand454f7672005-01-01 09:36:35 +0000472def check_call(*popenargs, **kwargs):
473 """Run command with arguments. Wait for command to complete. If
474 the exit code was zero then return, otherwise raise
475 CalledProcessError. The CalledProcessError object will have the
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000476 return code in the returncode attribute.
Peter Astrand454f7672005-01-01 09:36:35 +0000477
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400478 The arguments are the same as for the call function. Example:
Peter Astrand454f7672005-01-01 09:36:35 +0000479
480 check_call(["ls", "-l"])
481 """
482 retcode = call(*popenargs, **kwargs)
Peter Astrand454f7672005-01-01 09:36:35 +0000483 if retcode:
Georg Brandlf9734072008-12-07 15:30:06 +0000484 cmd = kwargs.get("args")
485 if cmd is None:
486 cmd = popenargs[0]
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000487 raise CalledProcessError(retcode, cmd)
Georg Brandlf9734072008-12-07 15:30:06 +0000488 return 0
489
490
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400491def check_output(*popenargs, timeout=None, **kwargs):
Georg Brandl2708f3a2009-12-20 14:38:23 +0000492 r"""Run command with arguments and return its output as a byte string.
Georg Brandlf9734072008-12-07 15:30:06 +0000493
494 If the exit code was non-zero it raises a CalledProcessError. The
495 CalledProcessError object will have the return code in the returncode
496 attribute and output in the output attribute.
497
498 The arguments are the same as for the Popen constructor. Example:
499
500 >>> check_output(["ls", "-l", "/dev/null"])
Georg Brandl2708f3a2009-12-20 14:38:23 +0000501 b'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
Georg Brandlf9734072008-12-07 15:30:06 +0000502
503 The stdout argument is not allowed as it is used internally.
Georg Brandl127d4702009-12-28 08:10:38 +0000504 To capture standard error in the result, use stderr=STDOUT.
Georg Brandlf9734072008-12-07 15:30:06 +0000505
506 >>> check_output(["/bin/sh", "-c",
Georg Brandl2708f3a2009-12-20 14:38:23 +0000507 ... "ls -l non_existent_file ; exit 0"],
Georg Brandl127d4702009-12-28 08:10:38 +0000508 ... stderr=STDOUT)
Georg Brandl2708f3a2009-12-20 14:38:23 +0000509 b'ls: non_existent_file: No such file or directory\n'
Georg Brandlf9734072008-12-07 15:30:06 +0000510 """
511 if 'stdout' in kwargs:
512 raise ValueError('stdout argument not allowed, it will be overridden.')
513 process = Popen(*popenargs, stdout=PIPE, **kwargs)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400514 try:
515 output, unused_err = process.communicate(timeout=timeout)
516 except TimeoutExpired:
517 process.kill()
518 output, unused_err = process.communicate()
Reid Kleckner2b228f02011-03-16 16:57:54 -0400519 raise TimeoutExpired(process.args, timeout, output=output)
Georg Brandlf9734072008-12-07 15:30:06 +0000520 retcode = process.poll()
521 if retcode:
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400522 raise CalledProcessError(retcode, process.args, output=output)
Georg Brandlf9734072008-12-07 15:30:06 +0000523 return output
Peter Astrand454f7672005-01-01 09:36:35 +0000524
525
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000526def list2cmdline(seq):
527 """
528 Translate a sequence of arguments into a command line
529 string, using the same rules as the MS C runtime:
530
531 1) Arguments are delimited by white space, which is either a
532 space or a tab.
533
534 2) A string surrounded by double quotation marks is
535 interpreted as a single argument, regardless of white space
Jean-Paul Calderone1ddd4072010-06-18 20:03:54 +0000536 contained within. A quoted string can be embedded in an
537 argument.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000538
539 3) A double quotation mark preceded by a backslash is
540 interpreted as a literal double quotation mark.
541
542 4) Backslashes are interpreted literally, unless they
543 immediately precede a double quotation mark.
544
545 5) If backslashes immediately precede a double quotation mark,
546 every pair of backslashes is interpreted as a literal
547 backslash. If the number of backslashes is odd, the last
548 backslash escapes the next double quotation mark as
549 described in rule 3.
550 """
551
552 # See
Eric Smith3c573af2009-11-09 15:23:15 +0000553 # http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
554 # or search http://msdn.microsoft.com for
555 # "Parsing C++ Command-Line Arguments"
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000556 result = []
557 needquote = False
558 for arg in seq:
559 bs_buf = []
560
561 # Add a space to separate this argument from the others
562 if result:
563 result.append(' ')
564
Jean-Paul Calderone1ddd4072010-06-18 20:03:54 +0000565 needquote = (" " in arg) or ("\t" in arg) or not arg
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000566 if needquote:
567 result.append('"')
568
569 for c in arg:
570 if c == '\\':
Tim Peterse718f612004-10-12 21:51:32 +0000571 # Don't know if we need to double yet.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000572 bs_buf.append(c)
573 elif c == '"':
Christian Heimesfdab48e2008-01-20 09:06:41 +0000574 # Double backslashes.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000575 result.append('\\' * len(bs_buf)*2)
576 bs_buf = []
577 result.append('\\"')
578 else:
579 # Normal char
580 if bs_buf:
581 result.extend(bs_buf)
582 bs_buf = []
583 result.append(c)
584
Christian Heimesfdab48e2008-01-20 09:06:41 +0000585 # Add remaining backslashes, if any.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000586 if bs_buf:
587 result.extend(bs_buf)
588
589 if needquote:
Peter Astrand7e78ade2005-03-03 21:10:23 +0000590 result.extend(bs_buf)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000591 result.append('"')
592
593 return ''.join(result)
594
595
Brett Cannona23810f2008-05-26 19:04:21 +0000596# Various tools for executing commands and looking at their output and status.
597#
Gregory P. Smithf5604852010-12-13 06:45:02 +0000598# NB This only works (and is only relevant) for POSIX.
Brett Cannona23810f2008-05-26 19:04:21 +0000599
600def getstatusoutput(cmd):
601 """Return (status, output) of executing cmd in a shell.
602
603 Execute the string 'cmd' in a shell with os.popen() and return a 2-tuple
604 (status, output). cmd is actually run as '{ cmd ; } 2>&1', so that the
605 returned output will contain output or error messages. A trailing newline
606 is stripped from the output. The exit status for the command can be
607 interpreted according to the rules for the C function wait(). Example:
608
609 >>> import subprocess
610 >>> subprocess.getstatusoutput('ls /bin/ls')
611 (0, '/bin/ls')
612 >>> subprocess.getstatusoutput('cat /bin/junk')
613 (256, 'cat: /bin/junk: No such file or directory')
614 >>> subprocess.getstatusoutput('/bin/junk')
615 (256, 'sh: /bin/junk: not found')
616 """
617 pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
618 text = pipe.read()
619 sts = pipe.close()
620 if sts is None: sts = 0
621 if text[-1:] == '\n': text = text[:-1]
622 return sts, text
623
624
625def getoutput(cmd):
626 """Return output (stdout or stderr) of executing cmd in a shell.
627
628 Like getstatusoutput(), except the exit status is ignored and the return
629 value is a string containing the command's output. Example:
630
631 >>> import subprocess
632 >>> subprocess.getoutput('ls /bin/ls')
633 '/bin/ls'
634 """
635 return getstatusoutput(cmd)[1]
636
637
Gregory P. Smith8edd99d2010-12-14 13:43:30 +0000638_PLATFORM_DEFAULT_CLOSE_FDS = object()
Gregory P. Smithf5604852010-12-13 06:45:02 +0000639
640
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000641class Popen(object):
642 def __init__(self, args, bufsize=0, executable=None,
643 stdin=None, stdout=None, stderr=None,
Gregory P. Smith8edd99d2010-12-14 13:43:30 +0000644 preexec_fn=None, close_fds=_PLATFORM_DEFAULT_CLOSE_FDS,
645 shell=False, cwd=None, env=None, universal_newlines=False,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000646 startupinfo=None, creationflags=0,
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000647 restore_signals=True, start_new_session=False,
648 pass_fds=()):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000649 """Create new Popen instance."""
650 _cleanup()
651
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000652 self._child_created = False
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400653 self._input = None
654 self._communication_started = False
Guido van Rossum46a05a72007-06-07 21:56:45 +0000655 if bufsize is None:
656 bufsize = 0 # Restore default
Walter Dörwaldaa97f042007-05-03 21:05:51 +0000657 if not isinstance(bufsize, int):
Peter Astrand738131d2004-11-30 21:04:45 +0000658 raise TypeError("bufsize must be an integer")
659
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000660 if mswindows:
Tim Peterse8374a52004-10-13 03:15:00 +0000661 if preexec_fn is not None:
662 raise ValueError("preexec_fn is not supported on Windows "
663 "platforms")
Gregory P. Smith8edd99d2010-12-14 13:43:30 +0000664 any_stdio_set = (stdin is not None or stdout is not None or
665 stderr is not None)
666 if close_fds is _PLATFORM_DEFAULT_CLOSE_FDS:
667 if any_stdio_set:
668 close_fds = False
669 else:
670 close_fds = True
671 elif close_fds and any_stdio_set:
672 raise ValueError(
673 "close_fds is not supported on Windows platforms"
674 " if you redirect stdin/stdout/stderr")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000675 else:
676 # POSIX
Gregory P. Smith8edd99d2010-12-14 13:43:30 +0000677 if close_fds is _PLATFORM_DEFAULT_CLOSE_FDS:
678 close_fds = True
679 if pass_fds and not close_fds:
680 warnings.warn("pass_fds overriding close_fds.", RuntimeWarning)
681 close_fds = True
Tim Peterse8374a52004-10-13 03:15:00 +0000682 if startupinfo is not None:
683 raise ValueError("startupinfo is only supported on Windows "
684 "platforms")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000685 if creationflags != 0:
Tim Peterse8374a52004-10-13 03:15:00 +0000686 raise ValueError("creationflags is only supported on Windows "
687 "platforms")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000688
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400689 self.args = args
Tim Peterse718f612004-10-12 21:51:32 +0000690 self.stdin = None
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000691 self.stdout = None
692 self.stderr = None
693 self.pid = None
694 self.returncode = None
695 self.universal_newlines = universal_newlines
696
697 # Input and output objects. The general principle is like
698 # this:
699 #
700 # Parent Child
701 # ------ -----
702 # p2cwrite ---stdin---> p2cread
703 # c2pread <--stdout--- c2pwrite
704 # errread <--stderr--- errwrite
705 #
706 # On POSIX, the child objects are file descriptors. On
707 # Windows, these are Windows file handles. The parent objects
708 # are file descriptors on both platforms. The parent objects
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000709 # are -1 when not using PIPEs. The child objects are -1
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000710 # when not redirecting.
Tim Peterse718f612004-10-12 21:51:32 +0000711
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000712 (p2cread, p2cwrite,
713 c2pread, c2pwrite,
714 errread, errwrite) = self._get_handles(stdin, stdout, stderr)
715
Antoine Pitrouc9982322011-01-04 19:07:07 +0000716 # We wrap OS handles *before* launching the child, otherwise a
717 # quickly terminating child could make our fds unwrappable
718 # (see #8458).
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000719
Thomas Wouterscf297e42007-02-23 15:07:44 +0000720 if mswindows:
Florent Xicluna3b8bfef2010-03-14 12:31:06 +0000721 if p2cwrite != -1:
Hirokazu Yamamoto0c988172009-03-03 22:41:26 +0000722 p2cwrite = msvcrt.open_osfhandle(p2cwrite.Detach(), 0)
Florent Xicluna3b8bfef2010-03-14 12:31:06 +0000723 if c2pread != -1:
Hirokazu Yamamoto0c988172009-03-03 22:41:26 +0000724 c2pread = msvcrt.open_osfhandle(c2pread.Detach(), 0)
Florent Xicluna3b8bfef2010-03-14 12:31:06 +0000725 if errread != -1:
Hirokazu Yamamoto0c988172009-03-03 22:41:26 +0000726 errread = msvcrt.open_osfhandle(errread.Detach(), 0)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000727
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000728 if p2cwrite != -1:
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000729 self.stdin = io.open(p2cwrite, 'wb', bufsize)
730 if self.universal_newlines:
731 self.stdin = io.TextIOWrapper(self.stdin)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000732 if c2pread != -1:
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000733 self.stdout = io.open(c2pread, 'rb', bufsize)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000734 if universal_newlines:
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000735 self.stdout = io.TextIOWrapper(self.stdout)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000736 if errread != -1:
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000737 self.stderr = io.open(errread, 'rb', bufsize)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000738 if universal_newlines:
Guido van Rossumfa0054a2007-05-24 04:05:35 +0000739 self.stderr = io.TextIOWrapper(self.stderr)
Tim Peterse718f612004-10-12 21:51:32 +0000740
Antoine Pitrouc9982322011-01-04 19:07:07 +0000741 try:
742 self._execute_child(args, executable, preexec_fn, close_fds,
743 pass_fds, cwd, env, universal_newlines,
744 startupinfo, creationflags, shell,
745 p2cread, p2cwrite,
746 c2pread, c2pwrite,
747 errread, errwrite,
748 restore_signals, start_new_session)
749 except:
750 # Cleanup if the child failed starting
751 for f in filter(None, [self.stdin, self.stdout, self.stderr]):
752 try:
753 f.close()
754 except EnvironmentError:
755 # Ignore EBADF or other errors
756 pass
757 raise
758
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000759
Guido van Rossum98297ee2007-11-06 21:34:58 +0000760 def _translate_newlines(self, data, encoding):
761 data = data.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
762 return data.decode(encoding)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000763
Brian Curtin79cdb662010-12-03 02:46:02 +0000764 def __enter__(self):
765 return self
766
767 def __exit__(self, type, value, traceback):
768 if self.stdout:
769 self.stdout.close()
770 if self.stderr:
771 self.stderr.close()
772 if self.stdin:
773 self.stdin.close()
Gregory P. Smith6b657452011-05-11 21:42:08 -0700774 # Wait for the process to terminate, to avoid zombies.
775 self.wait()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000776
Brett Cannon84df1e62010-05-14 00:33:40 +0000777 def __del__(self, _maxsize=sys.maxsize, _active=_active):
Victor Stinner87b9bc32011-06-01 00:57:47 +0200778 # If __init__ hasn't had a chance to execute (e.g. if it
779 # was passed an undeclared keyword argument), we don't
780 # have a _child_created attribute at all.
781 if not getattr(self, '_child_created', False):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000782 # We didn't get to successfully create a child process.
783 return
784 # In case the child hasn't been waited on, check if it's done.
Brett Cannon84df1e62010-05-14 00:33:40 +0000785 self._internal_poll(_deadstate=_maxsize)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000786 if self.returncode is None and _active is not None:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000787 # Child is still running, keep us alive until we can wait on it.
788 _active.append(self)
789
Ross Lagerwallba102ec2011-03-16 18:40:25 +0200790 def _get_devnull(self):
791 if not hasattr(self, '_devnull'):
792 self._devnull = os.open(os.devnull, os.O_RDWR)
793 return self._devnull
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000794
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400795 def communicate(self, input=None, timeout=None):
Peter Astrand23109f02005-03-03 20:28:59 +0000796 """Interact with process: Send data to stdin. Read data from
797 stdout and stderr, until end-of-file is reached. Wait for
Gregory P. Smitha454ef62011-05-22 22:29:49 -0700798 process to terminate. The optional input argument should be
799 bytes to be sent to the child process, or None, if no data
Peter Astrand23109f02005-03-03 20:28:59 +0000800 should be sent to the child.
Tim Peterseba28be2005-03-28 01:08:02 +0000801
Peter Astrand23109f02005-03-03 20:28:59 +0000802 communicate() returns a tuple (stdout, stderr)."""
803
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400804 if self._communication_started and input:
805 raise ValueError("Cannot send input after starting communication")
806
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400807 # Optimization: If we are not worried about timeouts, we haven't
808 # started communicating, and we have one or zero pipes, using select()
809 # or threads is unnecessary.
Victor Stinner7a8d0812011-04-05 13:13:08 +0200810 if (timeout is None and not self._communication_started and
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400811 [self.stdin, self.stdout, self.stderr].count(None) >= 2):
Tim Peterseba28be2005-03-28 01:08:02 +0000812 stdout = None
813 stderr = None
Peter Astrand23109f02005-03-03 20:28:59 +0000814 if self.stdin:
815 if input:
Ross Lagerwall4f61b022011-04-05 15:34:00 +0200816 try:
817 self.stdin.write(input)
818 except IOError as e:
819 if e.errno != errno.EPIPE and e.errno != errno.EINVAL:
820 raise
Peter Astrand23109f02005-03-03 20:28:59 +0000821 self.stdin.close()
822 elif self.stdout:
823 stdout = self.stdout.read()
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000824 self.stdout.close()
Peter Astrand23109f02005-03-03 20:28:59 +0000825 elif self.stderr:
826 stderr = self.stderr.read()
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000827 self.stderr.close()
Peter Astrand23109f02005-03-03 20:28:59 +0000828 self.wait()
Victor Stinner7a8d0812011-04-05 13:13:08 +0200829 else:
830 if timeout is not None:
831 endtime = time.time() + timeout
832 else:
833 endtime = None
Tim Peterseba28be2005-03-28 01:08:02 +0000834
Victor Stinner7a8d0812011-04-05 13:13:08 +0200835 try:
836 stdout, stderr = self._communicate(input, endtime, timeout)
837 finally:
838 self._communication_started = True
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400839
Victor Stinner7a8d0812011-04-05 13:13:08 +0200840 sts = self.wait(timeout=self._remaining_time(endtime))
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400841
842 return (stdout, stderr)
Peter Astrand23109f02005-03-03 20:28:59 +0000843
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000844
Georg Brandl6aa2d1f2008-08-12 08:35:52 +0000845 def poll(self):
846 return self._internal_poll()
847
848
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400849 def _remaining_time(self, endtime):
850 """Convenience for _communicate when computing timeouts."""
851 if endtime is None:
852 return None
853 else:
854 return endtime - time.time()
855
856
Reid Kleckner2b228f02011-03-16 16:57:54 -0400857 def _check_timeout(self, endtime, orig_timeout):
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400858 """Convenience for checking if a timeout has expired."""
859 if endtime is None:
860 return
861 if time.time() > endtime:
Reid Kleckner2b228f02011-03-16 16:57:54 -0400862 raise TimeoutExpired(self.args, orig_timeout)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -0400863
864
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000865 if mswindows:
866 #
867 # Windows methods
868 #
869 def _get_handles(self, stdin, stdout, stderr):
Alexandre Vassalotti711ed4a2009-07-17 10:42:05 +0000870 """Construct and return tuple with IO objects:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000871 p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
872 """
Peter Astrandd38ddf42005-02-10 08:32:50 +0000873 if stdin is None and stdout is None and stderr is None:
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000874 return (-1, -1, -1, -1, -1, -1)
Tim Peterse718f612004-10-12 21:51:32 +0000875
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000876 p2cread, p2cwrite = -1, -1
877 c2pread, c2pwrite = -1, -1
878 errread, errwrite = -1, -1
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000879
Peter Astrandd38ddf42005-02-10 08:32:50 +0000880 if stdin is None:
Brian Curtin1ce6b582010-04-24 16:19:22 +0000881 p2cread = _subprocess.GetStdHandle(_subprocess.STD_INPUT_HANDLE)
Hirokazu Yamamoto0c988172009-03-03 22:41:26 +0000882 if p2cread is None:
Brian Curtin1ce6b582010-04-24 16:19:22 +0000883 p2cread, _ = _subprocess.CreatePipe(None, 0)
Hirokazu Yamamoto0c988172009-03-03 22:41:26 +0000884 elif stdin == PIPE:
Brian Curtin1ce6b582010-04-24 16:19:22 +0000885 p2cread, p2cwrite = _subprocess.CreatePipe(None, 0)
Ross Lagerwallba102ec2011-03-16 18:40:25 +0200886 elif stdin == DEVNULL:
887 p2cread = msvcrt.get_osfhandle(self._get_devnull())
Peter Astrandd38ddf42005-02-10 08:32:50 +0000888 elif isinstance(stdin, int):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000889 p2cread = msvcrt.get_osfhandle(stdin)
890 else:
891 # Assuming file-like object
892 p2cread = msvcrt.get_osfhandle(stdin.fileno())
893 p2cread = self._make_inheritable(p2cread)
894
Peter Astrandd38ddf42005-02-10 08:32:50 +0000895 if stdout is None:
Brian Curtin1ce6b582010-04-24 16:19:22 +0000896 c2pwrite = _subprocess.GetStdHandle(_subprocess.STD_OUTPUT_HANDLE)
Hirokazu Yamamoto0c988172009-03-03 22:41:26 +0000897 if c2pwrite is None:
Brian Curtin1ce6b582010-04-24 16:19:22 +0000898 _, c2pwrite = _subprocess.CreatePipe(None, 0)
Hirokazu Yamamoto0c988172009-03-03 22:41:26 +0000899 elif stdout == PIPE:
Brian Curtin1ce6b582010-04-24 16:19:22 +0000900 c2pread, c2pwrite = _subprocess.CreatePipe(None, 0)
Ross Lagerwallba102ec2011-03-16 18:40:25 +0200901 elif stdout == DEVNULL:
902 c2pwrite = msvcrt.get_osfhandle(self._get_devnull())
Peter Astrandd38ddf42005-02-10 08:32:50 +0000903 elif isinstance(stdout, int):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000904 c2pwrite = msvcrt.get_osfhandle(stdout)
905 else:
906 # Assuming file-like object
907 c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
908 c2pwrite = self._make_inheritable(c2pwrite)
909
Peter Astrandd38ddf42005-02-10 08:32:50 +0000910 if stderr is None:
Brian Curtin1ce6b582010-04-24 16:19:22 +0000911 errwrite = _subprocess.GetStdHandle(_subprocess.STD_ERROR_HANDLE)
Hirokazu Yamamoto0c988172009-03-03 22:41:26 +0000912 if errwrite is None:
Brian Curtin1ce6b582010-04-24 16:19:22 +0000913 _, errwrite = _subprocess.CreatePipe(None, 0)
Hirokazu Yamamoto0c988172009-03-03 22:41:26 +0000914 elif stderr == PIPE:
Brian Curtin1ce6b582010-04-24 16:19:22 +0000915 errread, errwrite = _subprocess.CreatePipe(None, 0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000916 elif stderr == STDOUT:
917 errwrite = c2pwrite
Ross Lagerwallba102ec2011-03-16 18:40:25 +0200918 elif stderr == DEVNULL:
919 errwrite = msvcrt.get_osfhandle(self._get_devnull())
Peter Astrandd38ddf42005-02-10 08:32:50 +0000920 elif isinstance(stderr, int):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000921 errwrite = msvcrt.get_osfhandle(stderr)
922 else:
923 # Assuming file-like object
924 errwrite = msvcrt.get_osfhandle(stderr.fileno())
925 errwrite = self._make_inheritable(errwrite)
926
927 return (p2cread, p2cwrite,
928 c2pread, c2pwrite,
929 errread, errwrite)
930
931
932 def _make_inheritable(self, handle):
933 """Return a duplicate of handle, which is inheritable"""
Brian Curtin1ce6b582010-04-24 16:19:22 +0000934 return _subprocess.DuplicateHandle(_subprocess.GetCurrentProcess(),
935 handle, _subprocess.GetCurrentProcess(), 0, 1,
936 _subprocess.DUPLICATE_SAME_ACCESS)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000937
938
939 def _find_w9xpopen(self):
940 """Find and return absolut path to w9xpopen.exe"""
Brian Curtin1ce6b582010-04-24 16:19:22 +0000941 w9xpopen = os.path.join(
942 os.path.dirname(_subprocess.GetModuleFileName(0)),
Tim Peterse8374a52004-10-13 03:15:00 +0000943 "w9xpopen.exe")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000944 if not os.path.exists(w9xpopen):
945 # Eeek - file-not-found - possibly an embedding
946 # situation - see if we can locate it in sys.exec_prefix
Tim Peterse8374a52004-10-13 03:15:00 +0000947 w9xpopen = os.path.join(os.path.dirname(sys.exec_prefix),
948 "w9xpopen.exe")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000949 if not os.path.exists(w9xpopen):
Tim Peterse8374a52004-10-13 03:15:00 +0000950 raise RuntimeError("Cannot locate w9xpopen.exe, which is "
951 "needed for Popen to work with your "
952 "shell or platform.")
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000953 return w9xpopen
954
Tim Peterse718f612004-10-12 21:51:32 +0000955
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000956 def _execute_child(self, args, executable, preexec_fn, close_fds,
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000957 pass_fds, cwd, env, universal_newlines,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000958 startupinfo, creationflags, shell,
959 p2cread, p2cwrite,
960 c2pread, c2pwrite,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000961 errread, errwrite,
962 unused_restore_signals, unused_start_new_session):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000963 """Execute program (MS Windows version)"""
964
Gregory P. Smith8edd99d2010-12-14 13:43:30 +0000965 assert not pass_fds, "pass_fds not supported on Windows."
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000966
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000967 if not isinstance(args, str):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000968 args = list2cmdline(args)
969
Peter Astrandc1d65362004-11-07 14:30:34 +0000970 # Process startup details
Peter Astrandd38ddf42005-02-10 08:32:50 +0000971 if startupinfo is None:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000972 startupinfo = STARTUPINFO()
Victor Stinnerb3693582010-05-21 20:13:12 +0000973 if -1 not in (p2cread, c2pwrite, errwrite):
Brian Curtin1ce6b582010-04-24 16:19:22 +0000974 startupinfo.dwFlags |= _subprocess.STARTF_USESTDHANDLES
Peter Astrandc1d65362004-11-07 14:30:34 +0000975 startupinfo.hStdInput = p2cread
976 startupinfo.hStdOutput = c2pwrite
977 startupinfo.hStdError = errwrite
978
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000979 if shell:
Brian Curtin1ce6b582010-04-24 16:19:22 +0000980 startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
981 startupinfo.wShowWindow = _subprocess.SW_HIDE
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000982 comspec = os.environ.get("COMSPEC", "cmd.exe")
Tim Golden126c2962010-08-11 14:20:40 +0000983 args = '{} /c "{}"'.format (comspec, args)
Brian Curtin1ce6b582010-04-24 16:19:22 +0000984 if (_subprocess.GetVersion() >= 0x80000000 or
Tim Peterse8374a52004-10-13 03:15:00 +0000985 os.path.basename(comspec).lower() == "command.com"):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000986 # Win9x, or using command.com on NT. We need to
987 # use the w9xpopen intermediate program. For more
988 # information, see KB Q150956
989 # (http://web.archive.org/web/20011105084002/http://support.microsoft.com/support/kb/articles/Q150/9/56.asp)
990 w9xpopen = self._find_w9xpopen()
991 args = '"%s" %s' % (w9xpopen, args)
992 # Not passing CREATE_NEW_CONSOLE has been known to
993 # cause random failures on win9x. Specifically a
994 # dialog: "Your program accessed mem currently in
995 # use at xxx" and a hopeful warning about the
Mark Dickinson934896d2009-02-21 20:59:32 +0000996 # stability of your system. Cost is Ctrl+C won't
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000997 # kill children.
Brian Curtin1ce6b582010-04-24 16:19:22 +0000998 creationflags |= _subprocess.CREATE_NEW_CONSOLE
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000999
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001000 # Start the process
1001 try:
Brian Curtin1ce6b582010-04-24 16:19:22 +00001002 hp, ht, pid, tid = _subprocess.CreateProcess(executable, args,
Tim Peterse8374a52004-10-13 03:15:00 +00001003 # no special security
1004 None, None,
Guido van Rossume7ba4952007-06-06 23:52:48 +00001005 int(not close_fds),
Tim Peterse8374a52004-10-13 03:15:00 +00001006 creationflags,
1007 env,
1008 cwd,
1009 startupinfo)
Guido van Rossumb940e112007-01-10 16:19:56 +00001010 except pywintypes.error as e:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001011 # Translate pywintypes.error to WindowsError, which is
1012 # a subclass of OSError. FIXME: We should really
Ezio Melotti13925002011-03-16 11:05:33 +02001013 # translate errno using _sys_errlist (or similar), but
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001014 # how can this be done from Python?
1015 raise WindowsError(*e.args)
Tim Goldenad537f22010-08-08 11:18:16 +00001016 finally:
1017 # Child is launched. Close the parent's copy of those pipe
1018 # handles that only the child should have open. You need
1019 # to make sure that no handles to the write end of the
1020 # output pipe are maintained in this process or else the
1021 # pipe will not close when the child process exits and the
1022 # ReadFile will hang.
1023 if p2cread != -1:
1024 p2cread.Close()
1025 if c2pwrite != -1:
1026 c2pwrite.Close()
1027 if errwrite != -1:
1028 errwrite.Close()
Ross Lagerwallba102ec2011-03-16 18:40:25 +02001029 if hasattr(self, '_devnull'):
1030 os.close(self._devnull)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001031
1032 # Retain the process handle, but close the thread handle
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001033 self._child_created = True
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001034 self._handle = hp
1035 self.pid = pid
1036 ht.Close()
1037
Brett Cannon84df1e62010-05-14 00:33:40 +00001038 def _internal_poll(self, _deadstate=None,
Victor Stinnerc807a612010-05-14 21:53:45 +00001039 _WaitForSingleObject=_subprocess.WaitForSingleObject,
1040 _WAIT_OBJECT_0=_subprocess.WAIT_OBJECT_0,
1041 _GetExitCodeProcess=_subprocess.GetExitCodeProcess):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001042 """Check if child process has terminated. Returns returncode
Brett Cannon84df1e62010-05-14 00:33:40 +00001043 attribute.
1044
1045 This method is called by __del__, so it can only refer to objects
1046 in its local scope.
1047
1048 """
Peter Astrandd38ddf42005-02-10 08:32:50 +00001049 if self.returncode is None:
Brett Cannon84df1e62010-05-14 00:33:40 +00001050 if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
1051 self.returncode = _GetExitCodeProcess(self._handle)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001052 return self.returncode
1053
1054
Reid Kleckner2b228f02011-03-16 16:57:54 -04001055 def wait(self, timeout=None, endtime=None):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001056 """Wait for child process to terminate. Returns returncode
1057 attribute."""
Reid Kleckner2b228f02011-03-16 16:57:54 -04001058 if endtime is not None:
1059 timeout = self._remaining_time(endtime)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001060 if timeout is None:
Reid Kleckner91156ff2011-03-21 10:06:10 -07001061 timeout_millis = _subprocess.INFINITE
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001062 else:
Reid Kleckner91156ff2011-03-21 10:06:10 -07001063 timeout_millis = int(timeout * 1000)
Peter Astrandd38ddf42005-02-10 08:32:50 +00001064 if self.returncode is None:
Reid Kleckner91156ff2011-03-21 10:06:10 -07001065 result = _subprocess.WaitForSingleObject(self._handle,
1066 timeout_millis)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001067 if result == _subprocess.WAIT_TIMEOUT:
Reid Kleckner2b228f02011-03-16 16:57:54 -04001068 raise TimeoutExpired(self.args, timeout)
Brian Curtin1ce6b582010-04-24 16:19:22 +00001069 self.returncode = _subprocess.GetExitCodeProcess(self._handle)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001070 return self.returncode
1071
1072
1073 def _readerthread(self, fh, buffer):
1074 buffer.append(fh.read())
Victor Stinner667d4b52010-12-25 22:40:32 +00001075 fh.close()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001076
1077
Reid Kleckner2b228f02011-03-16 16:57:54 -04001078 def _communicate(self, input, endtime, orig_timeout):
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001079 # Start reader threads feeding into a list hanging off of this
1080 # object, unless they've already been started.
1081 if self.stdout and not hasattr(self, "_stdout_buff"):
1082 self._stdout_buff = []
1083 self.stdout_thread = \
1084 threading.Thread(target=self._readerthread,
1085 args=(self.stdout, self._stdout_buff))
1086 self.stdout_thread.daemon = True
1087 self.stdout_thread.start()
1088 if self.stderr and not hasattr(self, "_stderr_buff"):
1089 self._stderr_buff = []
1090 self.stderr_thread = \
1091 threading.Thread(target=self._readerthread,
1092 args=(self.stderr, self._stderr_buff))
1093 self.stderr_thread.daemon = True
1094 self.stderr_thread.start()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001095
1096 if self.stdin:
Peter Astrandd38ddf42005-02-10 08:32:50 +00001097 if input is not None:
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001098 try:
1099 self.stdin.write(input)
1100 except IOError as e:
1101 if e.errno != errno.EPIPE:
1102 raise
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001103 self.stdin.close()
1104
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001105 # Wait for the reader threads, or time out. If we time out, the
1106 # threads remain reading and the fds left open in case the user
1107 # calls communicate again.
1108 if self.stdout is not None:
1109 self.stdout_thread.join(self._remaining_time(endtime))
1110 if self.stdout_thread.isAlive():
Reid Kleckner9a67e6c2011-03-20 08:28:07 -07001111 raise TimeoutExpired(self.args, orig_timeout)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001112 if self.stderr is not None:
1113 self.stderr_thread.join(self._remaining_time(endtime))
1114 if self.stderr_thread.isAlive():
Reid Kleckner9a67e6c2011-03-20 08:28:07 -07001115 raise TimeoutExpired(self.args, orig_timeout)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001116
1117 # Collect the output from and close both pipes, now that we know
1118 # both have been read successfully.
1119 stdout = None
1120 stderr = None
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001121 if self.stdout:
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001122 stdout = self._stdout_buff
1123 self.stdout.close()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001124 if self.stderr:
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001125 stderr = self._stderr_buff
1126 self.stderr.close()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001127
1128 # All data exchanged. Translate lists into strings.
Peter Astrandd38ddf42005-02-10 08:32:50 +00001129 if stdout is not None:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001130 stdout = stdout[0]
Peter Astrandd38ddf42005-02-10 08:32:50 +00001131 if stderr is not None:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001132 stderr = stderr[0]
1133
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001134 return (stdout, stderr)
1135
Christian Heimesa342c012008-04-20 21:01:16 +00001136 def send_signal(self, sig):
1137 """Send a signal to the process
1138 """
1139 if sig == signal.SIGTERM:
1140 self.terminate()
Brian Curtineb24d742010-04-12 17:16:38 +00001141 elif sig == signal.CTRL_C_EVENT:
1142 os.kill(self.pid, signal.CTRL_C_EVENT)
1143 elif sig == signal.CTRL_BREAK_EVENT:
1144 os.kill(self.pid, signal.CTRL_BREAK_EVENT)
Christian Heimesa342c012008-04-20 21:01:16 +00001145 else:
Brian Curtin19651362010-09-07 13:24:38 +00001146 raise ValueError("Unsupported signal: {}".format(sig))
Christian Heimesa342c012008-04-20 21:01:16 +00001147
1148 def terminate(self):
1149 """Terminates the process
1150 """
Brian Curtin1ce6b582010-04-24 16:19:22 +00001151 _subprocess.TerminateProcess(self._handle, 1)
Christian Heimesa342c012008-04-20 21:01:16 +00001152
1153 kill = terminate
1154
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001155 else:
1156 #
1157 # POSIX methods
1158 #
1159 def _get_handles(self, stdin, stdout, stderr):
Alexandre Vassalotti711ed4a2009-07-17 10:42:05 +00001160 """Construct and return tuple with IO objects:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001161 p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
1162 """
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001163 p2cread, p2cwrite = -1, -1
1164 c2pread, c2pwrite = -1, -1
1165 errread, errwrite = -1, -1
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001166
Peter Astrandd38ddf42005-02-10 08:32:50 +00001167 if stdin is None:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001168 pass
1169 elif stdin == PIPE:
Gregory P. Smith51ee2702010-12-13 07:59:39 +00001170 p2cread, p2cwrite = _create_pipe()
Ross Lagerwallba102ec2011-03-16 18:40:25 +02001171 elif stdin == DEVNULL:
1172 p2cread = self._get_devnull()
Peter Astrandd38ddf42005-02-10 08:32:50 +00001173 elif isinstance(stdin, int):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001174 p2cread = stdin
1175 else:
1176 # Assuming file-like object
1177 p2cread = stdin.fileno()
1178
Peter Astrandd38ddf42005-02-10 08:32:50 +00001179 if stdout is None:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001180 pass
1181 elif stdout == PIPE:
Gregory P. Smith51ee2702010-12-13 07:59:39 +00001182 c2pread, c2pwrite = _create_pipe()
Ross Lagerwallba102ec2011-03-16 18:40:25 +02001183 elif stdout == DEVNULL:
1184 c2pwrite = self._get_devnull()
Peter Astrandd38ddf42005-02-10 08:32:50 +00001185 elif isinstance(stdout, int):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001186 c2pwrite = stdout
1187 else:
1188 # Assuming file-like object
Tim Peterse718f612004-10-12 21:51:32 +00001189 c2pwrite = stdout.fileno()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001190
Peter Astrandd38ddf42005-02-10 08:32:50 +00001191 if stderr is None:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001192 pass
1193 elif stderr == PIPE:
Gregory P. Smith51ee2702010-12-13 07:59:39 +00001194 errread, errwrite = _create_pipe()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001195 elif stderr == STDOUT:
1196 errwrite = c2pwrite
Ross Lagerwallba102ec2011-03-16 18:40:25 +02001197 elif stderr == DEVNULL:
1198 errwrite = self._get_devnull()
Peter Astrandd38ddf42005-02-10 08:32:50 +00001199 elif isinstance(stderr, int):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001200 errwrite = stderr
1201 else:
1202 # Assuming file-like object
1203 errwrite = stderr.fileno()
1204
1205 return (p2cread, p2cwrite,
1206 c2pread, c2pwrite,
1207 errread, errwrite)
1208
1209
Antoine Pitrou47f14ba2011-01-03 23:42:01 +00001210 def _close_fds(self, fds_to_keep):
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +00001211 start_fd = 3
Antoine Pitrou47f14ba2011-01-03 23:42:01 +00001212 for fd in sorted(fds_to_keep):
Gregory P. Smith8edd99d2010-12-14 13:43:30 +00001213 if fd >= start_fd:
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +00001214 os.closerange(start_fd, fd)
1215 start_fd = fd + 1
1216 if start_fd <= MAXFD:
1217 os.closerange(start_fd, MAXFD)
1218
1219
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001220 def _execute_child(self, args, executable, preexec_fn, close_fds,
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +00001221 pass_fds, cwd, env, universal_newlines,
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001222 startupinfo, creationflags, shell,
1223 p2cread, p2cwrite,
1224 c2pread, c2pwrite,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001225 errread, errwrite,
1226 restore_signals, start_new_session):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001227 """Execute program (POSIX version)"""
1228
Victor Stinner7b3b20a2011-03-03 12:54:05 +00001229 if isinstance(args, (str, bytes)):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001230 args = [args]
Thomas Wouters89f507f2006-12-13 04:49:30 +00001231 else:
1232 args = list(args)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001233
1234 if shell:
1235 args = ["/bin/sh", "-c"] + args
Stefan Krah9542cc62010-07-19 14:20:53 +00001236 if executable:
1237 args[0] = executable
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001238
Peter Astrandd38ddf42005-02-10 08:32:50 +00001239 if executable is None:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001240 executable = args[0]
1241
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001242 # For transferring possible exec failure from child to parent.
1243 # Data format: "exception name:hex errno:description"
1244 # Pickle is not used; it is complex and involves memory allocation.
Gregory P. Smith51ee2702010-12-13 07:59:39 +00001245 errpipe_read, errpipe_write = _create_pipe()
Christian Heimesfdab48e2008-01-20 09:06:41 +00001246 try:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001247 try:
Gregory P. Smith59fd1bf2011-05-28 09:32:39 -07001248 # We must avoid complex work that could involve
1249 # malloc or free in the child process to avoid
1250 # potential deadlocks, thus we do all this here.
1251 # and pass it to fork_exec()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001252
Gregory P. Smith59fd1bf2011-05-28 09:32:39 -07001253 if env:
1254 env_list = [os.fsencode(k) + b'=' + os.fsencode(v)
1255 for k, v in env.items()]
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001256 else:
Gregory P. Smith59fd1bf2011-05-28 09:32:39 -07001257 env_list = None # Use execv instead of execve.
1258 executable = os.fsencode(executable)
1259 if os.path.dirname(executable):
1260 executable_list = (executable,)
1261 else:
1262 # This matches the behavior of os._execvpe().
1263 executable_list = tuple(
1264 os.path.join(os.fsencode(dir), executable)
1265 for dir in os.get_exec_path(env))
1266 fds_to_keep = set(pass_fds)
1267 fds_to_keep.add(errpipe_write)
1268 self.pid = _posixsubprocess.fork_exec(
1269 args, executable_list,
1270 close_fds, sorted(fds_to_keep), cwd, env_list,
1271 p2cread, p2cwrite, c2pread, c2pwrite,
1272 errread, errwrite,
1273 errpipe_read, errpipe_write,
1274 restore_signals, start_new_session, preexec_fn)
Facundo Batista10706e22009-06-19 20:34:30 +00001275 finally:
1276 # be sure the FD is closed no matter what
1277 os.close(errpipe_write)
1278
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001279 if p2cread != -1 and p2cwrite != -1:
Facundo Batista10706e22009-06-19 20:34:30 +00001280 os.close(p2cread)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001281 if c2pwrite != -1 and c2pread != -1:
Facundo Batista10706e22009-06-19 20:34:30 +00001282 os.close(c2pwrite)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001283 if errwrite != -1 and errread != -1:
Facundo Batista10706e22009-06-19 20:34:30 +00001284 os.close(errwrite)
Ross Lagerwallba102ec2011-03-16 18:40:25 +02001285 if hasattr(self, '_devnull'):
1286 os.close(self._devnull)
Facundo Batista10706e22009-06-19 20:34:30 +00001287
1288 # Wait for exec to fail or succeed; possibly raising an
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001289 # exception (limited in size)
1290 data = bytearray()
1291 while True:
1292 part = _eintr_retry_call(os.read, errpipe_read, 50000)
1293 data += part
1294 if not part or len(data) > 50000:
1295 break
Facundo Batista10706e22009-06-19 20:34:30 +00001296 finally:
1297 # be sure the FD is closed no matter what
1298 os.close(errpipe_read)
1299
Guido van Rossumaf2362a2007-05-15 22:32:02 +00001300 if data:
Gregory P. Smithe85db2b2010-12-14 14:38:00 +00001301 try:
1302 _eintr_retry_call(os.waitpid, self.pid, 0)
1303 except OSError as e:
1304 if e.errno != errno.ECHILD:
1305 raise
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001306 try:
1307 exception_name, hex_errno, err_msg = data.split(b':', 2)
1308 except ValueError:
1309 print('Bad exception data:', repr(data))
1310 exception_name = b'RuntimeError'
1311 hex_errno = b'0'
1312 err_msg = b'Unknown'
1313 child_exception_type = getattr(
1314 builtins, exception_name.decode('ascii'),
1315 RuntimeError)
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001316 for fd in (p2cwrite, c2pread, errread):
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001317 if fd != -1:
Benjamin Petersond75fcb42009-02-19 04:22:03 +00001318 os.close(fd)
Victor Stinner4d078042010-04-23 19:28:32 +00001319 err_msg = err_msg.decode(errors="surrogatepass")
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001320 if issubclass(child_exception_type, OSError) and hex_errno:
Benjamin Petersonb8bc4392010-11-20 18:24:54 +00001321 errno_num = int(hex_errno, 16)
1322 if errno_num != 0:
1323 err_msg = os.strerror(errno_num)
1324 if errno_num == errno.ENOENT:
Benjamin Peterson5f780402010-11-20 18:07:52 +00001325 err_msg += ': ' + repr(args[0])
Benjamin Petersonb8bc4392010-11-20 18:24:54 +00001326 raise child_exception_type(errno_num, err_msg)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001327 raise child_exception_type(err_msg)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001328
1329
Brett Cannon84df1e62010-05-14 00:33:40 +00001330 def _handle_exitstatus(self, sts, _WIFSIGNALED=os.WIFSIGNALED,
1331 _WTERMSIG=os.WTERMSIG, _WIFEXITED=os.WIFEXITED,
1332 _WEXITSTATUS=os.WEXITSTATUS):
1333 # This method is called (indirectly) by __del__, so it cannot
1334 # refer to anything outside of its local scope."""
1335 if _WIFSIGNALED(sts):
1336 self.returncode = -_WTERMSIG(sts)
1337 elif _WIFEXITED(sts):
1338 self.returncode = _WEXITSTATUS(sts)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001339 else:
1340 # Should never happen
1341 raise RuntimeError("Unknown child exit status!")
1342
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001343
Brett Cannon84df1e62010-05-14 00:33:40 +00001344 def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid,
1345 _WNOHANG=os.WNOHANG, _os_error=os.error):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001346 """Check if child process has terminated. Returns returncode
Brett Cannon84df1e62010-05-14 00:33:40 +00001347 attribute.
1348
1349 This method is called by __del__, so it cannot reference anything
1350 outside of the local scope (nor can any methods it calls).
1351
1352 """
Peter Astrandd38ddf42005-02-10 08:32:50 +00001353 if self.returncode is None:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001354 try:
Brett Cannon84df1e62010-05-14 00:33:40 +00001355 pid, sts = _waitpid(self.pid, _WNOHANG)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001356 if pid == self.pid:
1357 self._handle_exitstatus(sts)
Brett Cannon84df1e62010-05-14 00:33:40 +00001358 except _os_error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001359 if _deadstate is not None:
1360 self.returncode = _deadstate
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001361 return self.returncode
1362
1363
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001364 def _try_wait(self, wait_flags):
1365 try:
1366 (pid, sts) = _eintr_retry_call(os.waitpid, self.pid, wait_flags)
1367 except OSError as e:
1368 if e.errno != errno.ECHILD:
1369 raise
1370 # This happens if SIGCLD is set to be ignored or waiting
1371 # for child processes has otherwise been disabled for our
1372 # process. This child is dead, we can't get the status.
1373 pid = self.pid
1374 sts = 0
1375 return (pid, sts)
1376
1377
1378 def wait(self, timeout=None, endtime=None):
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001379 """Wait for child process to terminate. Returns returncode
1380 attribute."""
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001381 if self.returncode is not None:
1382 return self.returncode
Reid Kleckner2b228f02011-03-16 16:57:54 -04001383
1384 # endtime is preferred to timeout. timeout is only used for
1385 # printing.
1386 if endtime is not None or timeout is not None:
1387 if endtime is None:
1388 endtime = time.time() + timeout
1389 elif timeout is None:
1390 timeout = self._remaining_time(endtime)
1391
1392 if endtime is not None:
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001393 # Enter a busy loop if we have a timeout. This busy loop was
1394 # cribbed from Lib/threading.py in Thread.wait() at r71065.
1395 delay = 0.0005 # 500 us -> initial delay of 1 ms
1396 while True:
1397 (pid, sts) = self._try_wait(os.WNOHANG)
1398 assert pid == self.pid or pid == 0
1399 if pid == self.pid:
1400 self._handle_exitstatus(sts)
1401 break
1402 remaining = self._remaining_time(endtime)
1403 if remaining <= 0:
Reid Kleckner2b228f02011-03-16 16:57:54 -04001404 raise TimeoutExpired(self.args, timeout)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001405 delay = min(delay * 2, remaining, .05)
1406 time.sleep(delay)
1407 elif self.returncode is None:
1408 (pid, sts) = self._try_wait(0)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001409 self._handle_exitstatus(sts)
1410 return self.returncode
1411
1412
Reid Kleckner2b228f02011-03-16 16:57:54 -04001413 def _communicate(self, input, endtime, orig_timeout):
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001414 if self.stdin and not self._communication_started:
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001415 # Flush stdio buffer. This might block, if the user has
1416 # been writing to .stdin in an uncontrolled fashion.
1417 self.stdin.flush()
1418 if not input:
1419 self.stdin.close()
1420
1421 if _has_poll:
Reid Kleckner2b228f02011-03-16 16:57:54 -04001422 stdout, stderr = self._communicate_with_poll(input, endtime,
1423 orig_timeout)
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001424 else:
Reid Kleckner2b228f02011-03-16 16:57:54 -04001425 stdout, stderr = self._communicate_with_select(input, endtime,
1426 orig_timeout)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001427
1428 self.wait(timeout=self._remaining_time(endtime))
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001429
1430 # All data exchanged. Translate lists into strings.
1431 if stdout is not None:
1432 stdout = b''.join(stdout)
1433 if stderr is not None:
1434 stderr = b''.join(stderr)
1435
1436 # Translate newlines, if requested.
1437 # This also turns bytes into strings.
1438 if self.universal_newlines:
1439 if stdout is not None:
1440 stdout = self._translate_newlines(stdout,
1441 self.stdout.encoding)
1442 if stderr is not None:
1443 stderr = self._translate_newlines(stderr,
1444 self.stderr.encoding)
1445
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001446 return (stdout, stderr)
1447
1448
Reid Kleckner2b228f02011-03-16 16:57:54 -04001449 def _communicate_with_poll(self, input, endtime, orig_timeout):
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001450 stdout = None # Return
1451 stderr = None # Return
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001452
1453 if not self._communication_started:
1454 self._fd2file = {}
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001455
1456 poller = select.poll()
1457 def register_and_append(file_obj, eventmask):
1458 poller.register(file_obj.fileno(), eventmask)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001459 self._fd2file[file_obj.fileno()] = file_obj
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001460
1461 def close_unregister_and_remove(fd):
1462 poller.unregister(fd)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001463 self._fd2file[fd].close()
1464 self._fd2file.pop(fd)
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001465
1466 if self.stdin and input:
1467 register_and_append(self.stdin, select.POLLOUT)
1468
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001469 # Only create this mapping if we haven't already.
1470 if not self._communication_started:
1471 self._fd2output = {}
1472 if self.stdout:
1473 self._fd2output[self.stdout.fileno()] = []
1474 if self.stderr:
1475 self._fd2output[self.stderr.fileno()] = []
1476
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001477 select_POLLIN_POLLPRI = select.POLLIN | select.POLLPRI
1478 if self.stdout:
1479 register_and_append(self.stdout, select_POLLIN_POLLPRI)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001480 stdout = self._fd2output[self.stdout.fileno()]
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001481 if self.stderr:
1482 register_and_append(self.stderr, select_POLLIN_POLLPRI)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001483 stderr = self._fd2output[self.stderr.fileno()]
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001484
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001485 # Save the input here so that if we time out while communicating,
1486 # we can continue sending input if we retry.
1487 if self.stdin and self._input is None:
1488 self._input_offset = 0
1489 self._input = input
1490 if self.universal_newlines:
1491 self._input = self._input.encode(self.stdin.encoding)
1492
1493 while self._fd2file:
Victor Stinner7a8d0812011-04-05 13:13:08 +02001494 timeout = self._remaining_time(endtime)
1495 if timeout is not None and timeout < 0:
1496 raise TimeoutExpired(self.args, orig_timeout)
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001497 try:
Victor Stinner7a8d0812011-04-05 13:13:08 +02001498 ready = poller.poll(timeout)
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001499 except select.error as e:
1500 if e.args[0] == errno.EINTR:
1501 continue
1502 raise
Reid Kleckner2b228f02011-03-16 16:57:54 -04001503 self._check_timeout(endtime, orig_timeout)
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001504
1505 # XXX Rewrite these to use non-blocking I/O on the
1506 # file objects; they are no longer using C stdio!
1507
1508 for fd, mode in ready:
1509 if mode & select.POLLOUT:
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001510 chunk = self._input[self._input_offset :
1511 self._input_offset + _PIPE_BUF]
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001512 try:
Ross Lagerwall0b9ea932011-04-05 16:07:49 +02001513 self._input_offset += os.write(fd, chunk)
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001514 except OSError as e:
1515 if e.errno == errno.EPIPE:
1516 close_unregister_and_remove(fd)
1517 else:
1518 raise
1519 else:
Ross Lagerwall0b9ea932011-04-05 16:07:49 +02001520 if self._input_offset >= len(self._input):
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001521 close_unregister_and_remove(fd)
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001522 elif mode & select_POLLIN_POLLPRI:
1523 data = os.read(fd, 4096)
1524 if not data:
1525 close_unregister_and_remove(fd)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001526 self._fd2output[fd].append(data)
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001527 else:
1528 # Ignore hang up or errors.
1529 close_unregister_and_remove(fd)
1530
1531 return (stdout, stderr)
1532
1533
Reid Kleckner2b228f02011-03-16 16:57:54 -04001534 def _communicate_with_select(self, input, endtime, orig_timeout):
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001535 if not self._communication_started:
1536 self._read_set = []
1537 self._write_set = []
1538 if self.stdin and input:
1539 self._write_set.append(self.stdin)
1540 if self.stdout:
1541 self._read_set.append(self.stdout)
1542 if self.stderr:
1543 self._read_set.append(self.stderr)
1544
1545 if self.stdin and self._input is None:
1546 self._input_offset = 0
1547 self._input = input
1548 if self.universal_newlines:
1549 self._input = self._input.encode(self.stdin.encoding)
1550
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001551 stdout = None # Return
1552 stderr = None # Return
1553
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001554 if self.stdout:
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001555 if not self._communication_started:
1556 self._stdout_buff = []
1557 stdout = self._stdout_buff
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001558 if self.stderr:
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001559 if not self._communication_started:
1560 self._stderr_buff = []
1561 stderr = self._stderr_buff
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001562
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001563 while self._read_set or self._write_set:
Victor Stinner7a8d0812011-04-05 13:13:08 +02001564 timeout = self._remaining_time(endtime)
1565 if timeout is not None and timeout < 0:
1566 raise TimeoutExpired(self.args, orig_timeout)
Georg Brandl86b2fb92008-07-16 03:43:04 +00001567 try:
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001568 (rlist, wlist, xlist) = \
1569 select.select(self._read_set, self._write_set, [],
Victor Stinner7a8d0812011-04-05 13:13:08 +02001570 timeout)
Georg Brandl86b2fb92008-07-16 03:43:04 +00001571 except select.error as e:
1572 if e.args[0] == errno.EINTR:
1573 continue
1574 raise
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001575
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001576 # According to the docs, returning three empty lists indicates
1577 # that the timeout expired.
1578 if not (rlist or wlist or xlist):
Reid Kleckner2b228f02011-03-16 16:57:54 -04001579 raise TimeoutExpired(self.args, orig_timeout)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001580 # We also check what time it is ourselves for good measure.
Reid Kleckner2b228f02011-03-16 16:57:54 -04001581 self._check_timeout(endtime, orig_timeout)
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001582
Guido van Rossum98297ee2007-11-06 21:34:58 +00001583 # XXX Rewrite these to use non-blocking I/O on the
1584 # file objects; they are no longer using C stdio!
1585
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001586 if self.stdin in wlist:
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001587 chunk = self._input[self._input_offset :
1588 self._input_offset + _PIPE_BUF]
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001589 try:
1590 bytes_written = os.write(self.stdin.fileno(), chunk)
1591 except OSError as e:
1592 if e.errno == errno.EPIPE:
1593 self.stdin.close()
Ross Lagerwall0b9ea932011-04-05 16:07:49 +02001594 self._write_set.remove(self.stdin)
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001595 else:
1596 raise
1597 else:
Ross Lagerwall0b9ea932011-04-05 16:07:49 +02001598 self._input_offset += bytes_written
1599 if self._input_offset >= len(self._input):
Ross Lagerwall4f61b022011-04-05 15:34:00 +02001600 self.stdin.close()
Ross Lagerwall0b9ea932011-04-05 16:07:49 +02001601 self._write_set.remove(self.stdin)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001602
1603 if self.stdout in rlist:
1604 data = os.read(self.stdout.fileno(), 1024)
Guido van Rossumc9e363c2007-05-15 23:18:55 +00001605 if not data:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001606 self.stdout.close()
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001607 self._read_set.remove(self.stdout)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001608 stdout.append(data)
1609
1610 if self.stderr in rlist:
1611 data = os.read(self.stderr.fileno(), 1024)
Guido van Rossumc9e363c2007-05-15 23:18:55 +00001612 if not data:
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001613 self.stderr.close()
Reid Kleckner31aa7dd2011-03-14 12:02:10 -04001614 self._read_set.remove(self.stderr)
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001615 stderr.append(data)
1616
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001617 return (stdout, stderr)
1618
Gregory P. Smithd06fa472009-07-04 02:46:54 +00001619
Christian Heimesa342c012008-04-20 21:01:16 +00001620 def send_signal(self, sig):
1621 """Send a signal to the process
1622 """
1623 os.kill(self.pid, sig)
1624
1625 def terminate(self):
1626 """Terminate the process with SIGTERM
1627 """
1628 self.send_signal(signal.SIGTERM)
1629
1630 def kill(self):
1631 """Kill the process with SIGKILL
1632 """
1633 self.send_signal(signal.SIGKILL)