PythoC: A New Way to Generate C Code from Python
PythoC introduces a new method for generating C code using Python, offering more flexibility and compile-time features than Cython. Python and C are closely connected, and PythoC expands this relationship by letting developers write Python that produces standalone C programs.
How PythoC Works
Instead of generating Python extension modules like Cython, PythoC uses type-hinted Python to create pure C source files. Functions are decorated with @compile, and developers use machine-level types such as i32 instead of Python’s int.
Simple PythoC Example
from pythoc import compile, i32
@compile
def add(x: i32, y: i32) -> i32:
return x + y
print(add(10, 20))
Generating a Standalone Executable
from pythoc import compile, i32, ptr, i8
from pythoc.libc.stdio import printf
@compile
def add(x: i32, y: i32) -> i32:
return x + y
@compile
def main(argc: i32, argv: ptr[ptr[i8]]) -> i32:
printf("%u\n", add(10, 20))
from pythoc import compile_to_executable
compile_to_executable()
C Feature Support
PythoC supports pointers, arrays, structs, unions, enums, and standard control flow. Only a few features like goto and variable-length arrays are not available yet.
Compile-Time Code Generation
PythoC allows creating type-specialized versions of classes and functions at compile time using suffixes. This enables polymorphism-like behavior in C.
Memory Safety Tools
PythoC adds linear types for safe memory allocation and refinement types for checks like non-null validation, reducing common C memory errors.
Future Possibilities
PythoC may eventually support caching compiled modules or deeper integration with Python’s build system. Its compile-time capabilities make it powerful for producing high-performance standalone applications.


0 Comments