{"id":23472,"library":"cstruct","title":"cstruct","description":"A Python library for defining and parsing C-style structs. Supports nested structs, arrays, strings, and variable-length fields. Version 6.2 works with Python 3.7+ and supports Python 3.14. Released under the MIT license, with active maintenance and periodic updates.","status":"active","version":"6.2","language":"python","source_language":"en","source_url":"https://github.com/andreax79/python-cstruct","tags":["struct","binary","parsing","serialization","C-structs"],"install":[{"cmd":"pip install cstruct","lang":"bash","label":"Install from PyPI"}],"dependencies":[],"imports":[{"note":"","wrong":"","symbol":"CStruct","correct":"from cstruct import CStruct"},{"note":"The module itself is also directly importable for low-level use.","wrong":"","symbol":"cstruct","correct":"import cstruct"}],"quickstart":{"code":"from cstruct import CStruct\n\n# Define a struct matching the C definition:\n# struct Point { int x; int y; };\nclass Point(CStruct):\n    __struct__ = [\n        ('x', 'int'),\n        ('y', 'int'),\n    ]\n\n# Create an instance and pack\np = Point(x=10, y=20)\ndata = p.pack()\nprint(data.hex())\n\n# Unpack from bytes\np2 = Point(data)\nprint(p2.x, p2.y)","lang":"python","description":"Defines a simple Point struct, packs values, and unpacks from bytes."},"warnings":[{"fix":"Ensure struct definitions use the __struct__ list format or check compatibility if using Python class attributes.","message":"In v6.0, access to Python class attributes in struct definitions was added. Prior versions did not support this; code relying on dynamic attribute access may need updates.","severity":"breaking","affected_versions":"<6.0"},{"fix":"Update your build configuration to use pyproject.toml instead of setup.cfg.","message":"The use of setup.cfg is deprecated; v6.2 moved to pyproject.toml. If you were customizing build via setup.cfg, migrate to pyproject.toml.","severity":"deprecated","affected_versions":">=6.2"},{"fix":"Define string fields with explicit length, e.g., ('name', 'char[32]'), and ensure raw bytes are encoded/decoded correctly.","message":"Strings in structs (e.g., 'char[10]') are packed as null-terminated byte strings. Mismatch in length or encoding can cause corruption.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Define __struct__ as a list of tuples, e.g., __struct__ = [('x', 'int'), ('y', 'int')].","cause":"The __struct__ attribute is not properly defined as a list of (field_name, field_type) tuples.","error":"TypeError: __struct__ must be a list of tuples"},{"fix":"Ensure string fields are unpacked as strings (they are automatically decoded) and binary fields are handled as bytes.","cause":"Attempting to call .decode() on a bytes object that is not a string field; or using a Python string where bytes are expected.","error":"AttributeError: 'bytes' object has no attribute 'decode'"},{"fix":"Verify the buffer length matches the struct size (calculate using len(struct_instance)).","cause":"The buffer length does not match the expected struct size (e.g., packing two ints expects 8 bytes).","error":"ValueError: unpack requires a buffer of 8 bytes"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}