Skip to content

Commit cc25b49

Browse files
committed
Initialize _io.py in the vm
1 parent 8951e1d commit cc25b49

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
@@ -77,7 +77,7 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
7777
"errno".to_owned() => Box::new(errno::make_module),
7878
"hashlib".to_owned() => Box::new(hashlib::make_module),
7979
"itertools".to_owned() => Box::new(itertools::make_module),
80-
"_io".to_owned() => Box::new(io::make_module),
80+
"_rust_io".to_owned() => Box::new(io::make_module),
8181
"json".to_owned() => Box::new(json::make_module),
8282
"marshal".to_owned() => Box::new(marshal::make_module),
8383
"math".to_owned() => Box::new(math::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
@@ -235,11 +235,32 @@ impl VirtualMachine {
235235
#[cfg(not(target_arch = "wasm32"))]
236236
import::import_builtin(self, "signal")?;
237237

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

240262
#[cfg(not(target_arch = "wasm32"))]
241263
{
242-
let io = self.import("io", &[], 0)?;
243264
let io_open = self.get_attribute(io.clone(), "open")?;
244265
let set_stdio = |name, fd, mode: &str| {
245266
let stdio = self.invoke(
@@ -1052,6 +1073,12 @@ impl VirtualMachine {
10521073
self.call_codec_func("encode", obj, encoding, errors)
10531074
}
10541075

1076+
pub fn io_open(&self, args: PyFuncArgs) -> PyResult {
1077+
let io = self.import("io", &[], 0)?;
1078+
let io_open = self.get_attribute(io, "open")?;
1079+
self.invoke(&io_open, args)
1080+
}
1081+
10551082
pub fn _sub(&self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
10561083
self.call_or_reflection(a, b, "__sub__", "__rsub__", |vm, a, b| {
10571084
Err(vm.new_unsupported_operand_error(a, b, "-"))

0 commit comments

Comments
 (0)