mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Make stdio a feature (#5420)
This commit is contained in:
@@ -10,9 +10,10 @@ repository.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[features]
|
||||
default = ["threading", "stdlib", "importlib"]
|
||||
default = ["threading", "stdlib", "stdio", "importlib"]
|
||||
importlib = ["rustpython-vm/importlib"]
|
||||
encodings = ["rustpython-vm/encodings"]
|
||||
stdio = ["rustpython-vm/stdio"]
|
||||
stdlib = ["rustpython-stdlib", "rustpython-pylib", "encodings"]
|
||||
flame-it = ["rustpython-vm/flame-it", "flame", "flamescope"]
|
||||
freeze-stdlib = ["stdlib", "rustpython-vm/freeze-stdlib", "rustpython-pylib?/freeze-stdlib"]
|
||||
|
||||
@@ -10,7 +10,8 @@ repository.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[features]
|
||||
default = ["compiler", "wasmbind"]
|
||||
default = ["compiler", "wasmbind", "stdio"]
|
||||
stdio = []
|
||||
importlib = []
|
||||
encodings = ["importlib"]
|
||||
vm-tracing-logging = []
|
||||
|
||||
@@ -14,6 +14,8 @@ mod vm_new;
|
||||
mod vm_object;
|
||||
mod vm_ops;
|
||||
|
||||
#[cfg(not(feature = "stdio"))]
|
||||
use crate::builtins::PyNone;
|
||||
use crate::{
|
||||
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
|
||||
builtins::{
|
||||
@@ -301,7 +303,8 @@ impl VirtualMachine {
|
||||
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
|
||||
{
|
||||
let io = import::import_builtin(self, "_io")?;
|
||||
let set_stdio = |name, fd, write| {
|
||||
#[cfg(feature = "stdio")]
|
||||
let make_stdio = |name, fd, write| {
|
||||
let buffered_stdio = self.state.settings.buffered_stdio;
|
||||
let unbuffered = write && !buffered_stdio;
|
||||
let buf = crate::stdlib::io::open(
|
||||
@@ -332,7 +335,13 @@ impl VirtualMachine {
|
||||
)?;
|
||||
let mode = if write { "w" } else { "r" };
|
||||
stdio.set_attr("mode", self.ctx.new_str(mode), self)?;
|
||||
Ok(stdio)
|
||||
};
|
||||
#[cfg(not(feature = "stdio"))]
|
||||
let make_stdio = |_name, _fd, _write| Ok(PyNone.into_pyobject(self));
|
||||
|
||||
let set_stdio = |name, fd, write| {
|
||||
let stdio = make_stdio(name, fd, write)?;
|
||||
let dunder_name = self.ctx.intern_str(format!("__{name}__"));
|
||||
self.sys_module.set_attr(
|
||||
dunder_name, // e.g. __stdin__
|
||||
|
||||
Reference in New Issue
Block a user