RustPython is a python interpreter written in rust. What makes it interesting is that it does no use cpython which makes it much more straightforward to target WebAssembly and do things like execute untrusted code on a host machine.
Getting started
# Clone the repo
git clone git@github.com:RustPython/RustPython.git && cd RustPython
# Add webassembly build target
rustup target add wasm32-wasip1
# Build it
cargo build --target wasm32-wasip1 --features freeze-stdlib,stdlib --release
# Run it with wasmer
curl https://get.wasmer.io -sSfL | sh
wasmer run --dir `pwd` -- target/wasm32-wasip1/release/rustpython.wasm `pwd`/extra_tests/snippets/stdlib_random.py
Adding libraries
rustpython --install-pip
rustpython -m pip install requests
rustpython -c 'import requests; print(requests.get("https://example.com").text)'
Quirks and shortcomings
- How does one add libraries when using wasm? Where do the libraries go? That doesn’t look possible right now and trying to run
--install-pipfails. - Stdout didn’t work when running
wasmer run ./target/wasm32-wasip1/release/rustpython.wasm. I had to remove the--no-default-featuresflag when building - Wasmer doesn’t install correctly when using
cargo install wasmer-clibut it installed fine with the sweat inducingcurlpiped tosh. - No HTTP requests are possible unless you use
wasixbut that’s a private company’s own standard. It will be possible via the wasm32-wasip2 target which uses webassembly’s component model. - I don’t see this being a viable, general purpose AI code sandbox anytime soon .