Skip to content

Commit f8f4392

Browse files
committed
Fix wasm32 build
1 parent 584d972 commit f8f4392

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

vm/src/exceptions.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,17 +1023,22 @@ pub(crate) fn raw_os_error_to_exc_type(errno: i32, vm: &VirtualMachine) -> Optio
10231023
}
10241024
}
10251025

1026+
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
1027+
pub(crate) fn raw_os_error_to_exc_type(_errno: i32, _vm: &VirtualMachine) -> Option<PyTypeRef> {
1028+
None
1029+
}
1030+
10261031
pub(super) mod types {
10271032
use crate::common::lock::PyRwLock;
1028-
#[cfg_attr(target_os = "wasi", allow(unused_imports))]
1033+
#[cfg_attr(target_arch = "wasm32", allow(unused_imports))]
10291034
use crate::{
10301035
builtins::{traceback::PyTracebackRef, PyInt, PyTupleRef, PyTypeRef},
10311036
convert::ToPyResult,
10321037
function::FuncArgs,
10331038
PyObjectRef, PyRef, PyResult, VirtualMachine,
10341039
};
10351040
use crossbeam_utils::atomic::AtomicCell;
1036-
#[cfg_attr(target_os = "wasi", allow(unused_imports))]
1041+
#[cfg_attr(target_arch = "wasm32", allow(unused_imports))]
10371042
use std::ops::Deref;
10381043

10391044
// This module is designed to be used as `use builtins::*;`.

vm/src/stdlib/io.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,34 @@ cfg_if::cfg_if! {
99
}
1010
}
1111

12-
use crate::{PyObjectRef, PyResult, TryFromObject, VirtualMachine};
13-
pub(crate) use _io::io_open as open;
12+
use crate::{
13+
builtins::PyBaseExceptionRef,
14+
convert::{ToPyException, ToPyObject},
15+
PyObjectRef, PyResult, TryFromObject, VirtualMachine,
16+
};
17+
pub use _io::io_open as open;
18+
19+
impl ToPyException for &'_ std::io::Error {
20+
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
21+
use std::io::ErrorKind;
22+
23+
let excs = &vm.ctx.exceptions;
24+
#[allow(unreachable_patterns)] // some errors are just aliases of each other
25+
let exc_type = match self.kind() {
26+
ErrorKind::NotFound => excs.file_not_found_error.clone(),
27+
ErrorKind::PermissionDenied => excs.permission_error.clone(),
28+
ErrorKind::AlreadyExists => excs.file_exists_error.clone(),
29+
ErrorKind::WouldBlock => excs.blocking_io_error.clone(),
30+
_ => self
31+
.raw_os_error()
32+
.and_then(|errno| crate::exceptions::raw_os_error_to_exc_type(errno, vm))
33+
.unwrap_or_else(|| excs.os_error.clone()),
34+
};
35+
let errno = self.raw_os_error().to_pyobject(vm);
36+
let msg = vm.ctx.new_str(self.to_string()).into();
37+
vm.new_exception(exc_type, vec![errno, msg])
38+
}
39+
}
1440

1541
pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
1642
let ctx = &vm.ctx;

vm/src/stdlib/os.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ use crate::{
88
VirtualMachine,
99
};
1010
use std::{
11-
ffi, fs,
12-
io::{self, ErrorKind},
11+
ffi, fs, io,
1312
path::{Path, PathBuf},
1413
};
1514

@@ -236,31 +235,6 @@ impl PathOrFd {
236235
}
237236
}
238237

239-
impl ToPyException for io::Error {
240-
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
241-
(&self).to_pyexception(vm)
242-
}
243-
}
244-
impl ToPyException for &'_ io::Error {
245-
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
246-
let excs = &vm.ctx.exceptions;
247-
#[allow(unreachable_patterns)] // some errors are just aliases of each other
248-
let exc_type = match self.kind() {
249-
ErrorKind::NotFound => excs.file_not_found_error.clone(),
250-
ErrorKind::PermissionDenied => excs.permission_error.clone(),
251-
ErrorKind::AlreadyExists => excs.file_exists_error.clone(),
252-
ErrorKind::WouldBlock => excs.blocking_io_error.clone(),
253-
_ => self
254-
.raw_os_error()
255-
.and_then(|errno| crate::exceptions::raw_os_error_to_exc_type(errno, vm))
256-
.unwrap_or_else(|| excs.os_error.clone()),
257-
};
258-
let errno = self.raw_os_error().to_pyobject(vm);
259-
let msg = vm.ctx.new_str(self.to_string()).into();
260-
vm.new_exception(exc_type, vec![errno, msg])
261-
}
262-
}
263-
264238
#[cfg(unix)]
265239
impl ToPyException for nix::Error {
266240
fn to_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {

vm/src/stdlib/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Implementation of the _thread module
2-
#[cfg_attr(target_os = "wasi", allow(unused_imports))]
2+
#[cfg_attr(target_arch = "wasm32", allow(unused_imports))]
33
pub(crate) use _thread::{make_module, RawRMutex};
44

55
#[pymodule]

0 commit comments

Comments
 (0)