Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion crates/spin-python-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {
clap::Parser,
std::{
env, fs,
io::Cursor,
io::{self, Cursor, Seek},
path::{Path, PathBuf},
process::Command,
str,
Expand Down Expand Up @@ -105,6 +105,17 @@ fn main() -> Result<()> {
)
}

// Spawn a subcommand to do the real work. This gives us an opportunity to clear the environment so that
// build-time environment variables don't end up in the Wasm module we're building.
//
// Note that we need to use temporary files for stdio instead of the default inheriting behavior since (as
// of this writing) CPython interacts poorly with Wasmtime's WASI implementation if any of the stdio
// descriptors point to non-files on Windows. Specifically, the WASI implementation will trap when CPython
// calls `fd_filestat_get` on non-files.

let mut stdout = tempfile::tempfile()?;
let mut stderr = tempfile::tempfile()?;

let status = Command::new(env::args().next().unwrap())
.env_clear()
.env("SPIN_PYTHON_WIZEN", "1")
Expand All @@ -116,9 +127,18 @@ fn main() -> Result<()> {
)
.arg(&python_path)
.arg(&options.output)
.stdin(tempfile::tempfile()?)
.stdout(stdout.try_clone()?)
.stderr(stderr.try_clone()?)
.status()?;

if !status.success() {
stdout.rewind()?;
io::copy(&mut stdout, &mut io::stdout().lock())?;

stderr.rewind()?;
io::copy(&mut stderr, &mut io::stderr().lock())?;

bail!("Couldn't create wasm from input");
}

Expand Down
2 changes: 1 addition & 1 deletion crates/spin-python-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ crate-type = [ "cdylib" ]
anyhow = "1"
bytes = { version = "1.2.1", features = ["serde"] }
http = "0.2"
spin-sdk = { git = "https://github.com/fermyon/spin" }
spin-sdk = { git = "https://github.com/fermyon/spin", default-features = false }
wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "dde4694aaa6acf9370206527a798ac4ba6a8c5b8" }
pyo3 = { version = "0.17.3", features = [ "abi3-py310" ] }
once_cell = "1.16.0"
3 changes: 3 additions & 0 deletions crates/spin-python-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ thread_local! {
static ENVIRON: OnceCell<Py<PyMapping>> = OnceCell::new();
}

#[export_name = "spin-sdk-language-python"]
extern "C" fn __spin_sdk_language() {}

fn bytes(py: Python<'_>, src: &[u8]) -> PyResult<Py<PyBytes>> {
Ok(PyBytes::new_with(py, src.len(), |dst| {
dst.copy_from_slice(src);
Expand Down