Skip to main content

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 a C99 compiler (gcc, clang, or MSVC on Windows). No Python dependencies beyond stdlib.

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:

# Generate the wrapper
c2py23 arraysum.c2py -o arraysum_wrapper.c

# Compile (dlsym mode  --  portable, no libpython)
cc -shared -fPIC -I c2py23/runtime/ arraysum_wrapper.c arraysum.c \
   c2py23/runtime/c2py_runtime.c -ldl -lm -o arraysum.so

# Or build via setuptools
python setup.py build_ext --inplace

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 file.c2py -o wrapper.c       # generate C wrapper
c2py23 file.c2py                     # print to stdout
c2py23 --version                     # print version

# Or via python -m:
python -m c2py23 file.c2py -o wrapper.c

See c2py23/setuptools_helper.py for PythonhBuildExt setuptools helper (pythonh mode only).

Supported Platforms

Linux (x86_64, aarch64, gcc/clang/tcc/zig), macOS (aarch64, clang), Windows (x64/i386, MSVC/MinGW). Free-threaded Python 3.13+ 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

  • c2py23 docs -- user guide, spec, examples, developer guide
  • docs/specification.md -- full .c2py grammar, supported types, architecture
  • docs/user_guide.md -- thread safety, wheel packaging, GIL release, timing
  • docs/getting_started.md -- installation and first build
  • docs/developer_guide.md -- testing with snakepit containers, CI, ABI matrix
  • AGENTS.md -- contributor guidelines for LLM agents

License

MIT. See LICENSE.

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.5.0.tar.gz (125.4 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.5.0-py3-none-any.whl (75.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: c2py23-0.5.0.tar.gz
  • Upload date:
  • Size: 125.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for c2py23-0.5.0.tar.gz
Algorithm Hash digest
SHA256 5fac5dd264224ab28ef1d6be795534456cdd7b5ea741fd713f71e1e18349a6c9
MD5 bae98133274d21c96b5f042f35aee395
BLAKE2b-256 c81f1d5faf8ba4c8a0fa62b82fafaac90e273f7660e1aa40d22e08ac52b9f6df

See more details on using hashes here.

File details

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

File metadata

  • Download URL: c2py23-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 75.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for c2py23-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9fb0c1968e0e269af2b5d0f1d5d3bab0daecd1c54775523ec0916cee39c2e24e
MD5 3de79f041c6f57b8928e3c5b26372a0a
BLAKE2b-256 82de049878d58006703aea21cf66fee37e73a41d8867a7f17e1312c97b4166d6

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 Sentry Error logging StatusPage Status page