Skip to content

Commit bc2e681

Browse files
committed
Initialize _io.py in the vm
1 parent 55b0814 commit bc2e681

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

vm/src/stdlib/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
7878
"errno".to_owned() => Box::new(errno::make_module),
7979
"hashlib".to_owned() => Box::new(hashlib::make_module),
8080
"itertools".to_owned() => Box::new(itertools::make_module),
81-
"_io".to_owned() => Box::new(io::make_module),
81+
"_rust_io".to_owned() => Box::new(io::make_module),
8282
"marshal".to_owned() => Box::new(marshal::make_module),
8383
"math".to_owned() => Box::new(math::make_module),
8484
"_operator".to_owned() => Box::new(operator::make_module),

vm/src/stdlib/subprocess.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::obj::objlist::PyListRef;
1212
use crate::obj::objstr::{self, PyStringRef};
1313
use crate::obj::objtype::PyClassRef;
1414
use crate::pyobject::{Either, IntoPyObject, PyObjectRef, PyRef, PyResult, PyValue};
15-
use crate::stdlib::io::io_open;
1615
use crate::stdlib::os::{convert_io_error, raw_file_number, rust_file};
1716
use crate::vm::VirtualMachine;
1817

@@ -94,8 +93,7 @@ fn convert_redirection(arg: Option<i64>, vm: &VirtualMachine) -> PyResult<subpro
9493

9594
fn convert_to_file_io(file: &Option<File>, mode: String, vm: &VirtualMachine) -> PyResult {
9695
match file {
97-
Some(ref stdin) => io_open(
98-
vm,
96+
Some(ref stdin) => vm.io_open(
9997
vec![
10098
vm.new_int(raw_file_number(stdin.try_clone().unwrap())),
10199
vm.new_str(mode),

vm/src/vm.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,32 @@ impl VirtualMachine {
237237
#[cfg(not(target_arch = "wasm32"))]
238238
import::import_builtin(self, "signal")?;
239239

240-
import::init_importlib(self, initialize_parameter)?;
240+
let io = {
241+
let module_name = "_io";
242+
let attrs = self.ctx.new_dict();
243+
let io_code = self.frozen.borrow().get(module_name).unwrap().code.clone();
244+
245+
attrs.set_item("__name__", self.new_str(module_name.to_owned()), self)?;
246+
247+
let module = self.new_module(module_name, attrs.clone());
248+
249+
// Store module in cache to prevent infinite loop with mutual importing libs:
250+
let sys_modules = self.get_attribute(self.sys_module.clone(), "modules")?;
251+
sys_modules.set_item(module_name, module.clone(), self)?;
252+
253+
import::init_importlib(self, initialize_parameter)?;
254+
255+
// Execute main code in module:
256+
self.run_code_obj(
257+
PyCode::new(io_code).into_ref(self),
258+
Scope::with_builtins(None, attrs, self),
259+
)?;
260+
261+
module
262+
};
241263

242264
#[cfg(not(target_arch = "wasm32"))]
243265
{
244-
let io = self.import("io", &[], 0)?;
245266
let io_open = self.get_attribute(io.clone(), "open")?;
246267
let set_stdio = |name, fd, mode: &str| {
247268
let stdio = self.invoke(
@@ -1082,6 +1103,12 @@ impl VirtualMachine {
10821103
self.call_codec_func("encode", obj, encoding, errors)
10831104
}
10841105

1106+
pub fn io_open(&self, args: PyFuncArgs) -> PyResult {
1107+
let io = self.import("io", &[], 0)?;
1108+
let io_open = self.get_attribute(io, "open")?;
1109+
self.invoke(&io_open, args)
1110+
}
1111+
10851112
pub fn _sub(&self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
10861113
self.call_or_reflection(a, b, "__sub__", "__rsub__", |vm, a, b| {
10871114
Err(vm.new_unsupported_operand_error(a, b, "-"))

0 commit comments

Comments
 (0)