diff --git a/Lib/_sitebuiltins.py b/Lib/_sitebuiltins.py index 18c1a1584..3e07ead16 100644 --- a/Lib/_sitebuiltins.py +++ b/Lib/_sitebuiltins.py @@ -8,13 +8,8 @@ The objects used by the site module to add custom builtins. # Note this means this module should also avoid keep things alive in its # globals. -import os import sys -sys.stdin = sys.__stdin__ = getattr(sys, '__stdin__', False) or os.fdopen(0, "r") -sys.stdout = sys.__stdout__ = getattr(sys, '__stdout__', False) or os.fdopen(1, "w") -sys.stderr = sys.__stderr__ = getattr(sys, '__stderr__', False) or os.fdopen(2, "w") - class Quitter(object): def __init__(self, name, eof): diff --git a/vm/src/builtins.rs b/vm/src/builtins.rs index 6f7bcb5f9..0ad3b37e9 100644 --- a/vm/src/builtins.rs +++ b/vm/src/builtins.rs @@ -32,8 +32,6 @@ use crate::pyobject::{ }; use crate::scope::Scope; use crate::stdlib::ast; -#[cfg(not(target_arch = "wasm32"))] -use crate::stdlib::io::io_open; use crate::vm::VirtualMachine; fn builtin_abs(x: PyObjectRef, vm: &VirtualMachine) -> PyResult { @@ -757,11 +755,6 @@ fn builtin_vars(obj: OptionalArg, vm: &VirtualMachine) -> PyResult { pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) { let ctx = &vm.ctx; - #[cfg(target_arch = "wasm32")] - let open = vm.ctx.none(); - #[cfg(not(target_arch = "wasm32"))] - let open = vm.ctx.new_function(io_open); - #[cfg(feature = "rustpython-compiler")] { extend_module!(vm, module, { @@ -817,7 +810,6 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) { "min" => ctx.new_function(builtin_min), "object" => ctx.object(), "oct" => ctx.new_function(builtin_oct), - "open" => open, "ord" => ctx.new_function(builtin_ord), "next" => ctx.new_function(builtin_next), "pow" => ctx.new_function(builtin_pow), diff --git a/vm/src/vm.rs b/vm/src/vm.rs index ce82da63d..a480fb4cf 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -231,13 +231,41 @@ impl VirtualMachine { builtins::make_module(self, self.builtins.clone()); sysmodule::make_module(self, self.sys_module.clone(), self.builtins.clone()); - #[cfg(not(target_arch = "wasm32"))] - import::import_builtin(self, "signal").expect("Couldn't initialize signal module"); + let inner_init = || -> PyResult<()> { + #[cfg(not(target_arch = "wasm32"))] + import::import_builtin(self, "signal")?; - self.expect_pyresult( - import::init_importlib(self, initialize_parameter), - "Initialize importlib fail", - ); + import::init_importlib(self, initialize_parameter)?; + + #[cfg(not(target_arch = "wasm32"))] + { + let io = self.import("io", &[], 0)?; + let io_open = self.get_attribute(io.clone(), "open")?; + let set_stdio = |name, fd, mode: &str| { + let stdio = self.invoke( + &io_open, + vec![self.new_int(fd), self.new_str(mode.to_owned())], + )?; + self.set_attr( + &self.sys_module, + format!("__{}__", name), // e.g. __stdin__ + stdio.clone(), + )?; + self.set_attr(&self.sys_module, name, stdio)?; + Ok(()) + }; + set_stdio("stdin", 0, "r")?; + set_stdio("stdout", 1, "w")?; + set_stdio("stderr", 2, "w")?; + + let open_wrapper = self.get_attribute(io, "OpenWrapper")?; + self.set_attr(&self.builtins, "open", open_wrapper)?; + } + + Ok(()) + }; + + self.expect_pyresult(inner_init(), "initializiation failed"); self.initialized = true; }