Skip to main content

Creating ctype structure like dataclass

Project description

cbridge

uv ruff GitHub License PyPI - Version PyPI - Python Version Tests codecov

A Python library that provides a dataclass-like interface for creating ctypes structures, making it easier to work with C libraries and data structures in Python.

Features

  • Dataclass-like syntax: Define C structures using familiar Python class syntax with type hints
  • Automatic field management: Fields are automatically converted to ctypes types based on type annotations
  • Array support: Built-in support for fixed-size arrays with proper type checking
  • Pointer support: Easy handling of C pointers with type safety
  • Default values: Support for default field values and factories
  • Inheritance: Full support for class inheritance in C structures
  • Memory layout control: Control structure packing and alignment
  • C function binding: Decorator for easy binding to C library functions

Installation

pip install cbridge

Quick Start

Creating C Structures

from cbridge import CStruct
from cbridge import types

class Point(CStruct):
    x: types.int32
    y: types.int32

class Rectangle(CStruct):
    top_left: Point
    bottom_right: Point
    color: types.uint32 = 0xFF0000  # Default value

# Create instances
point = Point(x=10, y=20)
rect = Rectangle(
    top_left=Point(x=0, y=0),
    bottom_right=Point(x=100, y=100)
)

print(rect)  # Rectangle(top_left=Point(x=0, y=0), bottom_right=Point(x=100, y=100), color=16711680)

Working with Arrays

from typing import Literal

from cbridge import CStruct
from cbridge import types


class Data(CStruct):
    id: types.int32
    values: types.Array[types.int8, Literal[4]]  # Array of 4 int8 values
    name: types.char_ptr


# Create with array data
data = Data(id=1, values=[1, 2, 3, 4], name=b"test")
print(data.values)  # [1, 2, 3, 4]

C Function Binding

from cbridge import cfunc
from cbridge import types

@cfunc("c")  # Bind to libc
def time(t: types.Pointer[types.ctime_t]) -> types.ctime_t: ...

# Call the C function
current_time = time(None)
print(current_time)

Memory Operations

import array

from cbridge import CStruct
from cbridge import types

class Point(CStruct):
    x: types.int32
    y: types.int32


# Create from buffer
buffer = array.array("B", b"\x01\x00\x00\x00\x02\x00\x00\x00")
point = Point.from_buffer(buffer)
print(point)  # Point(x=1, y=2)

Advanced Usage

Structure Packing

import ctypes

from cbridge import CStruct
from cbridge import types


class DefaultPackData(CStruct):  # 1-byte alignment
    a: types.char
    b: types.int32


class Pack1Data(CStruct, pack=1):  # 1-byte alignment
    a: types.char
    b: types.int32


print(ctypes.sizeof(DefaultPackData))  # 8 bytes
print(ctypes.sizeof(Pack1Data))  # 5 bytes

Default Values and Factories

from typing import Literal

from cbridge import CStruct
from cbridge import field
from cbridge import types


class Config(CStruct):
    name: types.char_ptr
    timeout: types.int32 = 30
    flags: types.Array[types.int32, Literal[2]] = field(default_factory=lambda: [0, 1])


print(Config(name=b"test"))  # Config(name=b'test', timeout=30, flags=[0, 1])

Pointer Operations

[!WARNING] Python 3.13 and above are currently not supported forward declarations

from cbridge import CStruct
from cbridge import types
from cbridge.types import pointer


class Node(CStruct):
    data: types.int32
    next: "types.Pointer[Node]"


# Create linked list
node1 = Node(data=1, next=None)
node2 = Node(data=2, next=pointer(node1))
node1.next = pointer(node2)

node = node1
data = []
for _ in range(8):
    data.append(node.data)
    node = node.next[0]   # same as *node.next
assert data == [1, 2, 1, 2, 1, 2, 1, 2]

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

cbridge-0.2.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cbridge-0.2.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file cbridge-0.2.0.tar.gz.

File metadata

  • Download URL: cbridge-0.2.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for cbridge-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d77991c01cba5eb0247414264a2a722f700eafc5122fb709444ec85eb991f927
MD5 e3adc3f4df8b3359071c07d8d3ee40c7
BLAKE2b-256 c00c88fd2e13ff8606d77c4376472d597c4263a42a05d643e112388668db46f6

See more details on using hashes here.

File details

Details for the file cbridge-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: cbridge-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for cbridge-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 12b5256eca8549a6a18f11ff7f0744460b200de34e67616e0b6502166bac2926
MD5 0c81e00e3e94640f50166e290b0ec366
BLAKE2b-256 fe587efad9db6c0ac96298b20b46cc9798e7c876ab5137d0778ad077809f0919

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page