From a152ea2dbbebb3de0f79310159bf060af07ce470 Mon Sep 17 00:00:00 2001 From: Jordan Guymon Date: Wed, 24 Aug 2016 15:44:43 -0700 Subject: [PATCH] Add buffer attribute to stdin stub --- AUTHORS | 1 + CHANGELOG.rst | 4 +++- _pytest/capture.py | 7 +++++++ testing/test_capture.py | 22 ++++++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 2e487e772..8bbb98d9c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -69,6 +69,7 @@ Javier Domingo Cansino Javier Romero John Towler Jon Sonesen +Jordan Guymon Joshua Bronson Jurko Gospodnetić Justyna Janczyszyn diff --git a/CHANGELOG.rst b/CHANGELOG.rst index d47d3810d..447b03b94 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,10 +6,12 @@ * Improve error message when passing non-string ids to ``pytest.mark.parametrize`` (`#1857`_). Thanks `@okken`_ for the report and `@nicoddemus`_ for the PR. -* +* Add ``buffer`` attribute to stdin stub class ``pytest.capture.DontReadFromInput`` + Thanks `@joguSD`_ for the PR. * +.. _@joguSD: https://github.com/joguSD .. _#1857: https://github.com/pytest-dev/pytest/issues/1857 diff --git a/_pytest/capture.py b/_pytest/capture.py index d58c72909..9f60db6ac 100644 --- a/_pytest/capture.py +++ b/_pytest/capture.py @@ -455,6 +455,13 @@ class DontReadFromInput: def close(self): pass + @property + def buffer(self): + if sys.version_info >= (3,0): + return self + else: + raise AttributeError('redirected stdin has no attribute buffer') + def _readline_workaround(): """ diff --git a/testing/test_capture.py b/testing/test_capture.py index b2e5be0be..cbb5fc81b 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -662,6 +662,28 @@ def test_dontreadfrominput(): f.close() # just for completeness +@pytest.mark.skipif('sys.version_info < (3,)', reason='python2 has no buffer') +def test_dontreadfrominput_buffer_python3(): + from _pytest.capture import DontReadFromInput + f = DontReadFromInput() + fb = f.buffer + assert not fb.isatty() + pytest.raises(IOError, fb.read) + pytest.raises(IOError, fb.readlines) + pytest.raises(IOError, iter, fb) + pytest.raises(ValueError, fb.fileno) + f.close() # just for completeness + + +@pytest.mark.skipif('sys.version_info >= (3,)', reason='python2 has no buffer') +def test_dontreadfrominput_buffer_python2(): + from _pytest.capture import DontReadFromInput + f = DontReadFromInput() + with pytest.raises(AttributeError): + f.buffer + f.close() # just for completeness + + @pytest.yield_fixture def tmpfile(testdir): f = testdir.makepyfile("").open('wb+')