[pypy-commit] pypy py3.3-fixes2: implement flush kwarg for print()
numerodix
noreply at buildbot.pypy.org
Sun Aug 17 19:35:29 CEST 2014
Author: Martin Matusiak <numerodix at gmail.com>
Branch: py3.3-fixes2
Changeset: r72843:c92b637d6a83
Date: 2014-08-17 15:01 +0200
http://bitbucket.org/pypy/pypy/changeset/c92b637d6a83/
Log: implement flush kwarg for print()
diff --git a/pypy/module/__builtin__/app_io.py b/pypy/module/__builtin__/app_io.py
--- a/pypy/module/__builtin__/app_io.py
+++ b/pypy/module/__builtin__/app_io.py
@@ -57,13 +57,14 @@
return line
def print_(*args, **kwargs):
- r"""print(value, ..., sep=' ', end='\n', file=sys.stdout)
+ r"""print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
- file: a file-like object (stream); defaults to the current sys.stdout.
- sep: string inserted between values, default a space.
- end: string appended after the last value, default a newline.
+ file: a file-like object (stream); defaults to the current sys.stdout.
+ sep: string inserted between values, default a space.
+ end: string appended after the last value, default a newline.
+ flush: whether to forcibly flush the stream.
"""
fp = kwargs.pop("file", None)
if fp is None:
@@ -80,6 +81,7 @@
if end is not None:
if not isinstance(end, str):
raise TypeError("end must be None or a string")
+ flush = kwargs.pop('flush', None)
if kwargs:
raise TypeError("invalid keyword arguments to print()")
if sep is None:
@@ -91,3 +93,5 @@
write(sep)
write(arg)
write(end)
+ if flush:
+ fp.flush()
diff --git a/pypy/module/__builtin__/test/test_print.py b/pypy/module/__builtin__/test/test_print.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/__builtin__/test/test_print.py
@@ -0,0 +1,29 @@
+class AppTestPrint:
+
+ 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)
+ assert f.written == '123\\n'
+ assert f.flushed == 2
+
+ # ensure exceptions from flush are passed through
+ class noflush():
+ def write(self, str):
+ pass
+ def flush(self):
+ raise RuntimeError
+ raises(RuntimeError, print, 1, file=noflush(), flush=True)
+ """
More information about the pypy-commit
mailing list