Changes in this release introduce a new inline syntax that can be used to unpack structs (does not include annotated struct classes) from bytes or a stream:
stream = BytesIO(b"\xff\xff@\xe2\x01\x00")
# To affect the endianess, either use
O_DEFAULT_ENDIAN.value = LittleEndian
# or creating a constant field that represents the typed struct
uint32le = LittleEndian + uint32
# to read/unpack values from a stream/buffer, just use the left shift operator
value1: int = uint32le << stream
value2: int = uint16 << stream
value3: str = String(10) << stream
# Instead of using the special operator, all default struct classes provide
# wrapper functions for packing and unpacking:
value = uint8.from_bytes(stream)
assert uint8.to_bytes(value) == 0xFFNote
To implement the from_bytes and to_bytes methods for annotated struct/bitfield/union classes, use the struct_factory.mixin type.
Added
- New inline syntax using the
<<operator to automatically unpack structs from data - All default struct types now implement
from_bytes,from_fileandto_bytesfor direct packing or unpacking - Add
PackMixinandUnpackMixinto automatically implement the methods mentioned above for struct types
Changes
pack,unpackandsizeofwere moved into their own module (no changed visibe when importing fromcaterpillar.models)
Removed
- Drop support for
'x'format character inPyStructFormattedField
Fixes
- Pre-computed conditions in the
Fieldclass incorrectly evaluated to true or false unconditionally (in case of a context-lambda). This issue has now been fixed in all places. - Fix some missing or wrong typing annotations
- #57: allow dynamic offsets to be used again
New Contributors
Full Changelog: v2.8.0...v2.8.1