bpo-28647: Update -u documentation after bpo-30404 by berkerpeksag · Pull Request #3961 · python/cpython (original) (raw)

"stdin is always unbuffered"

Wait, what? It's always buffered, no?

Reading one byte in Python reads 1024 bytes at the C level:

haypo@selma$ strace -o trace ./python -u -c 'import sys; sys.stdin.buffer.read(1)'
abc
haypo@selma$ grep -F 'read(0,' trace
read(0, "abc\n", 1024)                  = 4

If stdin is a not a TTY, we read even more at the C level: 4096 bytes.

haypo@selma$ echo abc|strace -o trace ./python -u -c 'import sys; sys.stdin.buffer.read(1)'
haypo@selma$ grep -F 'read(0,' trace
read(0, "abc\n", 4096)                  = 4

The -u option has an effect on sys.stdin: line_buffering. I don't think that it matters in practice, since line buffering only impacts write() whereas sys.stdin is open for read-only.

haypo@selma$ ./python -c 'import sys; print(sys.stdin.line_buffering)'
True
haypo@selma$ ./python -u -c 'import sys; print(sys.stdin.line_buffering)'
False