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.

classification
Title: Python 3 ignored PYTHONUNBUFFERED and -u
Type: behavior Stage:
Components: Documentation Versions: Python 3.1
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: georg.brandl, noam, pitrou, vstinner
Priority: normal Keywords:

Created on 2010-03-23 15:20 by noam, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (8)
msg101584 - (view) Author: Noam Yorav-Raphael (noam) Date: 2010-03-23 15:20
Hello,

Python 3.1 ignored the PYTHONUNBUFFERED environment variable and the '-u' switch (which do the same thing): stdout remains buffered even when the flag is raised.

To reproduce, run:
> python3 -u -c 'import time, sys; sys.stdout.write("a"); time.sleep(1); sys.stdout.write("\n")'

You can see that it first waits a second and then 'a' is printed.

I'm using Ubuntu 9.10. Tested this on both the 3.1.1 installed and svn checkout (revision 79345).

This follows a bug report: https://bugs.launchpad.net/dreampie/+bug/545012
which was reported on win32, so the problem is there too.
msg101586 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-03-23 15:29
-u is not ignored, but use line buffering: see issue #4705 and commit r68977.
msg101587 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-03-23 15:33
In the current state of affaires this is more of a documentation issue.

Python 3 doesn't support totally unbuffered text I/O (and standard streams are open in text mode). What `-u` and PYTHONUNBUFFERED do is that the binary layer of standard streams is unbuffered, but the text layer is still line-buffered (if in a tty).

"python --help" gives you an accurate description:

-u     : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x
         see man page for details on internal buffering relating to '-u'

Also, you can try out:

python3 -u -c  'import time, sys; sys.stdout.buffer.write(b"b"); time.sleep(1); sys.stdout.buffer.write(b"\n")'

To explicitly flush the text layer, you can use the flush() method.
msg101622 - (view) Author: Noam Yorav-Raphael (noam) Date: 2010-03-24 06:18
I typed "man python3" on ubuntu 9.10 and nothing was explained about that - I now checked and found out that it displayed the python2 man page. I don't know where to find the Python 3 man page.

I don't quite see the point in having the streams buffered in one level and unbuffered in another, but I guess there's a reason.

Anyway, how can I make those streams entirely unbuffered? This is for an interactive shell called DreamPie (dreampie.sourceforge.net), and I want to resemble the behavior of the regular shell as close as possible, and it's completely unbuffered.
msg101632 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-03-24 13:09
> I don't quite see the point in having the streams buffered in one
> level and unbuffered in another, but I guess there's a reason.

The reason is simply that it wasn't implemented. Unbuffered I/O isn't
useful very often.

> Anyway, how can I make those streams entirely unbuffered? This is for
> an interactive shell called DreamPie (dreampie.sourceforge.net), and I
> want to resemble the behavior of the regular shell as close as
> possible, and it's completely unbuffered.

You can just call flush() after each write to stdout or stderr.
msg101642 - (view) Author: Noam Yorav-Raphael (noam) Date: 2010-03-24 16:45
I can call flush() after the user code was executed, but a code like this:

>>> for i in range(10):
...     print(i, end=' ')
...     time.sleep(1)

will show the numbers only after 10 seconds, while I would like a number printed every second. I now see that that's what the regular shell does, so it's probably not a very big deal.
msg101650 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-03-24 19:08
Use :

...     print(i, end=' ')
...     sys.stdout.flush()
...     time.sleep(1)
msg102146 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-04-02 08:47
I've fixed the using/cmdline and manpage to describe the current behavior.
History
Date User Action Args
2022-04-11 14:56:59adminsetgithub: 52460
2010-04-02 08:47:26georg.brandlsetstatus: open -> closed
resolution: fixed
messages: + msg102146
2010-03-24 19:08:41vstinnersetmessages: + msg101650
2010-03-24 16:45:10noamsetmessages: + msg101642
2010-03-24 13:09:10pitrousetmessages: + msg101632
2010-03-24 06:18:38noamsetmessages: + msg101622
2010-03-23 15:35:04pitrousetassignee: georg.brandl

components: + Documentation, - IO
nosy: + georg.brandl
2010-03-23 15:33:50pitrousetpriority: normal

messages: + msg101587
2010-03-23 15:29:51vstinnersetnosy: + pitrou
2010-03-23 15:29:26vstinnersetnosy: + vstinner
messages: + msg101586
2010-03-23 15:20:13noamcreate