]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #10197 Rework subprocess.get[status]output to use subprocess functionality...
authorTim Golden <mail@timgolden.me.uk>
Sun, 3 Nov 2013 12:53:17 +0000 (12:53 +0000)
committerTim Golden <mail@timgolden.me.uk>
Sun, 3 Nov 2013 12:53:17 +0000 (12:53 +0000)
Lib/subprocess.py
Lib/test/test_subprocess.py
Misc/NEWS

index 376b50546bd95a21b63a3e9231bb8cdaa60b932e..aec39d49473a38ddee2ef1308f899cce5e9461b3 100644 (file)
@@ -681,21 +681,15 @@ def getstatusoutput(cmd):
     >>> 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.
index 4c9f29bdb8d51716e91e2c54a48a30fb02531f72..77f1ba31551190e31428aea97e685d6bc36cd01d 100644 (file)
@@ -2133,13 +2133,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')
@@ -2153,8 +2146,8 @@ class CommandTests(unittest.TestCase):
         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:
index ab1b98138a08214783ea0d05484d0cd98c2b9276..bdd65a6636c0ec6dbf63f3b407c6f720c5da2c0b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #10197: Rework subprocess.get[status]output to use subprocess
+  functionality and thus to work on Windows. Patch by Nick Coghlan.
+
 - Issue #19286: Directories in ``package_data`` are no longer added to
   the filelist, preventing failure outlined in the ticket.