Skip to main content

Wrap C99 code to Python via the buffer protocol

Project description

c2py23

All code in this repository was generated by DeepSeek V4 and other large language models, in collaboration with jonwright.

Wrap C99 functions as Python C extensions via the buffer protocol. One compiled .so works on Python 2.7 through 3.15 with no recompilation. Zero copies, zero allocations. No numpy required.

Install

pip install c2py23

Requires PyYAML and a C99 compiler (gcc, clang, or MSVC on Windows).

Quick Start

Create a C function:

/* arraysum.c */
#include <stdint.h>
int array_sum(const double *a, const double *b, double *out, intptr_t n) {
    for (intptr_t i = 0; i < n; i++) out[i] = a[i] + b[i];
    return n;
}

Write the interface file:

# arraysum.c2py
module: arraysum
source: [arraysum.c]

functions:
  - py_sig: "array_sum(a: buffer, b: buffer, out: buffer) -> int"
    checks:
      - "a.format == 'd'"
      - "b.format == 'd'"
      - "out.format == 'd'"
    c_overloads:
      - sig: "array_sum(const double *a, const double *b, double *out, intptr_t n) -> int"
        map: {a: "a.ptr", b: "b.ptr", out: "out.ptr", n: "a.n"}

Build and use:

c2py23 build arraysum.c2py
python3 -c "
import ctypes, sys; sys.path.insert(0, '.')
import arraysum
a = (ctypes.c_double * 4)(1, 2, 3, 4)
b = (ctypes.c_double * 4)(5, 6, 7, 8)
r = (ctypes.c_double * 4)(0, 0, 0, 0)
arraysum.array_sum(a, b, r)
print(list(r))  # [6.0, 8.0, 10.0, 12.0]
"

Usage

c2py23 build file.c2py                       # parse + generate + compile
c2py23 build file.c2py --generate-only       # generate wrapper .c only
c2py23 build wrapper.c --compile-only -s src.c -I inc/
                                              # compile existing wrapper
c2py23 build file.c2py --asan                # compile with address sanitizer
c2py23 generate file.c2py -o wrapper.c       # generate without building

Supported Platforms

Linux (x86_64, gcc) and Windows (x64, MSVC/MinGW). 32-bit not supported.

Examples

The GitHub repository includes build examples (not in the PyPI sdist):

  • examples/kissfft_wrap/ — real + complex FFT over float buffers
  • examples/lz4_wrap/ — compress/decompress over byte buffers
  • examples/simd_dispatch/ — CPU feature dispatch (SSE2/AVX2/AVX-512, NEON, Altivec)
  • examples/threading_bench/ — GIL release, free-threading, OpenMP
  • examples/wheel_demo/ — multi-platform wheel packaging
  • examples/cmake_demo/, examples/meson_demo/ — build system integration

Documentation

  • docs/specification.md — full .c2py grammar, supported types, architecture
  • docs/user_guide.md — thread safety, wheel packaging, GIL release
  • AGENTS.md — contributor guide, CI workflows, coding standards
  • audit/ — LLM peer review toolchain

License

MIT. See 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

c2py23-0.4.0.tar.gz (112.6 kB view details)

Uploaded Source

Built Distribution

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

c2py23-0.4.0-py3-none-any.whl (62.0 kB view details)

Uploaded Python 3

File details

Details for the file c2py23-0.4.0.tar.gz.

File metadata

  • Download URL: c2py23-0.4.0.tar.gz
  • Upload date:
  • Size: 112.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for c2py23-0.4.0.tar.gz
Algorithm Hash digest
SHA256 542796907f95a9a1e73bd8f9d1d624e844cc9509b2e04bc528a838efb1009c9c
MD5 084a5e620842a7f83a82e51c6c171277
BLAKE2b-256 2f20260d4b217072910ee777db4522b08843b2a60ee4226e36dc9d7bcc9cf6eb

See more details on using hashes here.

File details

Details for the file c2py23-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: c2py23-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 62.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for c2py23-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 324189ec5ae41d901022c20eae2922f8b4b1a135a524680cc837e0a3a1303c1c
MD5 4c3f607be79322db4c5f569cf77cd252
BLAKE2b-256 efae132343fbf3db5305f41fe073c7ef8bfc77911b2b553e29cad6f61d8f8563

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