{"record":{"id":"09187d96829c4935","repo":"pytest-dev/pytest","slug":"pytest-reading-from-stdin-while-output-is-capture","errorCode":null,"errorMessage":"pytest: reading from stdin while output is captured!  Consider using the `-s`.","messagePattern":"pytest: reading from stdin while output is captured!  Consider using the `-s`\\.","errorType":"exception","errorClass":"OSError","httpStatus":null,"severity":"error","filePath":"src/_pytest/capture.py","lineNumber":229,"sourceCode":"\nclass TeeCaptureIO(CaptureIO):\n    def __init__(self, other: TextIO) -> None:\n        self._other = other\n        super().__init__()\n\n    def write(self, s: str) -> int:\n        super().write(s)\n        return self._other.write(s)\n\n\nclass DontReadFromInput(TextIO):\n    @property\n    def encoding(self) -> str:\n        assert sys.__stdin__ is not None\n        return sys.__stdin__.encoding\n\n    def read(self, size: int = -1) -> str:\n        raise OSError(\n            \"pytest: reading from stdin while output is captured!  Consider using `-s`.\"\n        )\n\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()\")","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/capture.py#L211-L247","documentation":"Raised by DontReadFromInput.read() when test code (or code under test) attempts to read from sys.stdin while pytest's standard input capture is active. pytest replaces sys.stdin with a DontReadFromInput sentinel during capture to surface accidental stdin reads, which would otherwise hang or read garbage. The message suggests the -s option to disable capture.","triggerScenarios":"Code under test calls input() or sys.stdin.read(); a fixture reads stdin; interactive prompts invoked in non-interactive runs.","commonSituations":"Forgetting to mock input() in tests; CLI code that prompts unconditionally; running tests that exercise interactive flows without -s.","solutions":["Run pytest with -s (or --capture=no) to pass stdin through.","Mock input() / sys.stdin in the test using monkeypatch.setattr('sys.stdin', io.StringIO(...)).","Refactor code under test to accept input via a parameter rather than reading stdin directly."],"exampleFix":"// before\ndef test_prompt():\n    run_cli()  # calls input()\n// after\ndef test_prompt(monkeypatch):\n    monkeypatch.setattr('sys.stdin', io.StringIO('yes\\n'))\n    run_cli()","handlingStrategy":"validation","validationCode":"import io, sys\ndef with_fake_stdin(monkeypatch, text=''):\n    monkeypatch.setattr('sys.stdin', io.StringIO(text))","typeGuard":"def stdin_is_real() -> bool:\n    return hasattr(sys.stdin, 'fileno') and sys.stdin.__class__.__name__ != 'DontReadFromInput'","tryCatchPattern":"try:\n    data = sys.stdin.read()\nexcept OSError as e:\n    if 'reading from stdin' in str(e):\n        data = ''  # or re-run with -s","preventionTips":["Mock input() and sys.stdin in tests that touch interactive code.","Run with -s when interactive input is genuinely needed."],"tags":["capture","stdin","io","test-isolation"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}