cshape
A dependency-free C AST + printer for Python: build a tree and get C11
source back. No parsing, no dependencies beyond the standard library —
just CType* / CValue* / CStmt* nodes and a render() function that
turns them into text.
Originally the C backend's printer for the Modest compiler, split out because it doesn't know anything about Modest — it only knows about C.
Install
pip install cshape
Example
from cshape import *
func = CStmtDefFunc(
id="add",
type=CTypeFunction(
params=[CField(id="a", type=CTypeIdentifier("int")),
CField(id="b", type=CTypeIdentifier("int"))],
to=CTypeIdentifier("int"),
),
block=CStmtBlock([
CStmtReturn(CValueAdd(CValueIdentifier("a"), CValueIdentifier("b"))),
]),
)
print(render(func).strip())
int add(int a, int b) {
return a + b;
}
(render() returns the node's own leading blank lines too — CStmtDefFunc
starts with a blank line by default, so .strip() if you're printing a
single node standalone rather than concatenating several.)
Documentation
- docs/api.md - documentation index
- docs/quickstart.md - getting started and first example
- docs/reference.md - current API reference
What it covers
- Types: named types, pointers, arrays, function types, structs/unions, enums
- Values: the full C11 expression grammar with correct operator precedence and parenthesization (including the extra parens GCC/Clang warn about for mixed bitwise/shift expressions)
- Statements: blocks, if/while, return, break/continue, inline asm, raw inserts
- Preprocessor:
#define,#undef,#include,#if/#elif/#else/#endifregions - Two brace styles (
legacy— K&R-ish,modern— Allman-ish), configurable viarender(node, style=...) - A
.markfield on every node: set it to any string and it shows up as a/*mark*/comment right before that node in the output — useful for tracing which part of your own compiler/generator produced which bit of C - A
.notefield on every value, printed as a/* note */comment after the value — for saying where a value came from once it no longer shows:3.125 /* totalVolume / elementVolume */
What it doesn't do
No parsing, no semantic analysis, no type checking. It's a printer, not a compiler frontend — you build the tree, it hands you back text.
License
MIT
Metadata
Release files for cshape 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| cshape-0.2.3.tar.gz | 11.3 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cshape-0.2.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.6 kB
Release files / cshape-0.2.3.tar.gz
| Download URL | cshape-0.2.3.tar.gz |
|---|---|
| Size | 11.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1a1818927d5d49deb5cff4ec2f0f09257927b10c6b3ec878dc33a1c96daa2242
|
|
BLAKE2b-256 checksum How to use checksums |
e166841b494da9aa3d9a11fd4bf9d93490798ad230fdf20600a95dd8fcd80eea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.
Transparency logRelease files / cshape-0.2.3-py3-none-any.whl
| Download URL | cshape-0.2.3-py3-none-any.whl |
|---|---|
| Size | 10.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2b2246d9aa025946741d53438030be2b7fcf2f328840c8385c53b345945ac095
|
|
BLAKE2b-256 checksum How to use checksums |
812d896f3ed036266e5717094e429a72833d2f84aeff397412f6e96f45a7d6b9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.
Transparency logSecurity for cshape 0.2.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for cshape-0.2.3.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 12, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.