Skip to main content

Generate Python ctypes bindings from shared libraries

Project description

dwarfbind

Generate Python ctypes bindings from shared libraries.

A tool to help generate Python ctypes bindings from shared libraries. It reads DWARF debug information from library files to understand C structures and types, then generates equivalent Python classes.

Features

  • Reads DWARF debug information to understand C types and structures
  • Generates Python ctypes classes that match the C structures
  • Handles complex type relationships and dependencies
  • Creates proper Python modules with types, symbols, and constants submodules
  • Provides clean imports like from mylib.types import MyStruct
  • Extracts constants and macros from header files
  • Automatically discovers library paths using pkg-config
  • Supports relative header paths (e.g., freerdp/freerdp.h with -I /usr/include/freerdp3)

Requirements

  • Python 3.12 or newer
  • Debug symbols for the libraries you want to analyze:
    # Install debug symbols for a specific package
    sudo dnf debuginfo-install libfreerdp3
    
    # Or enable debuginfo repos and install manually
    sudo dnf install 'dnf-command(debuginfo-install)'
    sudo dnf debuginfo-install libfreerdp3
    
  • C preprocessor (cpp) for header file analysis:
    # Install gcc which includes cpp
    sudo dnf install gcc
    
  • pkg-config for library and include path discovery:
    # Install pkg-config
    sudo dnf install pkgconf-pkg-config
    

Installation

Usage

Generate bindings for a shared library:

# Basic usage
dwarfbind /usr/lib/libexample.so

# Extract constants from headers
dwarfbind --headers example.h /usr/lib/libexample.so

# Add include paths for header processing
dwarfbind -I /usr/include -I /usr/local/include --headers example.h /usr/lib/libexample.so

# Specify output file
dwarfbind -o bindings.py /usr/lib/libexample.so

# Process multiple libraries at once
dwarfbind -o output/ libone.so libtwo.so

# Use pkg-config to find library and include paths
dwarfbind --pkgconfig freerdp3 --headers freerdp/freerdp.h

# Enable verbose output
dwarfbind -v /usr/lib/libexample.so

The generated bindings provide a clean Python API:

from ctypes import *
from example.types import MyStruct
from example.symbols import my_function
from example.constants import BUFFER_SIZE

# Create and use structures
structure = MyStruct()
structure.field = BUFFER_SIZE

# Call library functions
my_function.argtypes = [POINTER(MyStruct)]
my_function.restype = c_int
result = my_function(byref(structure))

Development

To set up a development environment:

# Install Python 3.12 or newer
uv python install 3.12
# Optional: install shims for python/python3
uv python install --default

# Create a virtual environment
uv venv -p 3.12
# Optional: activate the environment
source .venv/bin/activate

# Sync dependencies (including dev tools)
uv sync

# Run tests
uv run pytest

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. See CONTRIBUTING.md for development setup, workflow guidelines, and coding style.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

dwarfbind-0.1.3.tar.gz (62.6 kB view details)

Uploaded Source

Built Distribution

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

dwarfbind-0.1.3-py3-none-any.whl (45.1 kB view details)

Uploaded Python 3

File details

Details for the file dwarfbind-0.1.3.tar.gz.

File metadata

  • Download URL: dwarfbind-0.1.3.tar.gz
  • Upload date:
  • Size: 62.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.12

File hashes

Hashes for dwarfbind-0.1.3.tar.gz
Algorithm Hash digest
SHA256 00fe8c999180ef57d504bb48bc149a389099e4898937aa6d5b41ebb1d7a11e6c
MD5 f37211edacfca12809a69613129a6a79
BLAKE2b-256 876adb83e8248c890c3cc8c33c49de72da6cdf1bb2e748d0390c26a1e2df826d

See more details on using hashes here.

File details

Details for the file dwarfbind-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: dwarfbind-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 45.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.12

File hashes

Hashes for dwarfbind-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 41cbd7c211cc361575dd927160e6ed8d82ab7700a0c98071b9fa4f4d1ae2b6c9
MD5 c97673176dd27852822787347013e4b3
BLAKE2b-256 9bc552bd254267f0f041a78e75aed51712554eaf314a59fcc2c444bd23fdbc23

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