Skip to content

CFFI Writer

The CFFI writer generates C declaration strings suitable for passing to ffibuilder.cdef(). It handles structs, unions, enums, functions, typedefs, variables, and integer constants.

Writer Class

CffiWriter

CffiWriter(exclude_patterns=None, define_patterns=None, extra_cdef=None)

Writer that generates CFFI cdef strings from headerkit IR.

Options

exclude_patterns : list[str] | None Regex patterns. Declarations with names matching any pattern are excluded from output. define_patterns : list[str] | None Regex patterns matched against #define names in the raw header source. Matching names are appended as #define NAME ... lines (CFFI's "resolve at verify-time" syntax). The actual matching is performed by the generate() pipeline, which sets _matched_defines before calling write(). extra_cdef : list[str] | None Arbitrary cdef lines appended verbatim after all generated output.

Example

::

from headerkit.writers import get_writer

writer = get_writer("cffi", exclude_patterns=["__.*"])
cdef_string = writer.write(header)

# Or directly:
from headerkit.writers.cffi import CffiWriter
writer = CffiWriter(exclude_patterns=["__.*"])
cdef_string = writer.write(header)

write

write(header)

Convert header IR to CFFI cdef string.

hash_comment_format

hash_comment_format()

Return format string for wrapping cache metadata in C-style comments.

Convenience Function

header_to_cffi

header_to_cffi(header, exclude_patterns=None)

Convert all declarations in a Header to a CFFI cdef string.

Parameters:

Name Type Description Default
header Header

Parsed header IR from headerkit.

required
exclude_patterns list[str] | None

List of regex patterns. Declarations with names matching any pattern will be excluded.

None

Returns:

Type Description
str

A string suitable for passing to ffibuilder.cdef().

Low-Level Functions

These functions are used internally by header_to_cffi and can be useful when working with individual declarations or type expressions.

type_to_cffi

type_to_cffi(t, tag_kinds=None)

Convert a type expression to its CFFI string representation.

decl_to_cffi

decl_to_cffi(decl, exclude_patterns=None, tag_kinds=None)

Convert a single IR declaration to a CFFI cdef string.

Returns None if the declaration should be excluded.