Buffering of sys.stdout and sys.stderr in python3 (and documentation)

swatkins at gmail.com swatkins at gmail.com
Tue Nov 12 06:48:09 CET 2013

It's surprising and broken that stderr should be buffered in python3.  python3 calls setvbuf(3) on stderr at startup to achieve this chuckle-headed behavior.  It makes stderr line buffered if on a terminal, and fully buffered if redirected to a log file.  A fully buffered stderr is a very bad idea.

This change goes against the C89 and C99 standards, 40+ years of precedent, and the behavior of nearly every other current programming language.

Error messages will not show up in logs on time, and may be completely lost if the process is terminated before the buffer is flushed, e.g. with SIGTERM

How about fixing this?

Until then, we should write this in every script:

sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 0)

More information about the Python-list mailing list