Skip to content

Commit 93847bf

Browse files
authored
Merge pull request #3315 from pytest-dev/issue/3314
Allow DontReadFromInput to produce iterator without error.
2 parents 278d8ac + 17468fc commit 93847bf

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

_pytest/capture.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ def snap(self):
560560
return res
561561

562562

563-
class DontReadFromInput(object):
563+
class DontReadFromInput(six.Iterator):
564564
"""Temporary stub class. Ideally when stdin is accessed, the
565565
capturing should be turned off, with possibly all data captured
566566
so far sent to the screen. This should be configurable, though,
@@ -574,7 +574,10 @@ def read(self, *args):
574574
raise IOError("reading from stdin while output is captured")
575575
readline = read
576576
readlines = read
577-
__iter__ = read
577+
__next__ = read
578+
579+
def __iter__(self):
580+
return self
578581

579582
def fileno(self):
580583
raise UnsupportedOperation("redirected stdin is pseudofile, "

changelog/3314.bugfix.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
During test collection, when stdin is not allowed to be read, the
2+
``DontReadFromStdin`` object still allow itself to be iterable and
3+
resolved to an iterator without crashing.

testing/test_capture.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,8 @@ def test_dontreadfrominput():
751751
assert not f.isatty()
752752
pytest.raises(IOError, f.read)
753753
pytest.raises(IOError, f.readlines)
754-
pytest.raises(IOError, iter, f)
754+
iter_f = iter(f)
755+
pytest.raises(IOError, next, iter_f)
755756
pytest.raises(UnsupportedOperation, f.fileno)
756757
f.close() # just for completeness
757758

@@ -764,7 +765,8 @@ def test_dontreadfrominput_buffer_python3():
764765
assert not fb.isatty()
765766
pytest.raises(IOError, fb.read)
766767
pytest.raises(IOError, fb.readlines)
767-
pytest.raises(IOError, iter, fb)
768+
iter_f = iter(f)
769+
pytest.raises(IOError, next, iter_f)
768770
pytest.raises(ValueError, fb.fileno)
769771
f.close() # just for completeness
770772

0 commit comments

Comments
 (0)