From c68b395c0a11dbe211b4eabaed2947e9870a74bf Mon Sep 17 00:00:00 2001 From: Marco Neumann Date: Mon, 22 Sep 2025 16:34:05 +0200 Subject: [PATCH] fix: compilation on wasip2 WASI strings are UTF8, so we don't need the nightly-only APIs. See https://github.com/rust-lang/rust/issues/130323 as well. --- newsfragments/5368.fixed.md | 1 + src/conversions/std/osstr.rs | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 newsfragments/5368.fixed.md diff --git a/newsfragments/5368.fixed.md b/newsfragments/5368.fixed.md new file mode 100644 index 00000000000..837a7398e62 --- /dev/null +++ b/newsfragments/5368.fixed.md @@ -0,0 +1 @@ +Fix compilation on `wasm32-wasip2`. diff --git a/src/conversions/std/osstr.rs b/src/conversions/std/osstr.rs index eb132a58f69..b30a3a5cc36 100644 --- a/src/conversions/std/osstr.rs +++ b/src/conversions/std/osstr.rs @@ -23,7 +23,7 @@ impl<'py> IntoPyObject<'py> for &OsStr { #[cfg(not(windows))] { #[cfg(target_os = "wasi")] - let bytes = std::os::wasi::ffi::OsStrExt::as_bytes(self); + let bytes = self.to_str().expect("wasi strings are UTF8").as_bytes(); #[cfg(not(target_os = "wasi"))] let bytes = std::os::unix::ffi::OsStrExt::as_bytes(self); @@ -87,9 +87,11 @@ impl FromPyObject<'_, '_> for OsString { }; // Create an OsStr view into the raw bytes from Python + // + // For WASI: OS strings are UTF-8 by definition. #[cfg(target_os = "wasi")] let os_str: &OsStr = - std::os::wasi::ffi::OsStrExt::from_bytes(fs_encoded_bytes.as_bytes(ob.py())); + OsStr::new(std::str::from_utf8(fs_encoded_bytes.as_bytes(ob.py()))?); #[cfg(not(target_os = "wasi"))] let os_str: &OsStr = std::os::unix::ffi::OsStrExt::from_bytes(fs_encoded_bytes.as_bytes(ob.py()));