Merge pull request #3315 from pytest-dev/issue/3314

Allow DontReadFromInput to produce iterator without error.
This commit is contained in:
Bruno Oliveira 2018-03-16 10:05:54 -03:00 committed by GitHub
commit 93847bfeb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 4 deletions

View File

@ -560,7 +560,7 @@ class SysCaptureBinary(SysCapture):
return res return res
class DontReadFromInput(object): class DontReadFromInput(six.Iterator):
"""Temporary stub class. Ideally when stdin is accessed, the """Temporary stub class. Ideally when stdin is accessed, the
capturing should be turned off, with possibly all data captured capturing should be turned off, with possibly all data captured
so far sent to the screen. This should be configurable, though, so far sent to the screen. This should be configurable, though,
@ -574,7 +574,10 @@ class DontReadFromInput(object):
raise IOError("reading from stdin while output is captured") raise IOError("reading from stdin while output is captured")
readline = read readline = read
readlines = read readlines = read
__iter__ = read __next__ = read
def __iter__(self):
return self
def fileno(self): def fileno(self):
raise UnsupportedOperation("redirected stdin is pseudofile, " raise UnsupportedOperation("redirected stdin is pseudofile, "

View File

@ -0,0 +1,3 @@
During test collection, when stdin is not allowed to be read, the
``DontReadFromStdin`` object still allow itself to be iterable and
resolved to an iterator without crashing.

View File

@ -751,7 +751,8 @@ def test_dontreadfrominput():
assert not f.isatty() assert not f.isatty()
pytest.raises(IOError, f.read) pytest.raises(IOError, f.read)
pytest.raises(IOError, f.readlines) pytest.raises(IOError, f.readlines)
pytest.raises(IOError, iter, f) iter_f = iter(f)
pytest.raises(IOError, next, iter_f)
pytest.raises(UnsupportedOperation, f.fileno) pytest.raises(UnsupportedOperation, f.fileno)
f.close() # just for completeness f.close() # just for completeness
@ -764,7 +765,8 @@ def test_dontreadfrominput_buffer_python3():
assert not fb.isatty() assert not fb.isatty()
pytest.raises(IOError, fb.read) pytest.raises(IOError, fb.read)
pytest.raises(IOError, fb.readlines) pytest.raises(IOError, fb.readlines)
pytest.raises(IOError, iter, fb) iter_f = iter(f)
pytest.raises(IOError, next, iter_f)
pytest.raises(ValueError, fb.fileno) pytest.raises(ValueError, fb.fileno)
f.close() # just for completeness f.close() # just for completeness