Low-level C API (z3_mojo.c)¶
z3_mojo keeps Z3's full C API accessible alongside the Pythonic API.
Import z3_mojo.c whenever you want direct 1-to-1 access to all 817 Z3_*
functions, raw Z3_* handle types, manual Z3_mk_context_rc AST reference
counting, or raw C callbacks.
from z3_mojo.c import (
c_api,
c_bytes,
to_string,
null,
Lbool,
Z3_context,
Z3_ast,
)
Every Z3_* function lives on the cached CApi table returned by c_api().
The shared library libz3 is loaded once per process, and every call taking a
Z3_context automatically checks Z3_get_error_code(ctx) afterwards and raises
a Mojo Error("z3: ...") if Z3 reports an error (except destructors and the
error-query functions themselves).
When to choose z3_mojo.c¶
| Need | Recommended layer |
|---|---|
Day-to-day SMT solving, operators, Pythonic idioms, exact apn_mojo models |
Pythonic API (from z3_mojo import ...) |
Safe RAII handles with 1-to-1 Z3 method names (ctx.mk_*, solver.*) |
Safe handle API (from z3_mojo import ...) |
Custom theory propagators, OnClause, Optimize.set_on_model |
Safe callback traits (UserPropagator, ClauseHandler, ModelHandler) |
Manual AST refcounting (Z3_mk_context_rc, Z3_inc_ref, Z3_dec_ref) |
Low-level C API (from z3_mojo.c import ...) |
Raw C proof-check or fixedpoint callbacks (Z3_set_proof_check_callback, Z3_fixedpoint_add_callback) |
Low-level C API (from z3_mojo.c import ...) |
| Porting C/C++ Z3 code line-by-line | Low-level C API (from z3_mojo.c import ...) |
Lifecycle and error handling¶
When creating a context directly through z3_mojo.c:
- Create a
Z3_configwithZ3_mk_config(), pass it toZ3_mk_context(cfg)orZ3_mk_context_rc(cfg), and delete the config withZ3_del_config(cfg). - Disable Z3's default aborting error handler immediately:
api[].Z3_set_error_handler(ctx, null[NoneType]()). Without this call, Z3 exits the process on invalid API arguments instead of lettingCApiraise a MojoError. - Release every handle you create:
- Ref-counted handles (
Z3_solver,Z3_model,Z3_goal,Z3_tactic,Z3_optimize,Z3_ast_vector, etc.) must be paired withZ3_<handle>_inc_refandZ3_<handle>_dec_ref. - Under
Z3_mk_context_rc, everyZ3_ast(andZ3_sort/Z3_func_declcast viaZ3_sort_to_ast/Z3_func_decl_to_ast) requires explicitZ3_inc_refandZ3_dec_refcalls beforeZ3_del_context(ctx). - Delete the context last with
api[].Z3_del_context(ctx).
Example: Z3_mk_context_rc with manual refcounting¶
The runnable example in docs/examples/c_api_low_level.mojo solves x > 40
using only z3_mojo.c:
from z3_mojo.c import c_api, c_bytes, to_string, null, Lbool
var api = c_api()
var cfg = api[].Z3_mk_config()
var ctx = api[].Z3_mk_context_rc(cfg)
api[].Z3_del_config(cfg)
api[].Z3_set_error_handler(ctx, null[NoneType]())
var int_sort = api[].Z3_mk_int_sort(ctx)
var int_sort_ast = api[].Z3_sort_to_ast(ctx, int_sort)
api[].Z3_inc_ref(ctx, int_sort_ast)
var x_name = c_bytes("x")
var x_sym = api[].Z3_mk_string_symbol(ctx, x_name.unsafe_ptr())
var x = api[].Z3_mk_const(ctx, x_sym, int_sort)
api[].Z3_inc_ref(ctx, x)
var forty = api[].Z3_mk_int(ctx, 40, int_sort)
api[].Z3_inc_ref(ctx, forty)
var gt = api[].Z3_mk_gt(ctx, x, forty)
api[].Z3_inc_ref(ctx, gt)
var solver = api[].Z3_mk_solver(ctx)
api[].Z3_solver_inc_ref(ctx, solver)
api[].Z3_solver_assert(ctx, solver, gt)
var res = api[].Z3_solver_check(ctx, solver)
if res == Lbool.TRUE:
var model = api[].Z3_solver_get_model(ctx, solver)
api[].Z3_model_inc_ref(ctx, model)
print(to_string(api[].Z3_model_to_string(ctx, model)))
api[].Z3_model_dec_ref(ctx, model)
api[].Z3_solver_dec_ref(ctx, solver)
api[].Z3_dec_ref(ctx, gt)
api[].Z3_dec_ref(ctx, forty)
api[].Z3_dec_ref(ctx, x)
api[].Z3_dec_ref(ctx, int_sort_ast)
api[].Z3_del_context(ctx)
FFI helper functions¶
z3_mojo.c re-exports the helpers used internally by the generated layer:
c_bytes(s: String) raises -> List[Int8]: NUL-terminated byte buffer forconst char*parameters. Rejects strings containing embedded NUL bytes. Keep theList[Int8]variable alive across the C call when passing.unsafe_ptr().to_string(p: CChars) raises -> String: immediately copies a Z3-returnedconst char*into an owned MojoString. Z3 reuses internal string buffers on subsequent calls on the same context, so always copy before making another Z3 call.null[T](): typed NULL pointer for optional C parameters and disabling callbacks.to_c_uint32(v: Int)andto_c_int32(v: Int): range-checked integer conversions for Cunsignedandintparameters.
Raw C callbacks¶
For the five raw-only callback registration functions (Z3_set_proof_check_callback,
Z3_fixedpoint_init, Z3_fixedpoint_set_reduce_assign_callback,
Z3_fixedpoint_set_reduce_app_callback, Z3_fixedpoint_add_callback), pass a
def (...) abi("C") trampoline function pointer and a heap-pinned void*
state pointer. Never allow a Mojo Error to unwind across a C callback
boundary; catch exceptions inside the trampoline and check them after the Z3
call returns.
For the full catalog of all 817 functions and signatures, see the Low-Level C API Reference and C API Coverage Report.