Message 123150 - Python tracker (original) (raw)
My idea is simply using Popen with appropriate args for stdout and stderr instead of using a shell command with redirections:
--- Lib/subprocess.py (révision 86943) +++ Lib/subprocess.py (copie de travail) @@ -560,11 +560,7 @@ return ''.join(result)
-# Various tools for executing commands and looking at their output and status. -# -# NB This only works (and is only relevant) for UNIX.
-def getstatusoutput(cmd): +def getstatusoutput(cmd, shell=True): """Return (status, output) of executing cmd in a shell.
Execute the string 'cmd' in a shell with os.popen() and return a 2-tuple
@@ -581,14 +577,21 @@ >>> subprocess.getstatusoutput('/bin/junk') (256, 'sh: /bin/junk: not found') """
- pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
- text = pipe.read()
- sts = pipe.close()
- if sts is None: sts = 0
- if text[-1:] == '\n': text = text[:-1]
Wish I could use with...
- popen = Popen(cmd, shell=shell, stdout=PIPE, stderr=STDOUT)
- sts = popen.communicate() #or wait() or whatever is the right thing
- try:
text = process.stdout.read()
- finally:
process.stdout.close()
- if sts is None:
sts = 0
- if text.endswith('\n'):
return sts, texttext = text[:-1]
(The new “shell” argument is icing on the cake, allowing us to later support a list as cmd argument like Popen does.)