From: "Brian C. Lane" bcl@redhat.com
Setting stderr would write to the passed file, etc. but would not allow combining the stdout and stderr output in the return value. This changes the behavior so that setting stderr=subprocess.STDOUT will result in the stderr output being returned by the method. Note that it may be mixed with stdout, depending on how the called process flushes its buffers. --- src/pylorax/executils.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/src/pylorax/executils.py b/src/pylorax/executils.py index 6da74c8..2f0a042 100644 --- a/src/pylorax/executils.py +++ b/src/pylorax/executils.py @@ -192,6 +192,9 @@ def execWithCapture(command, argv, stdin = None, stderr = None, root=None, @param cwd working directory to pass to Popen @param raise_err raise CalledProcessError when the returncode is not 0 @return The output of command from stdout. + + Setting stderr to subprocess.STDOUT will combine stderr and stdout + in the return value. """ def chroot(): os.chroot(root) @@ -215,9 +218,13 @@ def execWithCapture(command, argv, stdin = None, stderr = None, root=None, elif stdin is None or not isinstance(stdin, file): stdin = sys.stdin.fileno()
+ stderr_pipe = subprocess.PIPE if isinstance(stderr, str): stderr = os.open(stderr, os.O_RDWR|os.O_CREAT) stderrclose = lambda : os.close(stderr) + elif stderr is subprocess.STDOUT: + stderr_pipe = subprocess.STDOUT + stderr = sys.stderr.fileno() elif isinstance(stderr, int): pass elif stderr is None or not isinstance(stderr, file): @@ -238,7 +245,7 @@ def execWithCapture(command, argv, stdin = None, stderr = None, root=None, try: proc = subprocess.Popen([command] + argv, stdin=stdin, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, + stderr=stderr_pipe, preexec_fn=preexec_fn, cwd=cwd, env=env)