HelloZeroNet/ZeroNet · error · Exception
Invalid type: %s
Error message
Invalid type: %s
What it means
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.
Source
Thrown at plugins/Bigfile/BigfilePiecefield.py:71
if len(data) < idx:
data = data.ljust(idx + 1, b"\x00")
return data[:idx] + bit + data[idx+ 1:]
class Piecefield(object):
def tostring(self):
return "".join(["1" if b else "0" for b in self.tobytes()])
class BigfilePiecefield(Piecefield):
__slots__ = ["data"]
def __init__(self):
self.data = b""
def frombytes(self, s):
if not isinstance(s, bytes) and not isinstance(s, bytearray):
raise Exception("Invalid type: %s" % type(s))
self.data = s
def tobytes(self):
return self.data
def pack(self):
return packPiecefield(self.data).tobytes()
def unpack(self, s):
self.data = unpackPiecefield(array.array("H", s))
def __getitem__(self, key):
try:
return self.data[key]
except IndexError:
return False
def __setitem__(self, key, value):View on GitHub (pinned to 454c0b2e7e)
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.
Example fix
// before
piecefield.frombytes(piece_data) # str from JSON
// after
if isinstance(piece_data, str):
piece_data = piece_data.encode("latin1")
if piece_data:
piecefield.frombytes(piece_data) Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(s, (bytes, bytearray)):
s = s.encode("latin1") if isinstance(s, str) else bytes(s or b"") Type guard
def is_bytes_like(s):
return isinstance(s, (bytes, bytearray)) Try / catch
try:
piecefield.frombytes(s)
except Exception as e:
if "Invalid type" in str(e):
piecefield.frombytes(s.encode("latin1") if isinstance(s, str) else b"")
else:
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Invalid data type: %s
- Invalid bit: %s
- Style values must be strings
- Style values must be strings
- Unable to download piecemap: %s
AI-assisted analysis of HelloZeroNet/ZeroNet@454c0b2e7e (2026-09-02).
Data as JSON: /api/errors/fa4e7a1355e5fe51.
Report an issue: GitHub.