summaryrefslogtreecommitdiff
path: root/Lib/subprocess.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/subprocess.py')
-rw-r--r--Lib/subprocess.py136
1 files changed, 85 insertions, 51 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index d8d6ab2274..9df9318245 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -30,7 +30,8 @@ class Popen(args, bufsize=-1, executable=None,
preexec_fn=None, close_fds=True, shell=False,
cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0,
- restore_signals=True, start_new_session=False, pass_fds=()):
+ restore_signals=True, start_new_session=False, pass_fds=(),
+ *, encoding=None, errors=None):
Arguments are:
@@ -104,20 +105,13 @@ in the child process prior to executing the command.
If env is not None, it defines the environment variables for the new
process.
-If universal_newlines is False, the file objects stdin, stdout and stderr
-are opened as binary files, and no line ending conversion is done.
+If encoding or errors are specified or universal_newlines is True, the file
+objects stdout and stderr are opened in text mode. See io.TextIOWrapper for
+the interpretation of these parameters are used.
-If universal_newlines is True, the file objects stdout and stderr are
-opened as a text file, but lines may be terminated by any of '\n',
-the Unix end-of-line convention, '\r', the old Macintosh convention or
-'\r\n', the Windows convention. All of these external representations
-are seen as '\n' by the Python program. Also, the newlines attribute
-of the file objects stdout, stdin and stderr are not updated by the
-communicate() method.
-
-In either case, the process being communicated with should start up
-expecting to receive bytes on its standard input and decode them with
-the same encoding they are sent in.
+If no encoding is specified and universal_newlines is False, the file
+objects stdin, stdout and stderr are opened as binary files, and no
+line ending conversion is done.
The startupinfo and creationflags, if given, will be passed to the
underlying CreateProcess() function. They can specify things such as
@@ -234,11 +228,8 @@ communicate(input=None)
and stderr, until end-of-file is reached. Wait for process to
terminate. The optional input argument should be data to be
sent to the child process, or None, if no data should be sent to
- the child. If the Popen instance was constructed with universal_newlines
- set to True, the input argument should be a string and will be encoded
- using the preferred system encoding (see locale.getpreferredencoding);
- if universal_newlines is False, the input argument should be a
- byte string.
+ the child. If the Popen instance was constructed in text mode, the
+ input argument should be a string. Otherwise, it should be bytes.
communicate() returns a tuple (stdout, stderr).
@@ -372,9 +363,11 @@ class SubprocessError(Exception): pass
class CalledProcessError(SubprocessError):
- """This exception is raised when a process run by check_call() or
- check_output() returns a non-zero exit status.
- The exit status will be stored in the returncode attribute;
+ """Raised when a check_call() or check_output() process returns non-zero.
+
+ The exit status will be stored in the returncode attribute, negative
+ if it represents a signal number.
+
check_output() will also store the output in the output attribute.
"""
def __init__(self, returncode, cmd, output=None, stderr=None):
@@ -384,7 +377,16 @@ class CalledProcessError(SubprocessError):
self.stderr = stderr
def __str__(self):
- return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
+ if self.returncode and self.returncode < 0:
+ try:
+ return "Command '%s' died with %r." % (
+ self.cmd, signal.Signals(-self.returncode))
+ except ValueError:
+ return "Command '%s' died with unknown signal %d." % (
+ self.cmd, -self.returncode)
+ else:
+ return "Command '%s' returned non-zero exit status %d." % (
+ self.cmd, self.returncode)
@property
def stdout(self):
@@ -471,7 +473,8 @@ if _mswindows:
__all__.extend(["CREATE_NEW_CONSOLE", "CREATE_NEW_PROCESS_GROUP",
"STD_INPUT_HANDLE", "STD_OUTPUT_HANDLE",
"STD_ERROR_HANDLE", "SW_HIDE",
- "STARTF_USESTDHANDLES", "STARTF_USESHOWWINDOW"])
+ "STARTF_USESTDHANDLES", "STARTF_USESHOWWINDOW",
+ "STARTUPINFO"])
class Handle(int):
closed = False
@@ -520,6 +523,16 @@ DEVNULL = -3
# but it's here so that it can be imported when Python is compiled without
# threads.
+def _optim_args_from_interpreter_flags():
+ """Return a list of command-line arguments reproducing the current
+ optimization settings in sys.flags."""
+ args = []
+ value = sys.flags.optimize
+ if value > 0:
+ args.append('-' + 'O' * value)
+ return args
+
+
def _args_from_interpreter_flags():
"""Return a list of command-line arguments reproducing the current
settings in sys.flags and sys.warnoptions."""
@@ -527,7 +540,6 @@ def _args_from_interpreter_flags():
'debug': 'd',
# 'inspect': 'i',
# 'interactive': 'i',
- 'optimize': 'O',
'dont_write_bytecode': 'B',
'no_user_site': 's',
'no_site': 'S',
@@ -535,8 +547,9 @@ def _args_from_interpreter_flags():
'verbose': 'v',
'bytes_warning': 'b',
'quiet': 'q',
+ # -O is handled in _optim_args_from_interpreter_flags()
}
- args = []
+ args = _optim_args_from_interpreter_flags()
for flag, opt in flag_opt_map.items():
v = getattr(sys.flags, flag)
if v > 0:
@@ -786,8 +799,8 @@ def getstatusoutput(cmd):
""" Return (status, output) of executing cmd in a shell.
Execute the string 'cmd' in a shell with 'check_output' and
- return a 2-tuple (status, output). Universal newlines mode is used,
- meaning that the result with be decoded to a string.
+ return a 2-tuple (status, output). The locale encoding is used
+ to decode the output and process newlines.
A trailing newline is stripped from the output.
The exit status for the command can be interpreted
@@ -837,7 +850,7 @@ class Popen(object):
shell=False, cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0,
restore_signals=True, start_new_session=False,
- pass_fds=()):
+ pass_fds=(), *, encoding=None, errors=None):
"""Create new Popen instance."""
_cleanup()
# Held while anything is calling waitpid before returncode has been
@@ -890,6 +903,8 @@ class Popen(object):
self.pid = None
self.returncode = None
self.universal_newlines = universal_newlines
+ self.encoding = encoding
+ self.errors = errors
# Input and output objects. The general principle is like
# this:
@@ -922,22 +937,28 @@ class Popen(object):
if errread != -1:
errread = msvcrt.open_osfhandle(errread.Detach(), 0)
- if p2cwrite != -1:
- self.stdin = io.open(p2cwrite, 'wb', bufsize)
- if universal_newlines:
- self.stdin = io.TextIOWrapper(self.stdin, write_through=True,
- line_buffering=(bufsize == 1))
- if c2pread != -1:
- self.stdout = io.open(c2pread, 'rb', bufsize)
- if universal_newlines:
- self.stdout = io.TextIOWrapper(self.stdout)
- if errread != -1:
- self.stderr = io.open(errread, 'rb', bufsize)
- if universal_newlines:
- self.stderr = io.TextIOWrapper(self.stderr)
+ text_mode = encoding or errors or universal_newlines
self._closed_child_pipe_fds = False
+
try:
+ if p2cwrite != -1:
+ self.stdin = io.open(p2cwrite, 'wb', bufsize)
+ if text_mode:
+ self.stdin = io.TextIOWrapper(self.stdin, write_through=True,
+ line_buffering=(bufsize == 1),
+ encoding=encoding, errors=errors)
+ if c2pread != -1:
+ self.stdout = io.open(c2pread, 'rb', bufsize)
+ if text_mode:
+ self.stdout = io.TextIOWrapper(self.stdout,
+ encoding=encoding, errors=errors)
+ if errread != -1:
+ self.stderr = io.open(errread, 'rb', bufsize)
+ if text_mode:
+ self.stderr = io.TextIOWrapper(self.stderr,
+ encoding=encoding, errors=errors)
+
self._execute_child(args, executable, preexec_fn, close_fds,
pass_fds, cwd, env,
startupinfo, creationflags, shell,
@@ -971,9 +992,8 @@ class Popen(object):
raise
-
- def _translate_newlines(self, data, encoding):
- data = data.decode(encoding)
+ def _translate_newlines(self, data, encoding, errors):
+ data = data.decode(encoding, errors)
return data.replace("\r\n", "\n").replace("\r", "\n")
def __enter__(self):
@@ -995,6 +1015,11 @@ class Popen(object):
if not self._child_created:
# We didn't get to successfully create a child process.
return
+ if self.returncode is None:
+ # Not reading subprocess exit status creates a zombi process which
+ # is only destroyed at the parent python process exit
+ warnings.warn("subprocess %s is still running" % self.pid,
+ ResourceWarning, source=self)
# In case the child hasn't been waited on, check if it's done.
self._internal_poll(_deadstate=_maxsize)
if self.returncode is None and _active is not None:
@@ -1520,9 +1545,14 @@ class Popen(object):
if errpipe_data:
try:
- os.waitpid(self.pid, 0)
+ pid, sts = os.waitpid(self.pid, 0)
+ if pid == self.pid:
+ self._handle_exitstatus(sts)
+ else:
+ self.returncode = sys.maxsize
except ChildProcessError:
pass
+
try:
exception_name, hex_errno, err_msg = (
errpipe_data.split(b':', 2))
@@ -1748,13 +1778,15 @@ class Popen(object):
# Translate newlines, if requested.
# This also turns bytes into strings.
- if self.universal_newlines:
+ if self.encoding or self.errors or self.universal_newlines:
if stdout is not None:
stdout = self._translate_newlines(stdout,
- self.stdout.encoding)
+ self.stdout.encoding,
+ self.stdout.errors)
if stderr is not None:
stderr = self._translate_newlines(stderr,
- self.stderr.encoding)
+ self.stderr.encoding,
+ self.stderr.errors)
return (stdout, stderr)
@@ -1766,8 +1798,10 @@ class Popen(object):
if self.stdin and self._input is None:
self._input_offset = 0
self._input = input
- if self.universal_newlines and input is not None:
- self._input = self._input.encode(self.stdin.encoding)
+ if input is not None and (
+ self.encoding or self.errors or self.universal_newlines):
+ self._input = self._input.encode(self.stdin.encoding,
+ self.stdin.errors)
def send_signal(self, sig):