Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e004175

Browse files
committed
Issue #10197 Rework subprocess.get[status]output to use subprocess functionality and thus to work on Windows. Patch by Nick Coghlan.
1 parent 0bdcdec commit e004175

3 files changed

Lines changed: 14 additions & 24 deletions

File tree

Lib/subprocess.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -681,21 +681,15 @@ def getstatusoutput(cmd):
681681
>>> subprocess.getstatusoutput('/bin/junk')
682682
(256, 'sh: /bin/junk: not found')
683683
"""
684-
with os.popen('{ ' + cmd + '; } 2>&1', 'r') as pipe:
685-
try:
686-
text = pipe.read()
687-
sts = pipe.close()
688-
except:
689-
process = pipe._proc
690-
process.kill()
691-
process.wait()
692-
raise
693-
if sts is None:
694-
sts = 0
695-
if text[-1:] == '\n':
696-
text = text[:-1]
697-
return sts, text
698-
684+
try:
685+
data = check_output(cmd, shell=True, universal_newlines=True, stderr=STDOUT)
686+
status = 0
687+
except CalledProcessError as ex:
688+
data = ex.output
689+
status = ex.returncode
690+
if data[-1:] == '\n':
691+
data = data[:-1]
692+
return status, data
699693

700694
def getoutput(cmd):
701695
"""Return output (stdout or stderr) of executing cmd in a shell.

Lib/test/test_subprocess.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,13 +2133,6 @@ def test_kill_dead(self):
21332133
def test_terminate_dead(self):
21342134
self._kill_dead_process('terminate')
21352135

2136-
2137-
# The module says:
2138-
# "NB This only works (and is only relevant) for UNIX."
2139-
#
2140-
# Actually, getoutput should work on any platform with an os.popen, but
2141-
# I'll take the comment as given, and skip this suite.
2142-
@unittest.skipUnless(os.name == 'posix', "only relevant for UNIX")
21432136
class CommandTests(unittest.TestCase):
21442137
def test_getoutput(self):
21452138
self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
@@ -2153,8 +2146,8 @@ def test_getoutput(self):
21532146
try:
21542147
dir = tempfile.mkdtemp()
21552148
name = os.path.join(dir, "foo")
2156-
2157-
status, output = subprocess.getstatusoutput('cat ' + name)
2149+
status, output = subprocess.getstatusoutput(
2150+
("type " if mswindows else "cat ") + name)
21582151
self.assertNotEqual(status, 0)
21592152
finally:
21602153
if dir is not None:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- Issue #10197: Rework subprocess.get[status]output to use subprocess
17+
functionality and thus to work on Windows. Patch by Nick Coghlan.
18+
1619
- Issue #19286: Directories in ``package_data`` are no longer added to
1720
the filelist, preventing failure outlined in the ticket.
1821

0 commit comments

Comments
 (0)