Skip to content
This repository has been archived by the owner on Feb 17, 2024. It is now read-only.

Latest commit

 

History

History

python

timecraft/python

CPython running on WebAssembly in timecraft.

Run

Assuming in the directory of this readme, after building.

Interpreter

timecraft run \
  -e PYTHONPATH=`pwd`/deps:`pwd`/cpython/usr/local/lib/python311.zip \
  -e PYTHONHOME=`pwd`/cpython/usr/local/lib/python311.zip \
  -- `pwd`/cpython/python.wasm

Script

timecraft run \
  -e PYTHONPATH=`pwd`/deps:`pwd`/cpython/usr/local/lib/python311.zip \
  -e PYTHONHOME=`pwd`/cpython/usr/local/lib/python311.zip \
  -- `pwd`/cpython/python.wasm \
  ./script.py arg1 arg2 etc

Dependencies

To install Python dependencies for the script you want to run, use the following command (you need pip installed in your host python):

python3 -m pip install \
  --target deps/ \
  --only-binary :all: --implementation py --abi none --platform any --python-version "3.11" \
  whateverpackage

This will store the package (if available) in ./deps, which is provided using PYTHONPATH to wasm.

Download

make download

It retrieves the most recent build of python for the current branch. To enable debug logs, read the following "Build" section.

Build

./build.sh

Produces:

  • cpython/python.wasm - the python wasm module.
  • cpython/usr/local/lib/python311.zip - the python standard library.

Requirements

or

  • Docker

If you don't want to install everything locally, you can build a docker image that contains those dependencies:

make build-docker

Configuration

The build.sh script creates a ./build directory where it stores extra dependencies in.

Clang and LLVM are expected to be on the path. Otherwise, use the following environment variables:

export CC=/usr/lib/llvm-15/bin/clang
export AR=/usr/lib/llvm-15/bin/llvm-ar
export NM=/usr/lib/llvm-15/bin/llvm-nm
export RL=/usr/lib/llvm-15/bin/llvm-ranlib

If you want to iterate faster, disable the wasm-opt pass by providing any value to NO_OPT when building.

Set the NO_WASMEDGE environment variable to build CPython without socket support.

When making any change in configuration, it is possible you may have to run make dist-clean in /python.

Debugging

  • Provide NO_OPT to build.sh to disable wasm-opt.
  • Provide DEBUG to build.sh to enable --pydebug.
  • Provide PYTHONTRACEMALLOC=1 to timecraft run to trace Python malloc calls.

Example of all those options when running tests:

NO_OPT=true DEBUG=true ./build.sh && \
timecraft run \
  -e PYTHONPATH=`pwd`/deps:`pwd`/python/usr/local/lib/python311.zip \
  -e PYTHONHOME=`pwd`/python/usr/local/lib/python311.zip \
  -e PYTHONTRACEMALLOC=1 \
  -- `pwd`/python/python.wasm ./test.py -v

Layout

/wasmedge_sock

Static library implementation of posix sockets based on the wasmedge interface. Initially from vmware. Heavily modified. Should be contributed back to them at some point.