Skip to content

Commit db65c8d

Browse files
committed
create_stdio in vm initialization
1 parent a70294d commit db65c8d

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

Lib/_sitebuiltins.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@
88
# Note this means this module should also avoid keep things alive in its
99
# globals.
1010

11-
import os
1211
import sys
1312

14-
sys.stdin = sys.__stdin__ = getattr(sys, '__stdin__', False) or os.fdopen(0, "r")
15-
sys.stdout = sys.__stdout__ = getattr(sys, '__stdout__', False) or os.fdopen(1, "w")
16-
sys.stderr = sys.__stderr__ = getattr(sys, '__stderr__', False) or os.fdopen(2, "w")
17-
1813

1914
class Quitter(object):
2015
def __init__(self, name, eof):

vm/src/builtins.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ use crate::pyobject::{
3232
};
3333
use crate::scope::Scope;
3434
use crate::stdlib::ast;
35-
#[cfg(not(target_arch = "wasm32"))]
36-
use crate::stdlib::io::io_open;
3735
use crate::vm::VirtualMachine;
3836

3937
fn builtin_abs(x: PyObjectRef, vm: &VirtualMachine) -> PyResult {
@@ -757,11 +755,6 @@ fn builtin_vars(obj: OptionalArg, vm: &VirtualMachine) -> PyResult {
757755
pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
758756
let ctx = &vm.ctx;
759757

760-
#[cfg(target_arch = "wasm32")]
761-
let open = vm.ctx.none();
762-
#[cfg(not(target_arch = "wasm32"))]
763-
let open = vm.ctx.new_function(io_open);
764-
765758
#[cfg(feature = "rustpython-compiler")]
766759
{
767760
extend_module!(vm, module, {
@@ -817,7 +810,6 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
817810
"min" => ctx.new_function(builtin_min),
818811
"object" => ctx.object(),
819812
"oct" => ctx.new_function(builtin_oct),
820-
"open" => open,
821813
"ord" => ctx.new_function(builtin_ord),
822814
"next" => ctx.new_function(builtin_next),
823815
"pow" => ctx.new_function(builtin_pow),

vm/src/vm.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,41 @@ impl VirtualMachine {
231231
builtins::make_module(self, self.builtins.clone());
232232
sysmodule::make_module(self, self.sys_module.clone(), self.builtins.clone());
233233

234-
#[cfg(not(target_arch = "wasm32"))]
235-
import::import_builtin(self, "signal").expect("Couldn't initialize signal module");
234+
let inner_init = || -> PyResult<()> {
235+
#[cfg(not(target_arch = "wasm32"))]
236+
import::import_builtin(self, "signal")?;
237+
238+
import::init_importlib(self, initialize_parameter)?;
239+
240+
#[cfg(not(target_arch = "wasm32"))]
241+
{
242+
let io = self.import("io", &[], 0)?;
243+
let io_open = self.get_attribute(io.clone(), "open")?;
244+
let set_stdio = |name, fd, mode: &str| {
245+
let stdio = self.invoke(
246+
&io_open,
247+
vec![self.new_int(fd), self.new_str(mode.to_owned())],
248+
)?;
249+
self.set_attr(
250+
&self.sys_module,
251+
format!("__{}__", name), // e.g. __stdin__
252+
stdio.clone(),
253+
)?;
254+
self.set_attr(&self.sys_module, name, stdio)?;
255+
Ok(())
256+
};
257+
set_stdio("stdin", 0, "r")?;
258+
set_stdio("stdout", 1, "w")?;
259+
set_stdio("stderr", 2, "w")?;
260+
261+
let open_wrapper = self.get_attribute(io, "OpenWrapper")?;
262+
self.set_attr(&self.builtins, "open", open_wrapper)?;
263+
}
264+
265+
Ok(())
266+
};
236267

237-
self.expect_pyresult(
238-
import::init_importlib(self, initialize_parameter),
239-
"Initialize importlib fail",
240-
);
268+
self.expect_pyresult(inner_init(), "initializiation failed");
241269

242270
self.initialized = true;
243271
}

0 commit comments

Comments
 (0)