USING PYCForge
Convert a Python file from a terminal, or supply source text directly through the Python API.
# Convert a file and write C pycforge convert input.py --output generated.c # Check source without saving C pycforge validate --source input.py # Print the conversion result as JSON pycforge --format json convert input.py # See installed command options pycforge convert --help
If conversion fails, PyCForge reports the problem and leaves an existing output file untouched. Do not assume that an older file at the destination contains the latest conversion. Check the command's success status and diagnostics.
from pycforge import ConversionRequest, PythonToCConverter, ResultStatus
source = """def add(left: int, right: int) -> int:
return left + right
"""
result = PythonToCConverter().convert(
ConversionRequest.from_source(source)
)
if result.status == ResultStatus.CONVERTED:
print(result.generated_c)
else:
for diagnostic in result.diagnostics:
print(diagnostic.code, diagnostic.message)
The default request enables the current variable and string support. Inspect the result status before saving output. Conversion does not execute the supplied Python.
from pycforge import (
ConversionRequest, PythonToCConverter,
SourceBundle, SourceDocumentInput,
)
main_text = """from math_tools import double
def run(value: int) -> int:
return double(value)
"""
tools_text = """def double(value: int) -> int:
return value * 2
"""
bundle = SourceBundle(
primary=SourceDocumentInput("main.py", main_text, "main"),
companions=(SourceDocumentInput(
"math_tools.py", tools_text, "math_tools"
),),
)
result = PythonToCConverter().convert(
ConversionRequest(source_bundle=bundle)
)
Supply every imported source file explicitly and match its module name to the import. The converter does not discover modules on disk. See Importing functions.