{"record":{"id":"2fb148fc35fbb3c4","repo":"HelloZeroNet/ZeroNet","slug":"invalid-data-type-s","errorCode":null,"errorMessage":"Invalid data type: %s","messagePattern":"Invalid data type: (.+?)","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"plugins/Bigfile/BigfilePiecefield.py","lineNumber":6,"sourceCode":"import array\n\n\ndef packPiecefield(data):\n    if not isinstance(data, bytes) and not isinstance(data, bytearray):\n        raise Exception(\"Invalid data type: %s\" % type(data))\n\n    res = []\n    if not data:\n        return array.array(\"H\", b\"\")\n\n    if data[0] == b\"\\x00\":\n        res.append(0)\n        find = b\"\\x01\"\n    else:\n        find = b\"\\x00\"\n    last_pos = 0\n    pos = 0\n    while 1:\n        pos = data.find(find, pos)\n        if find == b\"\\x00\":\n            find = b\"\\x01\"\n        else:\n            find = b\"\\x00\"","sourceCodeStart":1,"sourceCodeEnd":24,"githubUrl":"https://github.com/HelloZeroNet/ZeroNet/blob/454c0b2e7e000fda7000cba49027541fbf327b96/plugins/Bigfile/BigfilePiecefield.py#L1-L24","documentation":"packPiecefield in BigfilePiecefield.py compresses a piecefield (bytes of b'\\x00'/b'\\x01' flags describing downloaded big-file pieces) into run-length encoded 16-bit words. It raises Exception(\"Invalid data type: %s\") when the input is neither bytes nor bytearray — the compressor refuses to guess how to encode other types.","triggerScenarios":"Calling packPiecefield (directly or via Piecefield.pack()/frombytes->pack chains) with a str, list, array, or None instead of bytes/bytearray — typically a piecefield stored/loaded through a channel that decoded it (e.g. JSON turning bytes into str).","commonSituations":"Piecefields persisted to sites.json/SQLite as text and read back as str; receiving piecefield data over the ZeroNet protocol where decoding changed the type; passing memoryview or array('H') directly.","solutions":["Convert the value first: data = data.encode() if isinstance(data, str) else bytes(data).","Check where the piecefield is stored/loaded (json/DB) and preserve it as bytes (e.g. store base64).","Fix the API caller to pass the raw bytes from Piecefield.tobytes().","Add an isinstance check at the boundary before packing."],"exampleFix":"// before\npacked = packPiecefield(piecefield_data)  # piecefield_data is str from JSON\n// after\nif isinstance(piecefield_data, str):\n    piecefield_data = piecefield_data.encode(\"latin1\")\npacked = packPiecefield(piecefield_data)","handlingStrategy":"type-guard","validationCode":"if not isinstance(data, (bytes, bytearray)):\n    data = data.encode(\"latin1\") if isinstance(data, str) else bytes(data)","typeGuard":"def is_piecefield_bytes(data):\n    return isinstance(data, (bytes, bytearray))","tryCatchPattern":"try:\n    packed = packPiecefield(data)\nexcept Exception as e:\n    if \"Invalid data type\" in str(e):\n        packed = packPiecefield(data.encode(\"latin1\"))\n    else:\n        raise","preventionTips":["Store piecefields in bytes-safe formats (base64) across JSON/DB round-trips","Never let JSON decoding turn piecefield bytes into str","Convert with encode('latin1') at load boundaries","Unit-test save/load round-trips of piecefields"],"tags":["type-error","bigfile","validation","piecefield"],"backgroundTag":"invalid-data-type","analyzedSha":"454c0b2e7e000fda7000cba49027541fbf327b96","analyzedAt":"2026-09-02T19:46:57.278Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}