Added file-like methods to DontReadFromInput (#10173)

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>

Fixes #10150
This commit is contained in:
Robert O'Shea 2022-07-31 14:44:02 +01:00 committed by GitHub
parent b4ab2f0942
commit 1c31a7e659
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1 @@
:data:`sys.stdin` now contains all expected methods of a file-like object when capture is enabled.

View File

@ -203,12 +203,39 @@ class DontReadFromInput:
def fileno(self) -> int: def fileno(self) -> int:
raise UnsupportedOperation("redirected stdin is pseudofile, has no fileno()") raise UnsupportedOperation("redirected stdin is pseudofile, has no fileno()")
def flush(self) -> None:
raise UnsupportedOperation("redirected stdin is pseudofile, has no flush()")
def isatty(self) -> bool: def isatty(self) -> bool:
return False return False
def close(self) -> None: def close(self) -> None:
pass pass
def readable(self) -> bool:
return False
def seek(self, offset: int) -> int:
raise UnsupportedOperation("redirected stdin is pseudofile, has no seek(int)")
def seekable(self) -> bool:
return False
def tell(self) -> int:
raise UnsupportedOperation("redirected stdin is pseudofile, has no tell()")
def truncate(self, size: int) -> None:
raise UnsupportedOperation("cannont truncate stdin")
def write(self, *args) -> None:
raise UnsupportedOperation("cannot write to stdin")
def writelines(self, *args) -> None:
raise UnsupportedOperation("Cannot write to stdin")
def writable(self) -> bool:
return False
@property @property
def buffer(self): def buffer(self):
return self return self

View File

@ -897,6 +897,15 @@ def test_dontreadfrominput() -> None:
iter_f = iter(f) iter_f = iter(f)
pytest.raises(OSError, next, iter_f) pytest.raises(OSError, next, iter_f)
pytest.raises(UnsupportedOperation, f.fileno) pytest.raises(UnsupportedOperation, f.fileno)
pytest.raises(UnsupportedOperation, f.flush)
assert not f.readable()
pytest.raises(UnsupportedOperation, f.seek, 0)
assert not f.seekable()
pytest.raises(UnsupportedOperation, f.tell)
pytest.raises(UnsupportedOperation, f.truncate, 0)
pytest.raises(UnsupportedOperation, f.write, b"")
pytest.raises(UnsupportedOperation, f.writelines, [])
assert not f.writable()
f.close() # just for completeness f.close() # just for completeness