cpython: 3120a988a1a3 (original) (raw)
Mercurial > cpython
changeset 74359:3120a988a1a3
Closes #13761: add a "flush" keyword argument to print(). [#13761]
Georg Brandl georg@python.org | |
---|---|
date | Fri, 13 Jan 2012 19:41:25 +0100 |
parents | 87331661042b |
children | 609482c6710e |
files | Doc/library/functions.rst Lib/test/test_print.py Misc/NEWS Python/bltinmodule.c |
diffstat | 4 files changed, 55 insertions(+), 9 deletions(-)[+] [-] Doc/library/functions.rst 11 Lib/test/test_print.py 26 Misc/NEWS 3 Python/bltinmodule.c 24 |
line wrap: on
line diff
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -946,7 +946,7 @@ are always available. They are listed h
must be of integer types, and y must be non-negative.
-.. function:: print([object, ...], *, sep=' ', end='\n', file=sys.stdout)
+.. function:: print([object, ...], *, sep=' ', end='\n', file=sys.stdout, flush=False)
Print object(s) to the stream file, separated by sep and followed by
end. sep, end and file, if present, must be given as keyword
@@ -959,9 +959,12 @@ are always available. They are listed h
end.
The file argument must be an object with a write(string)
method; if it
- is not present or
None
, :data:sys.stdout
will be used. Output buffering - is determined by file. Use
file.flush()
to ensure, for instance, - immediate appearance on a screen.
- is not present or
None
, :data:sys.stdout
will be used. Whether output - is buffered is usually determined by file, but if the flush keyword
- argument is true, the stream is forcibly flushed. +
- .. versionchanged:: 3.3
Added the *flush* keyword argument.[](#l1.24)
.. function:: property(fget=None, fset=None, fdel=None, doc=None)
--- a/Lib/test/test_print.py +++ b/Lib/test/test_print.py @@ -111,6 +111,32 @@ class TestPrint(unittest.TestCase): self.assertRaises(TypeError, print, '', end=3) self.assertRaises(AttributeError, print, '', file='')
- def test_print_flush(self):
# operation of the flush flag[](#l2.8)
class filelike():[](#l2.9)
def __init__(self):[](#l2.10)
self.written = ''[](#l2.11)
self.flushed = 0[](#l2.12)
def write(self, str):[](#l2.13)
self.written += str[](#l2.14)
def flush(self):[](#l2.15)
self.flushed += 1[](#l2.16)
f = filelike()[](#l2.18)
print(1, file=f, end='', flush=True)[](#l2.19)
print(2, file=f, end='', flush=True)[](#l2.20)
print(3, file=f, flush=False)[](#l2.21)
self.assertEqual(f.written, '123\n')[](#l2.22)
self.assertEqual(f.flushed, 2)[](#l2.23)
# ensure exceptions from flush are passed through[](#l2.25)
class noflush():[](#l2.26)
def write(self, str):[](#l2.27)
pass[](#l2.28)
def flush(self):[](#l2.29)
raise RuntimeError[](#l2.30)
self.assertRaises(RuntimeError, print, 1, file=noflush(), flush=True)[](#l2.31)
+ def test_main(): support.run_unittest(TestPrint)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1? Core and Builtins ----------------- +- Issue #13761: Add a "flush" keyword argument to the print() function,
- Issue #13645: pyc files now contain the size of the corresponding source code, to avoid timestamp collisions (especially on filesystems with a low timestamp resolution) when checking for freshness of the bytecode.
--- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1484,15 +1484,15 @@ equivalent to (x**y) % z, but may be mor static PyObject * builtin_print(PyObject *self, PyObject *args, PyObject *kwds) {
- PyObject *sep = NULL, *end = NULL, *file = NULL, *flush = NULL; int i, err; if (dummy_args == NULL && !(dummy_args = PyTuple_New(0)))
return NULL;[](#l4.15)
- if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
kwlist, &sep, &end, &file))[](#l4.17)
return NULL;[](#l4.18)
- if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOOO:print",
if (file == NULL || file == Py_None) { file = PySys_GetObject("stdout");kwlist, &sep, &end, &file, &flush))[](#l4.20) return NULL;[](#l4.21)
@@ -1543,6 +1543,20 @@ builtin_print(PyObject *self, PyObject * if (err) return NULL;
- if (flush != NULL) {
PyObject *tmp;[](#l4.29)
int do_flush = PyObject_IsTrue(flush);[](#l4.30)
if (do_flush == -1)[](#l4.31)
return NULL;[](#l4.32)
else if (do_flush) {[](#l4.33)
tmp = PyObject_CallMethod(file, "flush", "");[](#l4.34)
if (tmp == NULL)[](#l4.35)
return NULL;[](#l4.36)
else[](#l4.37)
Py_DECREF(tmp);[](#l4.38)
}[](#l4.39)
- }