diff options
author | Benjamin Peterson <benjamin@python.org> | 2015-01-02 10:20:40 -0600 |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2015-01-02 10:20:40 -0600 |
commit | 1ba12e99db60c3d5703f72b20e4d3513b375414a (patch) | |
tree | 5a2b6cb8c8d569ff50ef252b6deac865c3b5422d | |
parent | 23f2567ee94d0a1eebdd8afe6f11140815fcf094 (diff) | |
download | six-1ba12e99db60c3d5703f72b20e4d3513b375414a.tar.gz |
support the flush parameter of print_ (fixes #106)
-rw-r--r-- | CHANGES | 2 | ||||
-rw-r--r-- | documentation/index.rst | 5 | ||||
-rw-r--r-- | six.py | 8 | ||||
-rw-r--r-- | test_six.py | 11 |
4 files changed, 24 insertions, 2 deletions
@@ -6,6 +6,8 @@ This file lists the changes in each six version. Development version ------------------- +- Issue #106: Support the `flush` parameter to `six.print_`. + - Pull request #48 and issue #15: Add the `python_2_unicode_compatible` decorator. diff --git a/documentation/index.rst b/documentation/index.rst index 627862a..1f27400 100644 --- a/documentation/index.rst +++ b/documentation/index.rst @@ -270,10 +270,11 @@ Python 2 and 3. :func:`exec` with them should be avoided. -.. function:: print_(*args, *, file=sys.stdout, end="\\n", sep=" ") +.. function:: print_(*args, *, file=sys.stdout, end="\\n", sep=" ", flush=False) Print *args* into *file*. Each argument will be separated with *sep* and - *end* will be written to the file after the last argument is printed. + *end* will be written to the file after the last argument is printed. If + *flush* is true, ``file.flush()`` will be called after all data is written. .. note:: @@ -740,6 +740,14 @@ if print_ is None: write(sep) write(arg) write(end) +if sys.version_info[:2] < (3, 3): + _print = print_ + def print_(*args, **kwargs): + fp = kwargs.get("file", sys.stdout) + flush = kwargs.pop("flush", False) + _print(*args, **kwargs) + if flush and fp is not None: + fp.flush() _add_doc(reraise, """Reraise an exception.""") diff --git a/test_six.py b/test_six.py index 3a3faff..76a8ccb 100644 --- a/test_six.py +++ b/test_six.py @@ -636,6 +636,17 @@ def test_print_(): out = six.StringIO() six.print_(None, file=out) assert out.getvalue() == "None\n" + class FlushableStringIO(six.StringIO): + def __init__(self): + six.StringIO.__init__(self) + self.flushed = False + def flush(self): + self.flushed = True + out = FlushableStringIO() + six.print_("Hello", file=out) + assert not out.flushed + six.print_("Hello", file=out, flush=True) + assert out.flushed @py.test.mark.skipif("sys.version_info[:2] >= (2, 6)") |