Skip to content

Commit 1c31a7e

Browse files
authored
Added file-like methods to DontReadFromInput (#10173)
Co-authored-by: Bruno Oliveira <[email protected]> Fixes #10150
1 parent b4ab2f0 commit 1c31a7e

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

changelog/10150.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:data:`sys.stdin` now contains all expected methods of a file-like object when capture is enabled.

src/_pytest/capture.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,39 @@ def __iter__(self):
203203
def fileno(self) -> int:
204204
raise UnsupportedOperation("redirected stdin is pseudofile, has no fileno()")
205205

206+
def flush(self) -> None:
207+
raise UnsupportedOperation("redirected stdin is pseudofile, has no flush()")
208+
206209
def isatty(self) -> bool:
207210
return False
208211

209212
def close(self) -> None:
210213
pass
211214

215+
def readable(self) -> bool:
216+
return False
217+
218+
def seek(self, offset: int) -> int:
219+
raise UnsupportedOperation("redirected stdin is pseudofile, has no seek(int)")
220+
221+
def seekable(self) -> bool:
222+
return False
223+
224+
def tell(self) -> int:
225+
raise UnsupportedOperation("redirected stdin is pseudofile, has no tell()")
226+
227+
def truncate(self, size: int) -> None:
228+
raise UnsupportedOperation("cannont truncate stdin")
229+
230+
def write(self, *args) -> None:
231+
raise UnsupportedOperation("cannot write to stdin")
232+
233+
def writelines(self, *args) -> None:
234+
raise UnsupportedOperation("Cannot write to stdin")
235+
236+
def writable(self) -> bool:
237+
return False
238+
212239
@property
213240
def buffer(self):
214241
return self

testing/test_capture.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,15 @@ def test_dontreadfrominput() -> None:
897897
iter_f = iter(f)
898898
pytest.raises(OSError, next, iter_f)
899899
pytest.raises(UnsupportedOperation, f.fileno)
900+
pytest.raises(UnsupportedOperation, f.flush)
901+
assert not f.readable()
902+
pytest.raises(UnsupportedOperation, f.seek, 0)
903+
assert not f.seekable()
904+
pytest.raises(UnsupportedOperation, f.tell)
905+
pytest.raises(UnsupportedOperation, f.truncate, 0)
906+
pytest.raises(UnsupportedOperation, f.write, b"")
907+
pytest.raises(UnsupportedOperation, f.writelines, [])
908+
assert not f.writable()
900909
f.close() # just for completeness
901910

902911

0 commit comments

Comments
 (0)