{"record":{"id":"fa4e7a1355e5fe51","repo":"HelloZeroNet/ZeroNet","slug":"invalid-type-s","errorCode":null,"errorMessage":"Invalid type: %s","messagePattern":"Invalid type: (.+?)","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"plugins/Bigfile/BigfilePiecefield.py","lineNumber":71,"sourceCode":"\n    if len(data) < idx:\n        data = data.ljust(idx + 1, b\"\\x00\")\n    return data[:idx] + bit + data[idx+ 1:]\n\nclass Piecefield(object):\n    def tostring(self):\n        return \"\".join([\"1\" if b else \"0\" for b in self.tobytes()])\n\n\nclass BigfilePiecefield(Piecefield):\n    __slots__ = [\"data\"]\n\n    def __init__(self):\n        self.data = b\"\"\n\n    def frombytes(self, s):\n        if not isinstance(s, bytes) and not isinstance(s, bytearray):\n            raise Exception(\"Invalid type: %s\" % type(s))\n        self.data = s\n\n    def tobytes(self):\n        return self.data\n\n    def pack(self):\n        return packPiecefield(self.data).tobytes()\n\n    def unpack(self, s):\n        self.data = unpackPiecefield(array.array(\"H\", s))\n\n    def __getitem__(self, key):\n        try:\n            return self.data[key]\n        except IndexError:\n            return False\n\n    def __setitem__(self, key, value):","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/HelloZeroNet/ZeroNet/blob/454c0b2e7e000fda7000cba49027541fbf327b96/plugins/Bigfile/BigfilePiecefield.py#L53-L89","documentation":"Piecefield.frombytes initializes the piecefield from raw bytes. It raises Exception(\"Invalid type: %s\") when s is not bytes or bytearray — e.g. a str arriving from JSON storage or protocol decoding. This keeps the internal invariant that self.data is always a bytes blob.","triggerScenarios":"Loading a big file's piecefield from a source that decoded it to str (JSON round-trip, msgpack with text mode), passing memoryview/array('H'), or None when the DB value is missing.","commonSituations":"Piecefields stored in sites.json as latin1-mangled strings; upgrading ZeroNet where storage format changed; third-party code passing str piecefield read via .encode() elsewhere.","solutions":["Convert before calling: s = s.encode(\"latin1\") if isinstance(s, str) else bytes(s).","Persist piecefields as base64/bytes-safe format so they load back as bytes.","Check for None/missing DB values and use an empty Piecefield instead.","Add a loader helper that validates the type before frombytes."],"exampleFix":"// before\npiecefield.frombytes(piece_data)  # str from JSON\n// after\nif isinstance(piece_data, str):\n    piece_data = piece_data.encode(\"latin1\")\nif piece_data:\n    piecefield.frombytes(piece_data)","handlingStrategy":"type-guard","validationCode":"if not isinstance(s, (bytes, bytearray)):\n    s = s.encode(\"latin1\") if isinstance(s, str) else bytes(s or b\"\")","typeGuard":"def is_bytes_like(s):\n    return isinstance(s, (bytes, bytearray))","tryCatchPattern":"try:\n    piecefield.frombytes(s)\nexcept Exception as e:\n    if \"Invalid type\" in str(e):\n        piecefield.frombytes(s.encode(\"latin1\") if isinstance(s, str) else b\"\")\n    else:\n        raise","preventionTips":["Persist piecefields as base64 so they deserialize back to bytes","Handle missing DB values by constructing an empty Piecefield","Validate types at every storage/protocol boundary","Add round-trip tests for piecefield serialization"],"tags":["type-error","bigfile","validation","piecefield","deserialization"],"backgroundTag":"invalid-data-type","analyzedSha":"454c0b2e7e000fda7000cba49027541fbf327b96","analyzedAt":"2026-09-02T19:46:57.278Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}