{"record":{"id":"4ba3d5aac88b9b59","repo":"pytest-dev/pytest","slug":"cannot-write-to-stdin","errorCode":null,"errorMessage":"cannot write to stdin","messagePattern":"cannot write to stdin","errorType":"exception","errorClass":"UnsupportedOperation","httpStatus":null,"severity":"error","filePath":"src/_pytest/capture.py","lineNumber":274,"sourceCode":"        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()\")\n\n    def truncate(self, size: int | None = None) -> int:\n        raise UnsupportedOperation(\"cannot truncate stdin\")\n\n    def write(self, data: str) -> int:\n        raise UnsupportedOperation(\"cannot write to stdin\")\n\n    def writelines(self, lines: Iterable[str]) -> None:\n        raise UnsupportedOperation(\"Cannot write to stdin\")\n\n    def writable(self) -> bool:\n        return False\n\n    def __enter__(self) -> Self:\n        return self\n\n    def __exit__(\n        self,\n        type: type[BaseException] | None,\n        value: BaseException | None,\n        traceback: TracebackType | None,\n    ) -> None:\n        pass\n","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/capture.py#L256-L292","documentation":"Raised by DontReadFromInput.write() (src/_pytest/capture.py:273). Writing to stdin is semantically wrong; the redirected pseudo-file is read-only and writable() at capture.py:279 returns False. pytest raises UnsupportedOperation (lowercase 'cannot') to surface the misuse rather than silently drop data.","triggerScenarios":"Calling sys.stdin.write(data) in a test while default capture is active. capture.py:273-274 raises unconditionally; writable() returns False at capture.py:279 so callers that probe first are safe.","commonSituations":"Misimplemented stream multiplexers, mock/patch code that writes to 'all standard streams', or test scaffolding that mistakenly writes to sys.stdin instead of sys.stdout. Less common than read/seek errors.","solutions":["Route the write to the correct stream (sys.stdout or sys.stderr) instead of sys.stdin","Guard with `if sys.stdin.writable():` to short-circuit on the pseudo-file","Disable capture with -s if the write is intentional in your test harness","try/except OSError and ignore the dropped write"],"exampleFix":"// before\nsys.stdin.write('data')\n// after\nsys.stdout.write('data')","handlingStrategy":"type-guard","validationCode":"import sys\nif not sys.stdin.writable():\n    raise ValueError('refusing to write to read-only stdin')","typeGuard":"def is_writable(stream) -> bool:\n    try:\n        return stream.writable()\n    except Exception:\n        return False","tryCatchPattern":"from io import UnsupportedOperation\ntry:\n    sys.stdin.write(data)\nexcept UnsupportedOperation:\n    # Redirect instead\n    sys.stdout.write(data)","preventionTips":["Never write to sys.stdin - it is conceptually read-only","Stream-multiplexing helpers should check writable() before dispatching writes","Use sys.stdout/sys.stderr for diagnostic output"],"tags":["capture","stdin","io","unsupported-operation"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}