Architecture¶
For contributors and anyone who wants to understand how the runtime is structured. To run Python in a browser tab, Getting Started is shorter. For compatibility with MicroPython, see vs MicroPython.
Overview¶
petitpy has two halves that share one source tree:
- The compiler (
src/compiler/) is pure Zig. It lexes and parses Python source, runs scope analysis and emits MicroPython.mpybytecode. The parser is a Zig port of MicroPython'sparse.c. Its grammar is upstream'sgrammar.h, patched atmake porttime (port/grammar-patched/) to accept{**d}dict unpacking and PEP 570 positional-only/markers. It ships as the standalonezigpy-compilebinary and is linked into the interpreter. - The VM (
src/runtime/,src/objects/,src/modules/,src/memory/) loads bytecode, runs the bytecode interpreter, and implements the object model, the allocator and the garbage collector. It is Zig apart from the residual C described below.
The main binary (zig-out/bin/petitpy) contains both and calls the compiler at run time to compile source. A VM-only binary, petitpy-vm, loads .mpy files. Keeping the compiler separate means it can be tested on its own, and the VM can be checked against bytecode from MicroPython's own mpy-cross.
The ADR series records the decisions behind this page.
How it was built¶
The codebase is the result of an incremental C-to-Zig strangler of MicroPython, checked against a byte-exact differential test corpus. For each MicroPython subsystem, a Zig module was written with the same link surface as the vendor C file (function signatures and struct layouts), so the C file could be dropped from the build and the Zig module linked in its place. After every change the corpus ran against frozen reference output, and any byte of drift failed the build. This link contract is internal: downstream code should not link against it. How it was built tells the story.
The two configurations¶
port/mpconfigport.h has two arms, selected with -Dconfig=...:
full(the default) turns on everything available and tracks upstream MicroPython's unix port. Features that have not been ported to Zig, such as bignums (MPZ),ioand warnings, come from vendor C files thatbuild.zigadds under this config (bignums on wasm too).nanois the minimal arm. Its output must stay byte-identical to the frozen MicroPython reference, which keeps the surface that has to match small. The wasm targets are built asfull(ADR 023);make web WASM_CONFIG=nanogives a smaller nano build that no gate checks.
Per-feature build flags¶
-Dfeature-* flags (ADR 002) turn individual modules on or off on top of the preset: math, collections, struct, array, errno, micropython, gc-module, re, warning, the JS bridge (js, wasm only) and the POSIX modules of the full native build (os, time, socket, select, signal, pwd-grp, subprocess). zig build --help lists them with their defaults. Moving a flag away from its preset default prints a warning that the corpus gate does not cover the resulting build.
Targets¶
Native builds run on macOS and Linux, on arm64 and x86_64; CI also cross-compiles for aarch64-linux. The wasm target is wasm32-wasi, run under wasmtime or in the browser through the JS bridge.
The PyError convention¶
Errors propagate through Zig error unions, replacing MicroPython's setjmp/longjmp non-local return (NLR):
pub const PyError = error{PyRaise};
// Internal fns return PyError!T
fn computeImpl(...) PyError!Obj {
if (bad) try raiseTypeError(...);
return result;
}
// C-ABI exports bridge via pyerr.bridge
pub export fn npy_compute(...) callconv(.c) Obj {
return pyerr.bridge(Obj, computeImpl(...));
}
The raised exception is stored in pyerr.pending_exc. At a C boundary, pyerr.bridge(T, r) turns error.PyRaise into a zero value (MP_OBJ_NULL for Obj), and the C caller checks pending_exc. Dropping setjmp is what made the wasm target practical, since wasm has no native setjmp. See Internals: PyError Convention.
Type mirrors¶
src/mp_mirrors.zig declares Zig extern struct types that mirror vendor MicroPython C structs field for field, so Zig reads and writes the same memory as the C code, by name:
pub const MpRawCode = extern struct {
proto_fun_indicator: [2]u8,
kind: u8,
is_generator: bool,
fun_data: ?*const anyopaque,
children: [*c]?*MpRawCode,
};
Comptime @sizeOf and @offsetOf checks make a layout mismatch fail the build instead of corrupting memory at run time. See Internals: Type Mirrors.
The residual C¶
The core interpreter is Zig: lexer, parser, compiler, emitter, .mpy loader, VM, object model, allocator and GC. Some C remains.
Vendor C still compiles for features that were never ported. On both configs this is float (py/objfloat.c, py/formatfloat.c) and the re module (extmod/modre.c). Full, and nano on wasm, add MPZ bignums; full also adds io, help() and warnings. These are well-tuned upstream code, and reusing them costs less than rewriting them. The lists live in build.zig.
Three project-local C files also compile into every build:
src/objects/objtype/objmodule_shim.cholds the built-in module table. It expandsMICROPY_REGISTERED_MODULES, a macro that MicroPython's build tooling generates and that only resolves inside a C file (see qstr Generation).src/objects/strings/va_ops.cholds the functions that take ava_list(mp_printf,mp_vprintf,vstr_printfand the formatted exception constructors), because Zig 0.16 refusesva_liston aarch64-linux.src/runtime/nlr_trampoline.cis thesetjmppad from ADR 013. It lets exceptions raised inside the full config's vendor C reach Pythontry/except. The pad is active only on full native builds; elsewhere the wrappers forward the call.
Testing¶
Each gate answers a different question: does nano still match the frozen reference byte for byte, does full match its documented baseline, does wasm, does the binary actually start and run real programs, and do the compiler and VM agree with MicroPython's own when mixed. Build & Test lists the commands. The golden programs are a separate collection of real Python programs; COMPATIBILITY.md is generated from them.