Skip to content

Commit 6a391f5

Browse files
committed
populate os.environ at runtime
Since Python populates `os.environ` in the Wizer init function, we need to clear that out and replace it with the runtime environment prior to calling into the app. Signed-off-by: Joel Dice <[email protected]>
1 parent d7a838e commit 6a391f5

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
.PHONY: build
22
build: target/config.txt target/lib
33
PYO3_CONFIG_FILE=$$(pwd)/target/config.txt cargo build --release --target=wasm32-wasi
4-
wasi-vfs/target/release/wasi-vfs pack target/wasm32-wasi/release/python_wasi.wasm \
4+
env -i wasi-vfs/target/release/wasi-vfs pack target/wasm32-wasi/release/python_wasi.wasm \
55
--mapdir lib::$$(pwd)/target/lib \
66
-o target/wasm32-wasi/release/python-wasi-vfs.wasm
7-
PYTHONUNBUFFERED=1 PYTHONPATH=/py wizer target/wasm32-wasi/release/python-wasi-vfs.wasm \
7+
env -i PYTHONUNBUFFERED=1 PYTHONPATH=/py $$(which wizer) target/wasm32-wasi/release/python-wasi-vfs.wasm \
88
--inherit-env true --wasm-bulk-memory true --allow-wasi --dir py \
99
-o target/wasm32-wasi/release/python-wasi-vfs-wizer.wasm
1010

py/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
from spin_http import Request, Response, http_send
22
from spin_redis import redis_get, redis_set
33
from spin_config import config_get
4+
from os import environ
45

56
def handle_request(request):
67
print(f"Got request URI: {request.uri}")
78

9+
print(f"Here's my environment: {environ}")
10+
811
response = http_send(Request("GET", "https://some-random-api.ml/facts/dog", [], None))
912
print(f"Got dog fact: {str(response.body, 'utf-8')}")
1013

spin.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ version = "0.1.0"
77

88
[[component]]
99
id = "spin-webrtc"
10+
environment = { hello = "teapot" }
1011
config = { redis_address = "redis://127.0.0.1:6379" }
1112
source = "target/wasm32-wasi/release/python-wasi-vfs-wizer.wasm"
1213
allowed_http_hosts = ["insecure:allow-all"]

src/lib.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ use {
77
once_cell::unsync::OnceCell,
88
pyo3::{
99
exceptions::PyAssertionError,
10-
types::{PyBytes, PyModule},
10+
types::{PyBytes, PyMapping, PyModule},
1111
Py, PyErr, PyObject, PyResult, Python,
1212
},
1313
spin_sdk::{
1414
config,
1515
http::{Request, Response},
1616
outbound_http, redis,
1717
},
18-
std::{ops::Deref, str},
18+
std::{env, ops::Deref, str},
1919
};
2020

2121
thread_local! {
2222
static HANDLE_REQUEST: OnceCell<PyObject> = OnceCell::new();
23+
static ENVIRON: OnceCell<Py<PyMapping>> = OnceCell::new();
2324
}
2425

2526
fn bytes(py: Python<'_>, src: &[u8]) -> PyResult<Py<PyBytes>> {
@@ -203,6 +204,24 @@ fn do_init() -> Result<()> {
203204
cell.set(py.import("app")?.getattr("handle_request")?.into())
204205
.unwrap();
205206

207+
Ok::<_, PyErr>(())
208+
})?;
209+
210+
ENVIRON.with(|cell| {
211+
let environ = py
212+
.import("os")?
213+
.getattr("environ")?
214+
.downcast::<PyMapping>()
215+
.unwrap();
216+
217+
let keys = environ.keys()?;
218+
219+
for i in 0..keys.len()? {
220+
environ.del_item(keys.get_item(i)?)?;
221+
}
222+
223+
cell.set(environ.into()).unwrap();
224+
206225
Ok(())
207226
})
208227
})
@@ -242,6 +261,16 @@ fn handle(request: Request) -> Result<Response> {
242261
.transpose()?,
243262
};
244263

264+
ENVIRON.with(|cell| {
265+
let environ = cell.get().unwrap().as_ref(py);
266+
267+
for (k, v) in env::vars() {
268+
environ.set_item(k, v)?;
269+
}
270+
271+
Ok::<(), PyErr>(())
272+
})?;
273+
245274
let response = HANDLE_REQUEST.with(|cell| {
246275
cell.get()
247276
.unwrap()

0 commit comments

Comments
 (0)