[Python-3000] print() flushing problem.

Wojciech Walczak wojtek.gminick.walczak at gmail.com
Sun Nov 11 18:40:05 CET 2007

2007/11/9, Guido van Rossum <guido at python.org>:

> Well, passing '1' as the buffer size to open() is supposed to set the
> buffer size to the default value (typically 8K) *and* set a special
> flag that forces a flush() whenever a line end is written.

As for now I can't even imagine how to implement the latter in Lib/io.py.
If anyone would like to do that or give me some advice on how to do that - that
would be great.
Anyway, isn't it easier to set the buffer size in Lib/io.py to
DEFAULT_BUFFER_SIZE whenever open() is passing '1' as the buffer size?
I mean changing these lines in Lib/io.py's open():
if buffering < 0 and raw.isatty():
   buffering = 1
if buffering < 0 and raw.isatty():
   buffering = DEFAULT_BUFFER_SIZE
...and then we can implement flush() whenever a line end is written (I
mean that print()s end parameter is set and is not empty string) in
builtin_print(). Code below is not a proposition for a patch. It is
rather proof of concept ;-)
	if (end == NULL || end == Py_None)
		err = PyFile_WriteString("\n", file);
	else {
		err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
                                  if(PyObject_Length(end) > 0) {
		     if(Py_InteractiveFlag) {
                                              err =
PyFile_WriteString("\n", file);
		     else {
		         tmp = PyObject_CallMethod(file, "flush", "");
		         if (tmp == NULL)

To make it work I had to change Python/pythonrun.c to make sure that
it sets Py_InteractiveFlag when python works in interactive mode. I
have added that line:

Py_InteractiveFlag = 1;

to PyRun_InteractiveLoopFlags(). I find it arguable if this is the
best place ;-)

Wojtek Walczak

More information about the Python-3000 mailing list