Skip to content

Commit 9c45d6c

Browse files
Merge pull request #1866 from joguSD/bugfix-stdin-stub-buffer
Add buffer attribute to DontReadFromInput
2 parents 3345ac9 + a152ea2 commit 9c45d6c

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Javier Domingo Cansino
6969
Javier Romero
7070
John Towler
7171
Jon Sonesen
72+
Jordan Guymon
7273
Joshua Bronson
7374
Jurko Gospodnetić
7475
Justyna Janczyszyn

CHANGELOG.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
* Improve error message when passing non-string ids to ``pytest.mark.parametrize`` (`#1857`_).
77
Thanks `@okken`_ for the report and `@nicoddemus`_ for the PR.
88

9-
*
9+
* Add ``buffer`` attribute to stdin stub class ``pytest.capture.DontReadFromInput``
10+
Thanks `@joguSD`_ for the PR.
1011

1112
*
1213

14+
.. _@joguSD: https://github.com/joguSD
1315

1416
.. _#1857: https://github.com/pytest-dev/pytest/issues/1857
1517

_pytest/capture.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,13 @@ def isatty(self):
455455
def close(self):
456456
pass
457457

458+
@property
459+
def buffer(self):
460+
if sys.version_info >= (3,0):
461+
return self
462+
else:
463+
raise AttributeError('redirected stdin has no attribute buffer')
464+
458465

459466
def _readline_workaround():
460467
"""

testing/test_capture.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,28 @@ def test_dontreadfrominput():
662662
f.close() # just for completeness
663663

664664

665+
@pytest.mark.skipif('sys.version_info < (3,)', reason='python2 has no buffer')
666+
def test_dontreadfrominput_buffer_python3():
667+
from _pytest.capture import DontReadFromInput
668+
f = DontReadFromInput()
669+
fb = f.buffer
670+
assert not fb.isatty()
671+
pytest.raises(IOError, fb.read)
672+
pytest.raises(IOError, fb.readlines)
673+
pytest.raises(IOError, iter, fb)
674+
pytest.raises(ValueError, fb.fileno)
675+
f.close() # just for completeness
676+
677+
678+
@pytest.mark.skipif('sys.version_info >= (3,)', reason='python2 has no buffer')
679+
def test_dontreadfrominput_buffer_python2():
680+
from _pytest.capture import DontReadFromInput
681+
f = DontReadFromInput()
682+
with pytest.raises(AttributeError):
683+
f.buffer
684+
f.close() # just for completeness
685+
686+
665687
@pytest.yield_fixture
666688
def tmpfile(testdir):
667689
f = testdir.makepyfile("").open('wb+')

0 commit comments

Comments
 (0)