Skip to content

CLI and library API

p2w

p2w [-h] [-o OUTPUT] [-r] [-v] [-d] [-V] source
source
The Python file to compile. p2w compiles one file at a time: a module may import only math, typing and js.
-o, --output FILE
Write the WAT to FILE instead of standard output.
-r, --run
Compile, assemble with wasm-tools, and run the module under Node.js, printing its output. Ignores -o. Needs wasm-tools and Node.js 22 or later on PATH.
-v, --verbose
Report progress and the size of the generated WAT on standard error.
-d, --debug
Dump the Python AST to standard error before compiling.
-V, --version
Print the version and exit.

Exit status and errors

p2w exits with status 1 and a one-line message on standard error when:

  • the file does not exist: p2w: error: file not found: prog.py;
  • the source has a syntax error, or uses a construct p2w refuses: p2w: error: NotImplementedError: Import not supported: itertools;
  • with -r, the program raises an uncaught exception or the module traps. What the program printed before is still written to standard output.

p2w-benchmark

A benchmark harness that runs the Alioth suite on several runtimes and keeps the results in SQLite. See Benchmarking.

As a library

from p2w import compile_to_wat

wat = compile_to_wat('print("hello")')

compile_to_wat(source: str) -> str returns a complete WAT module, and raises NotImplementedError for a construct p2w refuses and SyntaxError for invalid source.

The runner and the test helpers are importable too:

from p2w.runner import run_wat, run_python, wat_to_wasm
from p2w.testing import compile_and_run, compare_outputs

print(run_wat(wat))  # assemble, run under Node.js, return standard output
same = compare_outputs("print(sum(range(10)))")[2]  # p2w and CPython agree

run_wat raises subprocess.CalledProcessError when the program fails, with what it printed in stdout and the error in stderr.