This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients vstinner
Date 2008-12-20.13:47:18
SpamBayes Score 3.380629e-13
Marked as misclassified No
Message-id <1229780843.45.0.259077303406.issue4705@psf.upfronthosting.co.za>
In-reply-to
Content
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
True
$ ./python |cat
>>> import sys; sys.stdout.line_buffering
False
------------

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 @@
 #endif
        }
        else {
-               if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, 
encoding,
+               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.
History
Date User Action Args
2008-12-20 13:47:23vstinnersetrecipients: + vstinner
2008-12-20 13:47:23vstinnersetmessageid: <1229780843.45.0.259077303406.issue4705@psf.upfronthosting.co.za>
2008-12-20 13:47:22vstinnerlinkissue4705 messages
2008-12-20 13:47:19vstinnercreate