Cython Writer¶
The Cython writer generates .pxd declaration files from headerkit IR. It
supports the full range of C and C++ declarations including structs, enums,
functions, typedefs, namespaces, templates, and operator aliasing. Python and
Cython keywords are automatically escaped with a _ suffix and a C name alias.
Writer Class¶
CythonWriter
¶
Convenience Function¶
write_pxd
¶
Convert an IR Header to Cython .pxd string.
Convenience function that creates a :class:PxdWriter and calls
:meth:~PxdWriter.write.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
header
|
Header
|
Parsed header in IR format. |
required |
stub_cimport_prefix
|
str | None
|
If set, emit cimport lines for stub types
using this dotted module prefix (e.g. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Complete |
Internal Writer¶
The PxdWriter class handles the actual conversion logic. It is created
internally by CythonWriter and
write_pxd.
PxdWriter
¶
Writes IR to Cython .pxd format.
Converts a :class:~headerkit.ir.Header containing parsed C/C++
declarations into valid Cython .pxd syntax. Handles keyword
escaping, stdint imports, topological sorting, circular dependency
detection, C++ namespaces, templates, operator aliasing, and
automatic cimport generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
header
|
Header
|
The parsed header to convert. |
required |
Example¶
from headerkit.backends import get_backend
from headerkit.writers import get_writer
backend = get_backend()
header = backend.parse("""
typedef struct {
int x;
int y;
} Point;
int distance(const Point* a, const Point* b);
""", "geometry.h")
writer = get_writer("cython")
print(writer.write(header))
Output: