[Buildbot-devel] Where did my stdout go?
Greg Ward
gerg.ward+buildbot at gmail.com
Mon Nov 5 16:00:03 UTC 2007
On 11/4/07, Sergey A. Lipnevich <sergey at optimaltec.com> wrote:
> Can you explicitly make the child inherit parent's streams?
>
> from sys import stdout, stderr
> from subprocess import Popen
> Popen(['ls'], shell=True, stdout=stdout, stderr=stderr)
Ah-HA!! Thank you. That worked perfectly. Gory details follow. (I
suspect this could be useful to anyone setting up build slaves on
Windows.)
Turns out this has nothing to do with Buildbot. I trivially
reproduced the problem with a tiny Python script:
"""
import sys
import subprocess
print "test.py: hello!"
subprocess.call(["tasklist"],
stdout=None, # defaults
stderr=None)
print "test.py: goodbye cruel world"
"""
>From the Windows command prompt, if I run "test.py" I get what I
expect: "hello!", output of tasklist.exe, "goodbye cruel world".
Good.
But if I run "test.py > x", then the file x only contains the output
of test.py itself. The output of tasklist.exe is gone. There's my
problem. No Buildbot involved: just Python, subprocess, and Windows.
So then I tried Sergey's suggestion and changed the subprocess.call() to
subprocess.call(["tasklist"],
stdout=sys.stdout,
stderr=sys.stderr)
...and it works! Excellent. The only problem is that when I redirect
to a file, the "hello!" line comes *after* the output of tasklist.exe.
Clearly a buffering problem. So I added sys.stdout.flush() before
subprocess.call(), and all is well.
Thanks!
Greg
More information about the devel
mailing list