[DEV] (2.5.0 rc) Bitfield revamp#34
Merged
Merged
Conversation
--- abc.py: + _ActionLike was removed and splitted into two separate protocols:_SupportsActionUnpack and _SupportsActionPack
--- + BitField was renamed to Bitfield + Each Bitfield now stores a list of groups, which either represent a field or a list of entries + bitfield_example was updated
--- + New option: B_NO_AUTO_BOOL to disable automatically assigning fields with one bit width a boolean factory
--- + Updated imports in __init__ + Added two new value factories: CharFactory and EnumFactory + Bitfields now corrently populate documentation types if S_REPLACE_TYPES is active
This was referenced Jun 27, 2025
MatrixEditor
added a commit
that referenced
this pull request
Jun 29, 2025
--- ## Changes made The code of all modules were refactored to include as little type hints as possible now. All typing related information should be taken from `.pyi` files. ##### `caterpillar.abc` - Removed `_Action` and split into two separate Protocols `_ActionLike` := `_SupportsActionUnpack` | `_SupportsActionPack` - Renamed `_Switch` to `_SwitchLike` - Removed `_EnumLike` - Added two new protocols: '_SupportsBits' and '_ContainsBits' - The following attributes and methods were moved into [caterpillar.shared](#caterpillarshared): `STRUCT_FIELD` -> `ATTR_STRUCT`, `hasstruct`, `getstruct` and `typeof` - Removed unused '_ContextPathStr' - Removed `__type__()` requirement from '_StructLike' - Added new protocol: `_SupportsType` ##### `caterpillar.byteorder` - Moved 'BYTEORDER_FIELD' to [caterpillar.shared](#caterpillarshared) as `ATTR_BYTEORDER` ##### `caterpillar.shortcuts` - Shortcuts now include `typeof`, `to_struct`, `hasstruct`, `getstruct` and `sizeof` ##### `caterpillar.shared` - New constants moved from other modules: `ATTR_BYTEORDER`, `ATTR_TYPE`, `ATTR_BITS`, `ATTR_SIGNED`, `ATTR_TEMPLATE` ##### `caterpillar.context` - New context attribute: '_root' can be set to point to the root context instance. Internally, instead of a for-loop that iterates through parent context instances, a simple self.get(...) call is made. (see #35) ##### `caterpillar.model._base` - Fixed an issue when parsing union objects with an unbound stream object - Fixed an issue where field options defined in Sequences were not populated when creating fields. ##### `caterpillar.model._struct` - `sizeof` now checks if the provided object implements the `_SupportsSize` protocol ##### `caterpillar.model._bitfield` - **Completely reworked the bitfield mechanism to make it even more powerful. For details refer to #34.** - Fixed an issue where field options defined in BitFields were not populated when creating fields. - Moved `BITS_ATTR` and `SIGNED_ATTR` into [caterpillar.shared](#caterpillarshared) - Removed unused `getformat` function ##### `caterpillar.fields.common` - `Transformer`: removed `__fmt__` method ##### `caterpillar.fields.compression` - Updated public compression methods to use lazy imports #### Documentation - Updated library documentation to reflect changes made to function signatures - Updated the reference to explicitly state the Protocols defined in `caterpillar.abc` - Created a change log to reflect changes made up to v2.5.0 ## Associated Issues and Pull Requests - #30: Stub files for Caterpillar allowing to use generics and more detailed return types - #27: Python3.14 does NOT break this project. However, some features (with-statements) can't be used - #34: Introducing a new Bitfield concept to make it even more flexible - #35: Optimized resolving the root context instance - #37: Various small fixes related to the documentation - #24: Bitfield docs - #33: General documentation updates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
New Concept
Each Bitfield instance maintains a sequence of bitfield groups, where each group contains a collection of sized fields. A bitfield group may consist of either multiple entries (i.e., any types that can be converted to an integral type) or a single _StructLike object. For example, consider the following bitfield definition:
This Bitfield definition will generate three distinct bitfield groups (labeled here as groups a, b, and c). By default, bitfields use 8-bit alignment, leading to the following layout:
Internally, only the first group requires special bit-level parsing. The remaining groups (b and c) are treated as standard structures since they span full bytes or words without sub-byte alignment. This dynamic grouping mechanism allows leveraging full struct-like class definitions within bitfields.
Example Code:
Additional changes introduced:
caterpillar.abcActionLikewas removed and split into two separate protocols:_SupportsActionUnpackand_SupportsActionPackIntandUIntfield structs now implement a reasonable__repr__format