# HG changeset patch # User Ned Deily # Date 1290823219 28800 # Branch release31-maint # Node ID ae0105cdc56e78a47d387fc94a9827105eac9159 # Parent 2de0200a768ec2458d572a22299287b300c3acc0 Issue9922: subprocess.getstatusoutput needs to return stdout as bytes rather than string [3.1 backport - rev1] diff -r 2de0200a768e -r ae0105cdc56e Doc/library/subprocess.rst --- Doc/library/subprocess.rst Fri Nov 26 19:29:10 2010 +0100 +++ Doc/library/subprocess.rst Fri Nov 26 18:00:19 2010 -0800 @@ -267,11 +267,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. @@ -284,7 +284,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 2de0200a768e -r ae0105cdc56e Lib/subprocess.py --- Lib/subprocess.py Fri Nov 26 19:29:10 2010 +0100 +++ Lib/subprocess.py Fri Nov 26 18:00:19 2010 -0800 @@ -133,11 +133,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. @@ -146,7 +146,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. @@ -553,17 +553,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 @@ -575,7 +575,7 @@ >>> import subprocess >>> subprocess.getoutput('ls /bin/ls') - '/bin/ls' + b'/bin/ls' """ return getstatusoutput(cmd)[1] diff -r 2de0200a768e -r ae0105cdc56e Lib/test/test_subprocess.py --- Lib/test/test_subprocess.py Fri Nov 26 19:29:10 2010 +0100 +++ Lib/test/test_subprocess.py Fri Nov 26 18:00:19 2010 -0800 @@ -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.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