Skip to content

Commit dabd93c

Browse files
danielstuart14coolreader18
authored andcommitted
Make stdio a feature (#5420)
1 parent 0d4faa0 commit dabd93c

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ repository.workspace = true
1010
license.workspace = true
1111

1212
[features]
13-
default = ["threading", "stdlib", "importlib"]
13+
default = ["threading", "stdlib", "stdio", "importlib"]
1414
importlib = ["rustpython-vm/importlib"]
1515
encodings = ["rustpython-vm/encodings"]
16+
stdio = ["rustpython-vm/stdio"]
1617
stdlib = ["rustpython-stdlib", "rustpython-pylib", "encodings"]
1718
flame-it = ["rustpython-vm/flame-it", "flame", "flamescope"]
1819
freeze-stdlib = ["stdlib", "rustpython-vm/freeze-stdlib", "rustpython-pylib?/freeze-stdlib"]

vm/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ repository.workspace = true
1010
license.workspace = true
1111

1212
[features]
13-
default = ["compiler", "wasmbind"]
13+
default = ["compiler", "wasmbind", "stdio"]
14+
stdio = []
1415
importlib = []
1516
encodings = ["importlib"]
1617
vm-tracing-logging = []

vm/src/vm/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ mod vm_new;
1414
mod vm_object;
1515
mod vm_ops;
1616

17+
#[cfg(not(feature = "stdio"))]
18+
use crate::builtins::PyNone;
1719
use crate::{
1820
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
1921
builtins::{
@@ -301,7 +303,8 @@ impl VirtualMachine {
301303
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
302304
{
303305
let io = import::import_builtin(self, "_io")?;
304-
let set_stdio = |name, fd, write| {
306+
#[cfg(feature = "stdio")]
307+
let make_stdio = |name, fd, write| {
305308
let buffered_stdio = self.state.settings.buffered_stdio;
306309
let unbuffered = write && !buffered_stdio;
307310
let buf = crate::stdlib::io::open(
@@ -332,7 +335,13 @@ impl VirtualMachine {
332335
)?;
333336
let mode = if write { "w" } else { "r" };
334337
stdio.set_attr("mode", self.ctx.new_str(mode), self)?;
338+
Ok(stdio)
339+
};
340+
#[cfg(not(feature = "stdio"))]
341+
let make_stdio = |_name, _fd, _write| Ok(PyNone.into_pyobject(self));
335342

343+
let set_stdio = |name, fd, write| {
344+
let stdio = make_stdio(name, fd, write)?;
336345
let dunder_name = self.ctx.intern_str(format!("__{name}__"));
337346
self.sys_module.set_attr(
338347
dunder_name, // e.g. __stdin__

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy