# HG changeset patch # User Ned Deily # Date 1285236039 25200 # Branch release31-maint # Node ID 7de8f04ff4b0d1b95ed684599eb6c9ca35274179 # Parent b1bee1383d8f7b8fc38c1ea821639b271918a132 Issue9922: subprocess.getstatusoutput needs to return stdout as bytes rather than string [3.1 backport] diff -r b1bee1383d8f -r 7de8f04ff4b0 Doc/library/subprocess.rst --- Doc/library/subprocess.rst Tue Sep 21 21:12:39 2010 +0200 +++ Doc/library/subprocess.rst Thu Sep 23 03:00:39 2010 -0700 @@ -249,11 +249,11 @@ >>> import subprocess >>> subprocess.getstatusoutput('ls /bin/ls') - (0, '/bin/ls') + (0, b'/bin/ls') >>> subprocess.getstatusoutput('cat /bin/junk') - (256, 'cat: /bin/junk: No such file or directory') + (256, b'cat: /bin/junk: No such file or directory') >>> subprocess.getstatusoutput('/bin/junk') - (256, 'sh: /bin/junk: not found') + (256, b'sh: /bin/junk: not found') Availability: UNIX. @@ -266,7 +266,7 @@ >>> import subprocess >>> subprocess.getoutput('ls /bin/ls') - '/bin/ls' + b'/bin/ls' Availability: UNIX. diff -r b1bee1383d8f -r 7de8f04ff4b0 Lib/subprocess.py --- Lib/subprocess.py Tue Sep 21 21:12:39 2010 +0200 +++ Lib/subprocess.py Thu Sep 23 03:00:39 2010 -0700 @@ -133,11 +133,11 @@ >>> import subprocess >>> subprocess.getstatusoutput('ls /bin/ls') - (0, '/bin/ls') + (0, b'/bin/ls') >>> subprocess.getstatusoutput('cat /bin/junk') - (256, 'cat: /bin/junk: No such file or directory') + (256, b'cat: /bin/junk: No such file or directory') >>> subprocess.getstatusoutput('/bin/junk') - (256, 'sh: /bin/junk: not found') + (256, b'sh: /bin/junk: not found') getoutput(cmd): Return output (stdout or stderr) of executing cmd in a shell. @@ -147,7 +147,7 @@ >>> import subprocess >>> subprocess.getoutput('ls /bin/ls') - '/bin/ls' + b'/bin/ls' check_output(*popenargs, **kwargs): Run command with arguments and return its output as a byte string. @@ -554,17 +554,17 @@ >>> import subprocess >>> subprocess.getstatusoutput('ls /bin/ls') - (0, '/bin/ls') + (0, b'/bin/ls') >>> subprocess.getstatusoutput('cat /bin/junk') - (256, 'cat: /bin/junk: No such file or directory') + (256, b'cat: /bin/junk: No such file or directory') >>> subprocess.getstatusoutput('/bin/junk') - (256, 'sh: /bin/junk: not found') + (256, b'sh: /bin/junk: not found') """ - pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r') - text = pipe.read() - sts = pipe.close() + p = Popen('{ ' + cmd + '; } 2>&1', shell=True, stdout=PIPE) + text = p.communicate()[0] + sts = p.returncode if sts is None: sts = 0 - if text[-1:] == '\n': text = text[:-1] + if text[-1:] == b'\n': text = text[:-1] return sts, text @@ -576,7 +576,7 @@ >>> import subprocess >>> subprocess.getoutput('ls /bin/ls') - '/bin/ls' + b'/bin/ls' """ return getstatusoutput(cmd)[1] diff -r b1bee1383d8f -r 7de8f04ff4b0 Lib/test/test_subprocess.py --- Lib/test/test_subprocess.py Tue Sep 21 21:12:39 2010 +0200 +++ Lib/test/test_subprocess.py Thu Sep 23 03:00:39 2010 -0700 @@ -895,11 +895,22 @@ # Actually, getoutput should work on any platform with an os.popen, but # I'll take the comment as given, and skip this suite. if os.name == 'posix': + def setUp(self): + support.unlink(support.TESTFN) + + def tearDown(self): + support.unlink(support.TESTFN) def test_getoutput(self): - self.assertEquals(subprocess.getoutput('echo xyzzy'), 'xyzzy') + self.assertEquals(subprocess.getoutput('echo xyzzy'), b'xyzzy') self.assertEquals(subprocess.getstatusoutput('echo xyzzy'), - (0, 'xyzzy')) + (0, b'xyzzy')) + with open(support.TESTFN, "wb") as f: + invalid_utf8 = b'\xac' + f.write(invalid_utf8) + f.close() + self.assertEqual(subprocess.getoutput('cat ' + support.TESTFN), + invalid_utf8) # we use mkdtemp in the next line to create an empty directory # under our exclusive control; from that, we can invent a pathname