{"record":{"id":"ade1221c81b58e8e","repo":"RustPython/RustPython","slug":"unknown-mode-r","errorCode":null,"errorMessage":"unknown mode: %r","messagePattern":"unknown mode: %r","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"info","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/RustPython/RustPython/blob/aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd/Lib/_pyio.py#L243-L279","documentation":"ValueError from the final else-branch of _pyio.open() after buffer-type selection - it fires when the mode is neither updating ('+'), nor creating/writing/appending, nor reading. Because the earlier check at line 221 already guarantees exactly one of x/r/w/a is present, this branch is effectively unreachable defensive code inherited from CPython; seeing it means the mode string was mutated concurrently, the module was monkey-patched, or the interpreter itself is misbehaving.","triggerScenarios":"Not producible through the public API under normal execution; would require mutating the mode between validation and dispatch (hostile monkey-patching of _pyio.open, or a divergent port such as an embedded interpreter).","commonSituations":"Essentially none in practice; reported occurrences are usually misattributed line numbers from a different open() implementation (e.g. a patched, embedded, or ported interpreter like RustPython drift).","solutions":["Treat it as an interpreter/framework bug: report to the runtime used (CPython or the port) with a minimal reproducer","Audit the process for monkey-patching of _pyio.open or of stream classes","Retry with an explicit canonical mode string ('r', 'rb', ...) to rule out mode corruption"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    f = open(path, mode)\nexcept ValueError as e:\n    if 'unknown mode' in str(e):\n        log.exception('mode dispatch failure - interpreter-level bug')\n    raise                              # not recoverable at the call site","preventionTips":["Use canonical mode strings so dispatch never reaches the fallback branch","Avoid monkey-patching _pyio.open or stream classes","If this fires, capture the interpreter version and report it upstream"],"tags":["io","open","valueerror","mode","dead-code","defensive-check"],"backgroundTag":"invalid-file-mode","analyzedSha":"aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd","analyzedAt":"2026-08-17T00:37:52.100Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}