{"record":{"id":"9c7be1c629239681","repo":"RustPython/RustPython","slug":"invalid-encoding-r","errorCode":null,"errorMessage":"invalid encoding: %r","messagePattern":"invalid encoding: %r","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/_pyio.py","lineNumber":203,"sourceCode":"    mode, it returns a BufferedReader; in write binary and append binary\n    modes, it returns a BufferedWriter, and in read/write mode, it returns\n    a BufferedRandom.\n\n    It is also possible to use a string or bytearray as a file for both\n    reading and writing. For strings StringIO can be used like a file\n    opened in a text mode, and for bytes a BytesIO can be used like a file\n    opened in a binary mode.\n    \"\"\"\n    if not isinstance(file, int):\n        file = os.fspath(file)\n    if not isinstance(file, (str, bytes, int)):\n        raise TypeError(\"invalid file: %r\" % file)\n    if not isinstance(mode, str):\n        raise TypeError(\"invalid mode: %r\" % mode)\n    if not isinstance(buffering, int):\n        raise TypeError(\"invalid buffering: %r\" % buffering)\n    if encoding is not None and not isinstance(encoding, str):\n        raise TypeError(\"invalid encoding: %r\" % encoding)\n    if errors is not None and not isinstance(errors, str):\n        raise TypeError(\"invalid errors: %r\" % errors)\n    modes = set(mode)\n    if modes - set(\"axrwb+t\") or len(mode) > len(modes):\n        raise ValueError(\"invalid mode: %r\" % mode)\n    creating = \"x\" in modes\n    reading = \"r\" in modes\n    writing = \"w\" in modes\n    appending = \"a\" in modes\n    updating = \"+\" in modes\n    text = \"t\" in modes\n    binary = \"b\" in modes\n    if text and binary:\n        raise ValueError(\"can't have text and binary mode at once\")\n    if creating + reading + writing + appending > 1:\n        raise ValueError(\"can't have read/write/append mode at once\")\n    if not (creating or reading or writing or appending):\n        raise ValueError(\"must have exactly one of read/write/append mode\")","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/RustPython/RustPython/blob/aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd/Lib/_pyio.py#L185-L221","documentation":"TypeError raised by _pyio.open() when encoding is given and is not a str (and not None). encoding is only meaningful in text mode and must be a codec name accepted by codecs.lookup(), such as 'utf-8', 'ascii', or 'latin-1'; None selects the platform locale default. The check runs before any file descriptor is opened, so no file is created or truncated as a side effect.","triggerScenarios":"encoding=65001 (a Windows code-page number instead of the string 'cp65001' or 'utf-8'); encoding=['utf-8'] from an over-eager config loader that wraps scalars in lists; the 4th positional argument slot holding a non-string due to argument misplacement.","commonSituations":"Porting Windows batch-script habits where code pages are integers; YAML/JSON configs that parse values as lists or numbers; mixing up the encoding and errors positional slots when refactoring call signatures.","solutions":["Pass a codec name string: encoding='utf-8'","Use None (the default) to select the platform locale encoding","Validate config values with codecs.lookup(encoding) before forwarding them to open()"],"exampleFix":"# before\nf = open(path, 'r', encoding=65001)\n\n# after\nf = open(path, 'r', encoding='cp65001')   # or 'utf-8'","handlingStrategy":"validation","validationCode":"import codecs\nif encoding is not None:\n    if not isinstance(encoding, str):\n        raise TypeError(f'encoding must be a codec name str, got {encoding!r}')\n    codecs.lookup(encoding)            # fails fast on unknown names too\nf = open(path, mode, encoding=encoding)","typeGuard":"def is_codec_name(value) -> bool:\n    return value is None or (isinstance(value, str) and _try(lambda: codecs.lookup(value)))","tryCatchPattern":null,"preventionTips":["Use codec name strings ('utf-8', 'cp65001'), never numeric code pages","Validate config-supplied encodings against codecs.lookup() at startup","Prefer keyword arguments encoding=..., errors=... to avoid slot swaps"],"tags":["io","open","typeerror","encoding","text-io","argument-validation"],"backgroundTag":"invalid-encoding-argument","analyzedSha":"aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd","analyzedAt":"2026-08-17T00:37:52.100Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}