# HG changeset patch # User Ned Deily # Date 1285236500 25200 # Branch py3k # Node ID 31723c6a4deabbc310d43a31c5ffc966a8b7c8e1 # Parent b844521f8908d784a6ef50737745d923f54efb4b Issue9922: subprocess.getstatusoutput needs to return stdout as bytes rather than string diff -r b844521f8908 -r 31723c6a4dea Doc/library/subprocess.rst --- Doc/library/subprocess.rst Thu Sep 23 02:46:13 2010 +0200 +++ Doc/library/subprocess.rst Thu Sep 23 03:08:20 2010 -0700 @@ -280,11 +280,11 @@ according to the rules for the C function :cfunc:`wait`. Example:: >>> 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. @@ -296,7 +296,7 @@ value is a string containing the command's output. Example:: >>> subprocess.getoutput('ls /bin/ls') - '/bin/ls' + b'/bin/ls' Availability: UNIX. diff -r b844521f8908 -r 31723c6a4dea Lib/subprocess.py --- Lib/subprocess.py Thu Sep 23 02:46:13 2010 +0200 +++ Lib/subprocess.py Thu Sep 23 03:08:20 2010 -0700 @@ -145,11 +145,11 @@ interpreted according to the rules for the C function wait(). Example: >>> 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. @@ -158,7 +158,7 @@ value is a string containing the command's output. Example: >>> 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. @@ -575,17 +575,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 @@ -597,7 +597,7 @@ >>> import subprocess >>> subprocess.getoutput('ls /bin/ls') - '/bin/ls' + b'/bin/ls' """ return getstatusoutput(cmd)[1] diff -r b844521f8908 -r 31723c6a4dea Lib/test/test_subprocess.py --- Lib/test/test_subprocess.py Thu Sep 23 02:46:13 2010 +0200 +++ Lib/test/test_subprocess.py Thu Sep 23 03:08:20 2010 -0700 @@ -1041,10 +1041,22 @@ # 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 setUp(self): + support.unlink(support.TESTFN) + + def tearDown(self): + support.unlink(support.TESTFN) + def test_getoutput(self): - self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy') + self.assertEqual(subprocess.getoutput('echo xyzzy'), b'xyzzy') self.assertEqual(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