{"record":{"id":"0f6fe85feb20beb5","repo":"juicedata/juicefs","slug":"binary-mode-doesn-t-take-an-encoding-argument","errorCode":null,"errorMessage":"binary mode doesn't take an encoding argument","messagePattern":"binary mode doesn't take an encoding argument","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdk/python/juicefs/juicefs/juicefs.py","lineNumber":225,"sourceCode":"        cnt = 0\n        for c in mode:\n            if c in 'rwxa':\n                cnt += 1\n                if c == 'r':\n                    flag |= MODE_READ\n                else:\n                    flag |= MODE_WRITE\n            elif c == '+':\n                flag |= MODE_READ | MODE_WRITE\n            elif c not in 'tb':\n                raise ValueError(f'invalid mode: {mode}')\n        if cnt != 1:\n            raise ValueError('must have exactly one of create/read/write/append mode')\n        if 'b' in mode:\n            if 't' in mode:\n                raise ValueError(\"can't have text and binary mode at once\")\n            if encoding:\n                raise ValueError(\"binary mode doesn't take an encoding argument\")\n            if errors:\n                raise ValueError(\"binary mode doesn't take an errors argument\")\n        else:\n            if not encoding:\n                encoding = locale.getpreferredencoding(False).lower()\n            if not errors:\n                errors = 'strict'\n            codecs.lookup(encoding)\n\n        size = 0\n        if 'x' in mode:\n            fd = self.lib.jfs_create(c_int64(_tid()), c_int64(self.h), _bin(path), c_uint16(0o666), c_uint16(self.umask))\n        else:\n            try:\n                sz = c_uint64()\n                fd = self.lib.jfs_open_posix(c_int64(_tid()), c_int64(self.h), _bin(path), byref(sz), c_int32(flag))\n                if 'w' in mode:\n                    self.lib.jfs_ftruncate(c_int64(_tid()), fd, c_uint64(0))","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/juicedata/juicefs/blob/c9a67b23e8e08ec23ec331aa6f1675e2319e921c/sdk/python/juicefs/juicefs/juicefs.py#L207-L243","documentation":"In binary mode, text decoding parameters are meaningless: open() raises ValueError when encoding (and separately errors/newline) is passed together with a mode containing 'b'. This matches CPython's builtin open() behavior for the JuiceFileSystem wrapper.","triggerScenarios":"jfs.open(path, 'rb', encoding='utf-8') or jfs.open(path, 'wb', errors='ignore') — any binary mode combined with a non-None encoding argument.","commonSituations":"Passing through kwargs from a generic file-opener that always supplies encoding; refactored code that switched mode from 'r' to 'rb' but left encoding in the call; default encoding values injected by wrappers.","solutions":["Drop the encoding argument when using a binary mode.","If decoding is needed, switch to text mode ('r'/'w') and keep encoding.","Pass encoding=None conditionally based on whether 'b' is in the mode."],"exampleFix":"# before\nf = jfs.open(path, 'rb', encoding='utf-8')\n# after\nf = jfs.open(path, 'rb')  # or jfs.open(path, 'r', encoding='utf-8')","handlingStrategy":"validation","validationCode":"if 'b' in mode and encoding is not None:\n    raise ValueError(\"encoding is not allowed in binary mode\")","typeGuard":"def is_valid_open_call(path, mode, encoding=None) -> bool:\n    return not ('b' in mode and encoding is not None)","tryCatchPattern":"try:\n    f = jfs.open(path, mode, encoding=encoding)\nexcept ValueError as e:\n    if \"binary mode doesn't take an encoding\" in str(e):\n        f = jfs.open(path, mode)\n    else:\n        raise","preventionTips":["Only pass encoding when the mode lacks 'b'.","Conditionally build kwargs: {'encoding': enc} if 'b' not in mode else {}.","Avoid forwarding generic opener kwargs blindly into binary open() calls."],"tags":["python","file-open","argument-validation"],"backgroundTag":"mutually-exclusive-options","analyzedSha":"c9a67b23e8e08ec23ec331aa6f1675e2319e921c","analyzedAt":"2026-09-06T17:55:48.476Z","contentChangedAt":"2026-09-06T17:55:48.476Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}