[New-bugs-announce] [issue4705] python3.0 -u: unbuffered stdout

STINNER Victor report at bugs.python.org
Sat Dec 20 14:47:23 CET 2008

New submission from STINNER Victor <victor.stinner at haypocalc.com>:

I like and I need an "unbuffered" standard output which was provided 
by -u command line option (or PYTHONUNBUFFERED environment variable). 
Current status of -u option in Python3: the option exists and change 
the buffer size (disable buffering) of the stdin, stdout and stderr 
file descriptors.

The problem is in initstdio() which creates files with buffering=-1 
(default buffer) instead of buffering=0 (no buffering) or buffering=1 
(line buffer). But open() enable line buffering of TextIOWrapper is 
buffering=-1 and the raw file is a tty.

Example with py3k trunk:
$ ./python
>>> import sys; sys.stdout.line_buffering
$ ./python |cat
>>> import sys; sys.stdout.line_buffering

I would like line buffering when stdout is redirected to a pipe and -u 
option is used. initstdio() have to be changed to choose buffering 
option. So it's something like:

Index: Python/pythonrun.c
--- Python/pythonrun.c  (révision 67870)
+++ Python/pythonrun.c  (copie de travail)
@@ -810,7 +810,12 @@
        else {
-               if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, 
+               int buffering;
+               if (1)
+                       buffering = 1; /* line */
+               else
+                       buffering = -1; /* default */
+               if (!(std = PyFile_FromFd(fd, "<stdout>", "w", 
buffering, encoding,
                                          errors, "\n", 0))) {
                        goto error;

But "if (1)" have to be replaced "if -u option is used" :-) See 
unbuffered variable of Modules/main.c.

messages: 78102
nosy: haypo
severity: normal
status: open
title: python3.0 -u: unbuffered stdout
versions: Python 3.0

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list