Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bc3b682

Browse files
committed
Closes #13761: add a "flush" keyword argument to print().
1 parent 5136ac0 commit bc3b682

4 files changed

Lines changed: 55 additions & 9 deletions

File tree

Doc/library/functions.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ are always available. They are listed here in alphabetical order.
946946
must be of integer types, and *y* must be non-negative.
947947

948948

949-
.. function:: print([object, ...], *, sep=' ', end='\\n', file=sys.stdout)
949+
.. function:: print([object, ...], *, sep=' ', end='\\n', file=sys.stdout, flush=False)
950950

951951
Print *object*\(s) to the stream *file*, separated by *sep* and followed by
952952
*end*. *sep*, *end* and *file*, if present, must be given as keyword
@@ -959,9 +959,12 @@ are always available. They are listed here in alphabetical order.
959959
*end*.
960960

961961
The *file* argument must be an object with a ``write(string)`` method; if it
962-
is not present or ``None``, :data:`sys.stdout` will be used. Output buffering
963-
is determined by *file*. Use ``file.flush()`` to ensure, for instance,
964-
immediate appearance on a screen.
962+
is not present or ``None``, :data:`sys.stdout` will be used. Whether output
963+
is buffered is usually determined by *file*, but if the *flush* keyword
964+
argument is true, the stream is forcibly flushed.
965+
966+
.. versionchanged:: 3.3
967+
Added the *flush* keyword argument.
965968

966969

967970
.. function:: property(fget=None, fset=None, fdel=None, doc=None)

Lib/test/test_print.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,32 @@ def x(expected, args, sep=NotDefined, end=NotDefined):
111111
self.assertRaises(TypeError, print, '', end=3)
112112
self.assertRaises(AttributeError, print, '', file='')
113113

114+
def test_print_flush(self):
115+
# operation of the flush flag
116+
class filelike():
117+
def __init__(self):
118+
self.written = ''
119+
self.flushed = 0
120+
def write(self, str):
121+
self.written += str
122+
def flush(self):
123+
self.flushed += 1
124+
125+
f = filelike()
126+
print(1, file=f, end='', flush=True)
127+
print(2, file=f, end='', flush=True)
128+
print(3, file=f, flush=False)
129+
self.assertEqual(f.written, '123\n')
130+
self.assertEqual(f.flushed, 2)
131+
132+
# ensure exceptions from flush are passed through
133+
class noflush():
134+
def write(self, str):
135+
pass
136+
def flush(self):
137+
raise RuntimeError
138+
self.assertRaises(RuntimeError, print, 1, file=noflush(), flush=True)
139+
114140
def test_main():
115141
support.run_unittest(TestPrint)
116142

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #13761: Add a "flush" keyword argument to the print() function,
14+
used to ensure flushing the output stream.
15+
1316
- Issue #13645: pyc files now contain the size of the corresponding source
1417
code, to avoid timestamp collisions (especially on filesystems with a low
1518
timestamp resolution) when checking for freshness of the bytecode.

Python/bltinmodule.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,15 +1484,15 @@ equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
14841484
static PyObject *
14851485
builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
14861486
{
1487-
static char *kwlist[] = {"sep", "end", "file", 0};
1487+
static char *kwlist[] = {"sep", "end", "file", "flush", 0};
14881488
static PyObject *dummy_args;
1489-
PyObject *sep = NULL, *end = NULL, *file = NULL;
1489+
PyObject *sep = NULL, *end = NULL, *file = NULL, *flush = NULL;
14901490
int i, err;
14911491

14921492
if (dummy_args == NULL && !(dummy_args = PyTuple_New(0)))
1493-
return NULL;
1494-
if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
1495-
kwlist, &sep, &end, &file))
1493+
return NULL;
1494+
if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOOO:print",
1495+
kwlist, &sep, &end, &file, &flush))
14961496
return NULL;
14971497
if (file == NULL || file == Py_None) {
14981498
file = PySys_GetObject("stdout");
@@ -1543,6 +1543,20 @@ builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
15431543
if (err)
15441544
return NULL;
15451545

1546+
if (flush != NULL) {
1547+
PyObject *tmp;
1548+
int do_flush = PyObject_IsTrue(flush);
1549+
if (do_flush == -1)
1550+
return NULL;
1551+
else if (do_flush) {
1552+
tmp = PyObject_CallMethod(file, "flush", "");
1553+
if (tmp == NULL)
1554+
return NULL;
1555+
else
1556+
Py_DECREF(tmp);
1557+
}
1558+
}
1559+
15461560
Py_RETURN_NONE;
15471561
}
15481562

0 commit comments

Comments
 (0)