{"record":{"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":"error","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/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/capture.py#L232-L268","documentation":"Raised by DontReadFromInput.flush() when code attempts to flush the captured stdin pseudofile. Since the pseudofile is read-only and backed by nothing, flushing is unsupported and raises UnsupportedOperation. This typically surfaces when generic IO-handling code calls flush() defensively on any stream, including stdin.","triggerScenarios":"Code calls sys.stdin.flush(); a logging or IO helper flushes every file-like object it touches; redirect helpers that flush stdin as part of cleanup.","commonSituations":"Generic stream-management utilities that flush indiscriminately; porting shell-pipeline-style code that flushes all std streams.","solutions":["Run pytest with -s to use the real stdin (which supports flush).","Monkeypatch sys.stdin to a StringIO or real file that supports flush.","Guard flush calls: if hasattr(sys.stdin, 'flush') and sys.stdin is not the captured sentinel."],"exampleFix":"// before\nsys.stdin.flush()  # raises under capture\n// after\nmonkeypatch.setattr('sys.stdin', io.StringIO())\nsys.stdin.flush()","handlingStrategy":"try-catch","validationCode":"def safe_flush(stream):\n    flush = getattr(stream, 'flush', None)\n    if flush is None:\n        return\n    try:\n        flush()\n    except (OSError, ValueError):\n        pass","typeGuard":"def flush_is_supported(stream) -> bool:\n    flush = getattr(stream, 'flush', None)\n    if flush is None:\n        return False\n    try:\n        flush(); return True\n    except Exception:\n        return False","tryCatchPattern":"try:\n    sys.stdin.flush()\nexcept (OSError, ValueError) as e:\n    if 'pseudofile' in str(e):\n        pass  # captured stdin; nothing to flush","preventionTips":["Only flush writable streams; stdin is read-only.","Guard flush() calls in generic IO utilities with try/except."],"tags":["capture","stdin","flush","io"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}