diff -r fb0d61fd1753 Doc/library/functions.rst --- a/Doc/library/functions.rst Wed Jan 11 14:14:14 2012 -0500 +++ b/Doc/library/functions.rst Wed Jan 11 20:53:06 2012 +0100 @@ -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 @@ -960,8 +960,11 @@ are always available. They are listed h 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 ``sys.stdout.flush()`` to ensure immediate - appearance on a screen. + is usually determined by *file*, but if the *flush* keyword argument is true, + the stream is forcibly flushed. + + .. versionchanged:: 3.3 + Added the *flush* parameter. .. function:: property(fget=None, fset=None, fdel=None, doc=None) diff -r fb0d61fd1753 Lib/test/test_print.py --- a/Lib/test/test_print.py Wed Jan 11 14:14:14 2012 -0500 +++ b/Lib/test/test_print.py Wed Jan 11 20:53:06 2012 +0100 @@ -111,6 +111,24 @@ 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 + class filelike(): + def __init__(self): + self.written = '' + self.flushed = 0 + def write(self, str): + self.written += str + def flush(self): + self.flushed += 1 + + f = filelike() + print(1, file=f, end='', flush=True) + print(2, file=f, end='', flush=True) + print(3, file=f, flush=False) + self.assertEquals(f.written, '123\n') + self.assertEquals(f.flushed, 2) + def test_main(): support.run_unittest(TestPrint) diff -r fb0d61fd1753 Python/bltinmodule.c --- a/Python/bltinmodule.c Wed Jan 11 14:14:14 2012 -0500 +++ b/Python/bltinmodule.c Wed Jan 11 20:53:06 2012 +0100 @@ -1484,17 +1484,17 @@ equivalent to (x**y) % z, but may be mor static PyObject * builtin_print(PyObject *self, PyObject *args, PyObject *kwds) { - static char *kwlist[] = {"sep", "end", "file", 0}; + static char *kwlist[] = {"sep", "end", "file", "flush", 0}; static PyObject *dummy_args; - PyObject *sep = NULL, *end = NULL, *file = NULL; + PyObject *sep = NULL, *end = NULL, *file = NULL, *flush = NULL; int i, err; if (dummy_args == NULL) { if (!(dummy_args = PyTuple_New(0))) return NULL; } - if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print", - kwlist, &sep, &end, &file)) + if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOOO:print", + kwlist, &sep, &end, &file, &flush)) return NULL; if (file == NULL || file == Py_None) { file = PySys_GetObject("stdout"); @@ -1545,6 +1545,20 @@ builtin_print(PyObject *self, PyObject * if (err) return NULL; + if (flush != NULL) { + PyObject *tmp; + int do_flush = PyObject_IsTrue(flush); + if (do_flush == -1) + return NULL; + else if (do_flush) { + tmp = PyObject_CallMethod(file, "flush", ""); + if (tmp == NULL) + return NULL; + else + Py_DECREF(tmp); + } + } + Py_RETURN_NONE; }