From 1c31a7e6598ff0290012f56c4ee6ce48a31486d9 Mon Sep 17 00:00:00 2001 From: Robert O'Shea Date: Sun, 31 Jul 2022 14:44:02 +0100 Subject: [PATCH] Added file-like methods to DontReadFromInput (#10173) Co-authored-by: Bruno Oliveira Fixes #10150 --- changelog/10150.bugfix.rst | 1 + src/_pytest/capture.py | 27 +++++++++++++++++++++++++++ testing/test_capture.py | 9 +++++++++ 3 files changed, 37 insertions(+) create mode 100644 changelog/10150.bugfix.rst diff --git a/changelog/10150.bugfix.rst b/changelog/10150.bugfix.rst new file mode 100644 index 000000000..24c7ccd35 --- /dev/null +++ b/changelog/10150.bugfix.rst @@ -0,0 +1 @@ +:data:`sys.stdin` now contains all expected methods of a file-like object when capture is enabled. diff --git a/src/_pytest/capture.py b/src/_pytest/capture.py index d6cf42c3b..bcfd3449f 100644 --- a/src/_pytest/capture.py +++ b/src/_pytest/capture.py @@ -203,12 +203,39 @@ class DontReadFromInput: def fileno(self) -> int: 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: return False def close(self) -> None: 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 def buffer(self): return self diff --git a/testing/test_capture.py b/testing/test_capture.py index f0c582b66..00cab1933 100644 --- a/testing/test_capture.py +++ b/testing/test_capture.py @@ -897,6 +897,15 @@ def test_dontreadfrominput() -> None: iter_f = iter(f) pytest.raises(OSError, next, iter_f) 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