Issue 13761: Add flush keyword to print() (original) (raw)

process

Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: Arfrever, cameron, eric.araujo, georg.brandl, giampaolo.rodola, gvanrossum, pitrou, python-dev, techtonik, terry.reedy
Priority: normal Keywords: easy, patch

Created on 2012-01-11 08:24 by georg.brandl, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
flush.patch georg.brandl,2012-01-11 19:50 review
Messages (14)
msg151041 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2012-01-11 08:24
Add a flush keyword argument to print(), defaulting to False. If true, output will be flushed immediately. See http://mail.python.org/pipermail/python-ideas/2012-January/013340.html
msg151075 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2012-01-11 18:27
In the python-ideas discussion people have argued that flush=False should or could be interpreted as "definitely do not flush" which is unimplementable (the buffer may be full, or the stream may be unbuffered, and there is no way to tell a write() call to skip the flushing if the stream's policy would be to flush). Sticklers have proposed to name the flag "force_flush" to avoid this ambiguity, or to pass None instead of False. I think that's all being hypercorrect -- first of all, nobody is going to explicitly write flush=False since that is the default, and second of all, who could possibly care about not flushing on a per-call basis? The flag should have a short name and simple semantics. flush=True/False does this: if flush is true, an explicit flush() call is made, if it is false, flush() is not called. What the stream does is its business.
msg151082 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-01-11 19:28
I added a couple of lines to the 3.2, 3.3 print doc in #11633. When a change is made here, they could be updated to something like: "Output buffering is normally determined by *file*. Use flush=True to force immediate output to a device such as a screen." Unless there is a good reason, I prefer, unlike the other params, *not* allowing flush=None as a synonym for flush=<the default, ie False>.
msg151083 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2012-01-11 19:38
There is a reason: we don't usually check the type of flag arguments; just their truth value is used.
msg151084 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2012-01-11 19:50
Here is a patch. One open question is whether failure in flush() should be reraised (as implemented in the patch), or ignored (as in input()).
msg151091 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2012-01-11 21:14
I'd ignore the failure. It could easily mask some other more interesting error.
msg151104 - (view) Author: Cameron Simpson (cameron) * Date: 2012-01-12 01:12
I'm against ignoring a flush failure. What happened to "errors should never pass silently"? IMO, if we get as far as calling flush and having an exception occur, a "more interesting error" hasn't yet occurred. I really dislike things that fail silently. If the caller asks print to flush, and the flush fails, the caller's request has not been met. The caller needs to know or incorrect behaviour can ensue.
msg151107 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-01-12 01:51
I think it should be re-raised (or rather, let pass through as in the patch). If you are asking for a flush you want to know why it failed. (it shouldn't ever fail on "normal" files, anyway)
msg151108 - (view) Author: Cameron Simpson (cameron) * Date: 2012-01-12 02:02
Flush can fail of disc full or any number of low level things that prevent the OS getting the data into the on-disc file. Speaking for myself, I certainly want to know if that happens.
msg151109 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2012-01-12 02:57
Ok, I'm fine with passing through the exception from flush().
msg151119 - (view) Author: anatoly techtonik (techtonik) Date: 2012-01-12 08:24
I am not discussing "printing to file", so my 0.02 is that "flush=True" should be made default for print() with sys.stdout, because it is probably what users expect when calling this function. If somebody needs buffering/cache or more fine-grained control over output, they are free to use sys.stdout.write/flush directly. And users won't be happy to wrap every print() into try/catch to guard from the flush exceptions.
msg151132 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2012-01-12 15:55
Anatoly, duly noted, and disagreed with.
msg151134 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-01-12 16:08
Patch LGTM. (s/assertEquals/assertEqual/ though, or you’ll get a warning)
msg151187 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-01-13 18:42
New changeset 3120a988a1a3 by Georg Brandl in branch 'default': Closes #13761: add a "flush" keyword argument to print(). http://hg.python.org/cpython/rev/3120a988a1a3
History
Date User Action Args
2022-04-11 14:57:25 admin set github: 57970
2012-01-13 18:42:17 python-dev set status: open -> closednosy: + python-devmessages: + resolution: fixedstage: needs patch -> resolved
2012-01-12 16:08:27 eric.araujo set nosy: + eric.araujomessages: +
2012-01-12 15:55:06 gvanrossum set messages: +
2012-01-12 08:24:56 techtonik set nosy: + techtonikmessages: +
2012-01-12 04:04:16 Arfrever set nosy: + Arfrever
2012-01-12 02:57:29 gvanrossum set messages: +
2012-01-12 02:02:17 cameron set messages: +
2012-01-12 01:51:23 pitrou set nosy: + pitroumessages: +
2012-01-12 01:12:35 cameron set messages: +
2012-01-11 23:56:28 cameron set nosy: + cameron
2012-01-11 21:14:27 gvanrossum set messages: +
2012-01-11 19:50:17 georg.brandl set files: + flush.patchkeywords: + patchmessages: +
2012-01-11 19:38:57 georg.brandl set messages: +
2012-01-11 19:28:07 terry.reedy set nosy: + terry.reedymessages: +
2012-01-11 18:27:13 gvanrossum set nosy: + gvanrossummessages: +
2012-01-11 08:47:31 giampaolo.rodola set nosy: + giampaolo.rodola
2012-01-11 08:24:42 georg.brandl create