cpython: 720f0cf580e2 (original) (raw)

Mercurial > cpython

changeset 103203:720f0cf580e2

Issue #6135: Adds encoding and errors parameters to subprocess [#6135]

Steve Dower steve.dower@microsoft.com
date Tue, 06 Sep 2016 20:16:17 -0700
parents 7a243a40b421
children b38e68ff9751
files Doc/library/subprocess.rst Doc/whatsnew/3.6.rst Lib/subprocess.py Lib/test/test_subprocess.py Misc/NEWS
diffstat 5 files changed, 154 insertions(+), 100 deletions(-)[+] [-] Doc/library/subprocess.rst 103 Doc/whatsnew/3.6.rst 3 Lib/subprocess.py 87 Lib/test/test_subprocess.py 59 Misc/NEWS 2

line wrap: on

line diff

--- a/Doc/library/subprocess.rst +++ b/Doc/library/subprocess.rst @@ -38,7 +38,8 @@ compatibility with older versions, see t .. function:: run(args, *, stdin=None, input=None, stdout=None, stderr=None,[](#l1.6)

Run the command described by args. Wait for command to complete, then return a :class:CompletedProcess instance. @@ -60,15 +61,20 @@ compatibility with older versions, see t The input argument is passed to :meth:Popen.communicate and thus to the subprocess's stdin. If used it must be a byte sequence, or a string if

+ .. class:: CompletedProcess The return value from :func:run, representing a process that has finished. @@ -104,8 +114,8 @@ compatibility with older versions, see t .. attribute:: stdout Captured stdout from the child process. A bytes sequence, or a string if

If you ran the process with stderr=subprocess.STDOUT, stdout and stderr will be combined in this attribute, and :attr:stderr will be @@ -114,8 +124,8 @@ compatibility with older versions, see t .. attribute:: stderr Captured stderr from the child process. A bytes sequence, or a string if

.. method:: check_returncode() @@ -249,19 +259,22 @@ default values. The arguments that are m .. index:: single: universal newlines; subprocess module

.. note:: @@ -306,7 +319,8 @@ functions. stderr=None, preexec_fn=None, close_fds=True, shell=False, [](#l1.106) cwd=None, env=None, universal_newlines=False, [](#l1.107) startupinfo=None, creationflags=0, restore_signals=True, [](#l1.108)

Execute a child program in a new process. On POSIX, the class uses :meth:os.execvp-like behavior to execute the child program. On Windows, @@ -482,10 +496,14 @@ functions. .. _side-by-side assembly: https://en.wikipedia.org/wiki/Side-by-Side_Assembly[](#l1.117)

If given, startupinfo will be a :class:STARTUPINFO object, which is passed to the underlying CreateProcess function. @@ -601,11 +619,12 @@ Instances of the :class:Popen class ha Interact with process: Send data to stdin. Read data from stdout 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

.. attribute:: Popen.stdout If the stdout argument was :data:PIPE, this attribute is a readable stream object as returned by :func:open. Reading from the stream provides

.. attribute:: Popen.stderr If the stderr argument was :data:PIPE, this attribute is a readable stream object as returned by :func:open. Reading from the stream provides

.. warning:: @@ -886,7 +907,9 @@ calls these functions. timeout was added. -.. function:: check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False, timeout=None) +.. function:: check_output(args, *, stdin=None, stderr=None, shell=False, [](#l1.196)

Run command with arguments and return its output. @@ -1142,7 +1165,7 @@ handling consistency are valid for these Return (status, output) of executing cmd in a shell. Execute the string cmd in a shell with :meth:Popen.check_output and

--- a/Doc/whatsnew/3.6.rst +++ b/Doc/whatsnew/3.6.rst @@ -589,6 +589,9 @@ proc: ...``) or call explicitly the :met read the exit status of the child process (Contributed by Victor Stinner in :issue:26741). +The :class:subprocess.Popen constructor and all functions that pass arguments +through to it now accept encoding and errors arguments. Specifying either +of these will enable text mode for the stdin, stdout and stderr streams. telnetlib ---------

--- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -30,7 +30,8 @@ class Popen(args, bufsize=-1, executable preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0,

Arguments are: @@ -104,20 +105,13 @@ in the child process prior to executing 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

communicate() returns a tuple (stdout, stderr). @@ -808,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

A trailing newline is stripped from the output. The exit status for the command can be interpreted @@ -859,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,

@@ -912,6 +903,8 @@ class Popen(object): self.pid = None self.returncode = None self.universal_newlines = universal_newlines

# Input and output objects. The general principle is like # this: @@ -944,22 +937,28 @@ class Popen(object): if errread != -1: errread = msvcrt.open_osfhandle(errread.Detach(), 0)

self._closed_child_pipe_fds = False + try:

+ self._execute_child(args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, @@ -993,8 +992,8 @@ class Popen(object): raise

def enter(self): @@ -1779,13 +1778,15 @@ class Popen(object): # Translate newlines, if requested. # This also turns bytes into strings.

return (stdout, stderr) @@ -1797,8 +1798,10 @@ class Popen(object): if self.stdin and self._input is None: self._input_offset = 0 self._input = input

def send_signal(self, sig):

--- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -894,31 +894,42 @@ class ProcessTestCase(BaseTestCase): # # UTF-16 and UTF-32-BE are sufficient to check both with BOM and # without, and UTF-16 and UTF-32.

+ def test_no_leaking(self): # Make sure we leak no resources if not mswindows: @@ -2539,6 +2550,18 @@ class Win32ProcessTestCase(BaseTestCase) with p: self.assertIn(b"physalis", p.stdout.read())

+ def test_call_string(self): # call() function with string argument on Windows rc = subprocess.call(sys.executable +

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -237,6 +237,8 @@ Build Windows ------- +- Issue #6135: Adds encoding and errors parameters to subprocess. +