From fccb0d2adedee314b2c00525f577a262e2497029 Mon Sep 17 00:00:00 2001 From: lynskylate Date: Wed, 7 Aug 2019 23:48:53 +0800 Subject: [PATCH] Disable socket import on wasm --- vm/Cargo.toml | 4 +++- vm/src/stdlib/mod.rs | 1 + vm/src/stdlib/socket.rs | 13 +------------ 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/vm/Cargo.toml b/vm/Cargo.toml index d5412bbe21..64b9134d5a 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -24,7 +24,6 @@ sha2 = "0.8" sha3 = "0.8" blake2 = "0.8" -gethostname = "0.2.0" num-complex = { version = "0.2", features = ["serde"] } num-bigint = { version = "0.2", features = ["serde"] } num-traits = "=0.2.6" @@ -70,3 +69,6 @@ flamer = { version = "0.3", optional = true } [target.'cfg(all(unix, not(any(target_os = "android", target_os = "redox"))))'.dependencies] pwd = "1" + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +gethostname = "0.2.0" diff --git a/vm/src/stdlib/mod.rs b/vm/src/stdlib/mod.rs index dea27dfe08..2637ade1e7 100644 --- a/vm/src/stdlib/mod.rs +++ b/vm/src/stdlib/mod.rs @@ -17,6 +17,7 @@ mod platform; mod pystruct; mod random; mod re; +#[cfg(not(target_arch = "wasm32"))] pub mod socket; mod string; #[cfg(feature = "rustpython-compiler")] diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index e30bdd62e9..22f431a82b 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -5,9 +5,8 @@ use std::io::Write; use std::net::{Ipv4Addr, SocketAddr, TcpListener, TcpStream, ToSocketAddrs, UdpSocket}; #[cfg(unix)] -use nix::unistd::{gethostname, sethostname}; +use nix::unistd::sethostname; -#[cfg(windows)] use gethostname::gethostname; use byteorder::{BigEndian, ByteOrder}; @@ -382,16 +381,6 @@ fn get_addr_tuple(vm: &VirtualMachine, addr: SocketAddr) -> PyResult { Ok(vm.ctx.new_tuple(vec![ip, port])) } -#[cfg(unix)] -fn socket_gethostname(vm: &VirtualMachine) -> PyResult { - let mut buf = [0u8; 1024]; - match gethostname(&mut buf) { - Ok(cstr) => Ok(vm.new_str(String::from(cstr.to_str().unwrap()))), - Err(e) => Err(convert_nix_error(vm, e)), - } -} - -#[cfg(windows)] fn socket_gethostname(vm: &VirtualMachine) -> PyResult { gethostname() .into_string()