3.2.1. Core Operations#
All public parsing and building helpers follow the same model: resolve a
struct-like object, create a root Context, and
delegate to the relevant protocol method.
The most commonly used helpers are available from caterpillar.py and
caterpillar.model.
3.2.1.1. Packing#
- pack(obj, struct=None, /, *, use_tempfile=False, as_field=False, order=None, arch=None, fill=None, **kwargs)#
Builds
objand returns the generated bytes.If
structis omitted, Caterpillar tries to infer it fromobjvia the__struct__container attribute. Ifstructis supplied andas_field=Trueis used, the value is wrapped inFieldbefore packing. Additional keyword arguments are copied into the root context and can be read withctxor custom context paths.orderandarchtemporarily override the default byte order and architecture for the whole packing operation. This includes dynamic byte-order and pointer-size decisions that consult the root context.fillcontrols bytes inserted for holes created by offset fields. It may beNone(the defaultb"\x00"),bytes,stror a single-byte integer. The fill pattern must evenly divide the gap it fills.
- pack_into(obj, buffer, struct=None, /, *, use_tempfile=False, as_field=False, order=None, arch=None, fill=None, **kwargs)#
Builds
objand writes the result intobuffer.buffermust be a writable stream. When no offset fields are involved and the struct can safely write directly, Caterpillar writes to the target stream without the intermediate buffer used for offset reconciliation. If offset fields are encountered, packing falls back to the buffered path and applies the collected offset writes in order.use_tempfile=Trueforces the buffered path to use a temporary file rather than an in-memory buffer. This is mainly useful for very large outputs with offset writes.
- pack_file(obj, filename, struct=None, /, *, use_tempfile=False, as_field=False, order=None, arch=None, fill=None, **kwargs)#
Opens
filenamein binary write mode and delegates topack_into().
3.2.1.2. Unpacking#
- unpack(struct, buffer, /, as_field=False, order=None, arch=None, **kwargs)#
Parses
bufferwithstructand returns the unpacked value.buffercan be bytes-like data or a readable stream. Ifas_field=Trueis used, Caterpillar wrapsstructinFieldbefore unpacking. As with packing,orderandarchtemporarily override the operation defaults andkwargsare copied into the root context.Fixed-size primitive reads are exact: if the stream provides fewer bytes than required, unpacking raises
ValidationErrorinstead of silently accepting a short value.
3.2.1.3. Size and type helpers#
- sizeof(obj, **kwargs)#
Returns the static size of
objin bytes.objmay be a struct-like object, a class or instance that contains__struct__, or any object implementing__size__(context). Keyword arguments are copied into the context before size calculation, which allows context-dependent static sizes to be evaluated.If the struct cannot determine an integer size, Caterpillar raises
DynamicSizeError. Dynamic or greedy layouts should therefore either be measured with enough context to make their size static or handled during actual packing/unpacking.
- typeof(obj)#
Returns the Python type represented by
obj.If
objis a struct container, Caterpillar first resolves its__struct__value. It then calls__type__()when present. Missing, unimplemented orNotImplementedtype hooks resolve toobject.
- getbits(obj)#
Returns the number of bits represented by a bitfield-compatible object.
__bits__may be either an integer attribute or a zero-argument callable.
- issigned(obj)#
Returns whether
objdeclares signed integer semantics with__signed__.
3.2.1.4. Context created by operations#
Top-level pack and unpack operations create a root context containing at least these operation-specific values:
Key |
Meaning |
|---|---|
|
Active input or output stream. |
|
Current logical path; the root path is |
|
Parent context, |
|
Effective byte order. |
|
Effective architecture. |
|
Either pack or unpack mode. |
|
Pack-only map used for deferred offset writes. |
Nested fields and models add field, object, index, and path information while they delegate to their child structs.