(original) (raw)
changeset: 86880:05ce1bd1a4c2 parent: 86871:6e592d972b86 parent: 86879:c34e163c0086 user: Tim Golden mail@timgolden.me.uk date: Sun Nov 03 12:55:51 2013 +0000 files: Lib/subprocess.py Lib/test/test_subprocess.py Misc/NEWS description: Issue #10197 Rework subprocess.get[status]output to use subprocess functionality and thus to work on Windows. Patch by Nick Coghlan. diff -r 6e592d972b86 -r 05ce1bd1a4c2 Lib/subprocess.py --- a/Lib/subprocess.py Sun Nov 03 11:59:28 2013 +0000 +++ b/Lib/subprocess.py Sun Nov 03 12:55:51 2013 +0000 @@ -700,21 +700,15 @@ >>> subprocess.getstatusoutput('/bin/junk') (256, 'sh: /bin/junk: not found') """ - with os.popen('{ ' + cmd + '; } 2>&1', 'r') as pipe: - try: - text = pipe.read() - sts = pipe.close() - except: - process = pipe._proc - process.kill() - process.wait() - raise - if sts is None: - sts = 0 - if text[-1:] == '\n': - text = text[:-1] - return sts, text - + try: + data = check_output(cmd, shell=True, universal_newlines=True, stderr=STDOUT) + status = 0 + except CalledProcessError as ex: + data = ex.output + status = ex.returncode + if data[-1:] == '\n': + data = data[:-1] + return status, data def getoutput(cmd): """Return output (stdout or stderr) of executing cmd in a shell. diff -r 6e592d972b86 -r 05ce1bd1a4c2 Lib/test/test_subprocess.py --- a/Lib/test/test_subprocess.py Sun Nov 03 11:59:28 2013 +0000 +++ b/Lib/test/test_subprocess.py Sun Nov 03 12:55:51 2013 +0000 @@ -2158,13 +2158,6 @@ 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 @@ try: dir = tempfile.mkdtemp() name = os.path.join(dir, "foo") - - status, output = subprocess.getstatusoutput('cat ' + name) + status, output = subprocess.getstatusoutput( + ("type " if mswindows else "cat ") + name) self.assertNotEqual(status, 0) finally: if dir is not None: diff -r 6e592d972b86 -r 05ce1bd1a4c2 Misc/NEWS --- a/Misc/NEWS Sun Nov 03 11:59:28 2013 +0000 +++ b/Misc/NEWS Sun Nov 03 12:55:51 2013 +0000 @@ -31,6 +31,9 @@ Library ------- +- Issue #10197: Rework subprocess.get[status]output to use subprocess + functionality and thus to work on Windows. Patch by Nick Coghlan + - Issue #19403: contextlib.redirect_stdout is now reentrant - Issue #19286: Directories in ``package_data`` are no longer added to /mail@timgolden.me.uk