{"record":{"id":"61d69415e507f75d","repo":"python/cpython","slug":"unknown-mode-r","errorCode":null,"errorMessage":"unknown mode: %r","messagePattern":"unknown mode: %r","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/_pyio.py","lineNumber":261,"sourceCode":"        if buffering == 1 or buffering < 0 and raw._isatty_open_only():\n            buffering = -1\n            line_buffering = True\n        if buffering < 0:\n            buffering = max(min(raw._blksize, 8192 * 1024), DEFAULT_BUFFER_SIZE)\n        if buffering < 0:\n            raise ValueError(\"invalid buffering size\")\n        if buffering == 0:\n            if binary:\n                return result\n            raise ValueError(\"can't have unbuffered text I/O\")\n        if updating:\n            buffer = BufferedRandom(raw, buffering)\n        elif creating or writing or appending:\n            buffer = BufferedWriter(raw, buffering)\n        elif reading:\n            buffer = BufferedReader(raw, buffering)\n        else:\n            raise ValueError(\"unknown mode: %r\" % mode)\n        result = buffer\n        if binary:\n            return result\n        encoding = text_encoding(encoding)\n        text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering)\n        result = text\n        text.mode = mode\n        return result\n    except:\n        result.close()\n        raise\n\n# Define a default pure-Python implementation for open_code()\n# that does not allow hooks. Warn on first use. Defined for tests.\ndef _open_code_with_warning(path):\n    \"\"\"Opens the provided file with mode ``'rb'``. This function\n    should be used when the intent is to treat the contents as\n    executable code.","sourceCodeStart":243,"sourceCodeEnd":279,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pyio.py#L243-L279","documentation":"Raised by io.open() as a ValueError ('unknown mode') when, after building the buffered layer, none of updating/creating/writing/appending/reading matched a buffer class. Earlier validation already requires exactly one of r/w/a/x, so through the public open() this branch is dead code — a defensive assertion against internal inconsistency or patched behavior.","triggerScenarios":"Not reachable via normal open() calls: mode has passed 'must have exactly one of read/write/append mode' by this point. Could only fire with monkeypatched mode flags, a fork of _pyio with modified validation, or direct misuse of the internal code path.","commonSituations":"Users encounter this string only when grepping the stdlib source or running heavily patched IO stacks (test mocks that stub the mode-flag computation, vendored variants of open()).","solutions":["Use a standard mode string ('r', 'w', 'a', 'x' plus 'b'/'+'/'t'); the normal validators will then guide you.","If you monkeypatched open/_pyio internals, undo the patch or fix the patched flag computation.","In forks of this logic, ensure the buffer-class selection covers the same cases your validation admits."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"assert mode in {'r','w','a','x'} | {m+'+' for m in 'rwax'} | {m+c for m in 'rwax' for c in ('b','t','+b','+t')}, mode\nopen(path, mode)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Whitelist mode strings at the config/CLI boundary; never free-form-build them.","If this appears at runtime, suspect monkeypatched open/_pyio internals, not the mode string."],"tags":["io","open","mode","valueerror","defensive"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}