diff --git a/AUTHORS b/AUTHORS index 2e487e77226..8bbb98d9cea 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 d47d3810dfc..447b03b9422 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 d58c7290985..9f60db6ac07 100644 --- a/_pytest/capture.py +++ b/_pytest/capture.py @@ -455,6 +455,13 @@ def isatty(self): 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 b2e5be0bef1..cbb5fc81bb7 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+')