Search code examples
pythonstreamsubprocesspopenbuffering

In subprocess.Popen, which file does bufsize apply to?


I'm in need of setting the stderr stream in a Popen call to line-buffered. I discovered the bufsize argument, but it doesn't say which of the 3 (stdin, stdout, stderr) files it's actually applied to.

  • Which file does the bufsize argument modify?
  • How do I modify the other file buffering modes?

Solution

  • Use the source, Luke :-) /usr/lib/python2.7/subprocess.py:

    if p2cwrite is not None:
        self.stdin = os.fdopen(p2cwrite, 'wb', bufsize)
    if c2pread is not None:
        if universal_newlines:
            self.stdout = os.fdopen(c2pread, 'rU', bufsize)
        else:
            self.stdout = os.fdopen(c2pread, 'rb', bufsize)
    if errread is not None:
        if universal_newlines:
            self.stderr = os.fdopen(errread, 'rU', bufsize)
        else:
            self.stderr = os.fdopen(errread, 'rb', bufsize)
    

    So it seems it uses bufsize in all of them, no way to be specific.