Python package to translate C struct to classes
Project description
cstructimpl
A Python package for translating C
structs into Python classes.
⚡ Quick Start
Install from PyPI:
pip install cstructimpl
Define your struct and parse raw bytes:
from cstructimpl import *
class Info(CStruct):
age: Annotated[int, CType.U8]
height: Annotated[int, CType.U8]
class Person(CStruct):
info: Info
name: Annotated[str, CStr(6)]
person = Person.c_build(bytes([18, 170]) + b"Pippo\x00")
print(person) # Person(info=Info(age=18, height=170), name='Pippo')
🚀 Introduction
cstructimpl makes working with binary data in Python simple and intuitive.
By subclassing CStruct, you can define Python classes that map directly to C-style structs and parse raw bytes into fully typed objects.
No manual parsing, no boilerplate — just define your struct and let the library do the heavy lifting.
🔧 Type System
At the core of the library is the BaseType protocol, which defines how types behave in the C world:
class BaseType(Protocol[T]):
def c_size(self) -> int: ...
def c_align(self) -> int: ...
def c_signed(self) -> bool: ...
def c_build(
self,
raw: bytes,
*,
byteorder: Literal["little", "big"] = "little",
signed: bool | None = None,
) -> T | None: ...
Any class that follows this protocol can act as a BaseType, controlling its own parsing, size, and alignment.
When parsing a struct:
- If a field type is itself a
BaseType, parsing happens automatically. - Otherwise, annotate the field with
Annotated[..., BaseType]to tell the parser how to interpret it.
The library comes with a set of ready-to-use type definitions that cover the majority of C primitive types.
🎭 Autocast
Sometimes raw numeric values carry semantic meaning. In C, this is usually handled with enums.
With cstructimpl, you can automatically reinterpret values into enums (or other types) using Autocast.
from cstructimpl import *
class ResultType(Enum):
OK = 0
ERROR = 1
class Person(CStruct):
kind: Annotated[ResultType, CType.U8, Autocast()]
error_code: Annotated[int, CType.I32]
This is equivalent to writing a custom builder:
from cstructimpl import *
class ResultType(Enum):
OK = 0
ERROR = 1
class Person(CStruct):
kind: Annotated[ResultType, CBuilder(CType.U8, lambda u8: ResultType(u8))]
error_code: Annotated[int, CType.I32]
But much simpler and less error-prone.
✨ Features
- Define Python classes that map directly to C
structs - Parse raw bytes into typed objects with a single method call
- Built-in type system for common C primitives
- Support for nested structs
- Flexible extension via the
BaseTypeprotocol
📖 Use Cases
- Parsing binary network protocols
- Working with binary file formats
- Interfacing with C libraries and data structures
- Replacing boilerplate parsing code with clean, type-safe classes
📚 Documentation
More detailed usage examples and advanced topics are available in the documentation.
🤝 Contributing
Contributions are welcome!
If you'd like to improve cstructimpl, please open an issue or submit a pull request on GitHub.
📜 License
This project is licensed under the terms of the Apache-2.0 License.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cstructimpl-0.2.0.tar.gz.
File metadata
- Download URL: cstructimpl-0.2.0.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb8f7ee28fd269d300cee1f5b4cc3f0ea76a54a68be66b20ecbd9c95d4b14c24
|
|
| MD5 |
90dca0d3a6feebd34243a9e979e79aba
|
|
| BLAKE2b-256 |
c60e84eb41d74ba2dd03f1b7ad75833d2d9897d8c7ba8477623416e06cb5b18e
|
File details
Details for the file cstructimpl-0.2.0-py3-none-any.whl.
File metadata
- Download URL: cstructimpl-0.2.0-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
858d7c9bf1faa128854de59b490a48395583b54660a138d3adb62aff5a0d27bf
|
|
| MD5 |
50f90336d4ecbf9ab9edfd218dccb22e
|
|
| BLAKE2b-256 |
c1f87ff793388c6c958a0fc24c6d1ddbb3f2b7286800fd9af723d9b5d1f74859
|