mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
create_stdio in vm initialization
This commit is contained in:
5
Lib/_sitebuiltins.py
vendored
5
Lib/_sitebuiltins.py
vendored
@@ -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):
|
||||
|
||||
@@ -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),
|
||||
|
||||
40
vm/src/vm.rs
40
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user