cpython: 28a0ae3dcb16 (original) (raw)
Mercurial > cpython
changeset 86882:28a0ae3dcb16
Issue #10197: merge heads [#10197]
Tim Golden mail@timgolden.me.uk | |
---|---|
date | Sun, 03 Nov 2013 14:21:29 +0000 |
parents | 2ed8d500e113(current diff)05ce1bd1a4c2(diff) |
children | fe828884a077 |
files | Misc/NEWS |
diffstat | 3 files changed, 14 insertions(+), 24 deletions(-)[+] [-] Lib/subprocess.py 24 Lib/test/test_subprocess.py 11 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -700,21 +700,15 @@ def getstatusoutput(cmd): >>> subprocess.getstatusoutput('/bin/junk') (256, 'sh: /bin/junk: not found') """
- with os.popen('{ ' + cmd + '; } 2>&1', 'r') as pipe:
try:[](#l1.8)
text = pipe.read()[](#l1.9)
sts = pipe.close()[](#l1.10)
except:[](#l1.11)
process = pipe._proc[](#l1.12)
process.kill()[](#l1.13)
process.wait()[](#l1.14)
raise[](#l1.15)
- if sts is None:
sts = 0[](#l1.17)
- if text[-1:] == '\n':
text = text[:-1][](#l1.19)
- return sts, text
- try:
data = check_output(cmd, shell=True, universal_newlines=True, stderr=STDOUT)[](#l1.23)
status = 0[](#l1.24)
- except CalledProcessError as ex:
data = ex.output[](#l1.26)
status = ex.returncode[](#l1.27)
- if data[-1:] == '\n':
data = data[:-1][](#l1.29)
- return status, data
def getoutput(cmd): """Return output (stdout or stderr) of executing cmd in a shell.
--- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -2158,13 +2158,6 @@ class Win32ProcessTestCase(BaseTestCase) def test_terminate_dead(self): self._kill_dead_process('terminate') - -# The module says: -# "NB This only works (and is only relevant) for UNIX." -# -# Actually, getoutput should work on any platform with an os.popen, but -# I'll take the comment as given, and skip this suite. -@unittest.skipUnless(os.name == 'posix', "only relevant for UNIX") class CommandTests(unittest.TestCase): def test_getoutput(self): self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy') @@ -2178,8 +2171,8 @@ class CommandTests(unittest.TestCase): try: dir = tempfile.mkdtemp() name = os.path.join(dir, "foo") -
status, output = subprocess.getstatusoutput('cat ' + name)[](#l2.22)
status, output = subprocess.getstatusoutput([](#l2.23)
("type " if mswindows else "cat ") + name)[](#l2.24) self.assertNotEqual(status, 0)[](#l2.25) finally:[](#l2.26) if dir is not None:[](#l2.27)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -35,6 +35,9 @@ Library TypeError instead of TclError on wrong number of arguments. Original patch by Guilherme Polo. +- Issue #10197: Rework subprocess.get[status]output to use subprocess