Index: Doc/library/subprocess.rst =================================================================== --- Doc/library/subprocess.rst (révision 88721) +++ Doc/library/subprocess.rst (copie de travail) @@ -312,11 +312,11 @@ Return ``(status, output)`` of executing *cmd* in a shell. - Execute the string *cmd* in a shell with :func:`os.popen` and return a 2-tuple - ``(status, output)``. *cmd* is actually run as ``{ cmd ; } 2>&1``, so that the - returned output will contain output or error messages. A trailing newline is - stripped from the output. The exit status for the command can be interpreted - according to the rules for the C function :c:func:`wait`. Example:: + Execute the string *cmd* in a shell and return a 2-tuple (status, output). + The returned output contains output and error messages, uses universal + newlines and removes trailing whitespaces and newlines. The exit status for + the command can be interpreted according to the rules for the C function + :c:func:`wait`. Example: >>> subprocess.getstatusoutput('ls /bin/ls') (0, '/bin/ls') @@ -325,9 +325,9 @@ >>> subprocess.getstatusoutput('/bin/junk') (256, 'sh: /bin/junk: not found') - Availability: UNIX. + .. versionchanged:: 3.3 + Remove all trailing spaces and newlines, not only the last newline. - .. function:: getoutput(cmd) Return output (stdout and stderr) of executing *cmd* in a shell. @@ -338,9 +338,7 @@ >>> subprocess.getoutput('ls /bin/ls') '/bin/ls' - Availability: UNIX. - Exceptions ^^^^^^^^^^ Index: Lib/subprocess.py =================================================================== --- Lib/subprocess.py (révision 88721) +++ Lib/subprocess.py (copie de travail) @@ -143,11 +143,11 @@ getstatusoutput(cmd): Return (status, output) of executing cmd in a shell. - Execute the string 'cmd' in a shell with os.popen() and return a 2-tuple - (status, output). cmd is actually run as '{ cmd ; } 2>&1', so that the - returned output will contain output or error messages. A trailing newline - is stripped from the output. The exit status for the command can be - interpreted according to the rules for the C function wait(). Example: + Execute the string 'cmd' in a shell and return a 2-tuple (status, output). + The returned output contains output and error messages, uses universal + newlines and removes trailing whitespaces and newlines. The exit status + for the command can be interpreted according to the rules for the C + function wait(). Example: >>> subprocess.getstatusoutput('ls /bin/ls') (0, '/bin/ls') @@ -156,7 +156,7 @@ >>> subprocess.getstatusoutput('/bin/junk') (256, 'sh: /bin/junk: not found') -getoutput(cmd): +getoutput(cmd, shell=True, **kw): Return output (stdout or stderr) of executing cmd in a shell. Like getstatusoutput(), except the exit status is ignored and the return @@ -589,11 +589,11 @@ def getstatusoutput(cmd): """Return (status, output) of executing cmd in a shell. - Execute the string 'cmd' in a shell with os.popen() and return a 2-tuple - (status, output). cmd is actually run as '{ cmd ; } 2>&1', so that the - returned output will contain output or error messages. A trailing newline - is stripped from the output. The exit status for the command can be - interpreted according to the rules for the C function wait(). Example: + Execute the string 'cmd' in a shell and return a 2-tuple (status, output). + The returned output contains output and error messages, uses universal + newlines and removes trailing whitespaces and newlines. The exit status + for the command can be interpreted according to the rules for the C + function wait(). Example: >>> import subprocess >>> subprocess.getstatusoutput('ls /bin/ls') @@ -603,12 +603,17 @@ >>> subprocess.getstatusoutput('/bin/junk') (256, 'sh: /bin/junk: not found') """ - pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r') - text = pipe.read() - sts = pipe.close() - if sts is None: sts = 0 - if text[-1:] == '\n': text = text[:-1] - return sts, text + kw.update({'stdout': PIPE, 'stderr': STDOUT, 'universal_newlines': True}) + if not isinstance(cmd, str): + raise TypeError("invalid cmd type (%s, expected string)" % type(cmd)) + with Popen(cmd, shell=shell, **kw) as proc: + stdout, stderr = proc.communicate() + sts = proc.wait() + stdout = stdout.rstrip() + if os.name != 'nt': + # convert status to be interpreted according to the wait() rules + sts = sts << 8 + return sts, stdout def getoutput(cmd):