{"id":"c6e43083aa0a5c1d","repo":"pytest-dev/pytest","slug":"redirected-stdin-is-pseudofile-has-no-flush","errorCode":null,"errorMessage":"redirected stdin is pseudofile, has no flush()","messagePattern":"redirected stdin is pseudofile, has no flush\\(\\)","errorType":"exception","errorClass":"UnsupportedOperation","httpStatus":null,"severity":"warning","filePath":"src/_pytest/capture.py","lineNumber":250,"sourceCode":"\n    readline = read\n\n    def __next__(self) -> str:\n        return self.readline()\n\n    def readlines(self, hint: int | None = -1) -> list[str]:\n        raise OSError(\n            \"pytest: reading from stdin while output is captured!  Consider using `-s`.\"\n        )\n\n    def __iter__(self) -> Iterator[str]:\n        return self\n\n    def fileno(self) -> int:\n        raise UnsupportedOperation(\"redirected stdin is pseudofile, has no fileno()\")\n\n    def flush(self) -> None:\n        raise UnsupportedOperation(\"redirected stdin is pseudofile, has no flush()\")\n\n    def isatty(self) -> bool:\n        return False\n\n    def close(self) -> None:\n        pass\n\n    def readable(self) -> bool:\n        return False\n\n    def seek(self, offset: int, whence: int = 0) -> int:\n        raise UnsupportedOperation(\"redirected stdin is pseudofile, has no seek(int)\")\n\n    def seekable(self) -> bool:\n        return False\n\n    def tell(self) -> int:\n        raise UnsupportedOperation(\"redirected stdin is pseudofile, has no tell()\")","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/capture.py#L232-L268","documentation":"DontReadFromInput.flush() raises UnsupportedOperation because the captured stdin pseudo-file is read-only and backed by an in-memory buffer; flushing it has no meaning.","triggerScenarios":"In a test under default capture, code calls sys.stdin.flush() — often libraries that defensively flush all standard streams.","commonSituations":"A logging or progress-bar library that iterates sys.std{in,out,err} and calls .flush() on each; code copied from a context that mixed up stdin with stdout.","solutions":["Run pytest with -s.","Refactor the code to flush only stdout/stderr, not stdin.","Guard the flush call with a check like getattr(stream, 'writable', lambda: True)() or skip streams where writable() is False."],"exampleFix":"// before\n# code: for s in (sys.stdin, sys.stdout, sys.stderr): s.flush()\n// after\n# code: for s in (sys.stdout, sys.stderr): s.flush()","handlingStrategy":"try-catch","validationCode":"def safe_flush(stream):\n    if getattr(stream, 'writable', lambda: False)():\n        stream.flush()","typeGuard":"def is_writable(stream) -> bool:\n    return bool(getattr(stream, 'writable', lambda: False)())","tryCatchPattern":"from io import UnsupportedOperation\ntry:\n    stream.flush()\nexcept (OSError, UnsupportedOperation, ValueError):\n    pass","preventionTips":["Only flush streams you opened, or those whose writable() returns True.","Run pytest with -s if a third-party library must flush all standard streams."],"tags":["pytest","capture","stdin","io"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}