Skip to content

Commit 84bdd8d

Browse files
Make stdio a feature
When embedding RustPython into a graphical windows application (where windows_subsystem = "windows"), it fails to create the RustPython VM. This happens because the VM will try to open stdin/stdout/stderr when initializing, but stdio isn't available for /SUBSYSTEM:WINDOWS apps. This commit makes stdio a feature, so it can be disabled when needed. https://doc.rust-lang.org/reference/runtime.html#the-windows_subsystem-attribute https://learn.microsoft.com/en-us/cpp/build/reference/subsystem-specify-subsystem?view=msvc-170 https://asawicki.info/news_1768_ways_to_print_and_capture_text_output_of_a_process
1 parent 29d9534 commit 84bdd8d

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
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", "zlib", "importlib"]
13+
default = ["threading", "stdlib", "stdio", "zlib", "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: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -305,25 +305,29 @@ impl VirtualMachine {
305305
// builtins.open to io.OpenWrapper, but this is easier, since it doesn't
306306
// require the Python stdlib to be present
307307
let io = import::import_builtin(self, "_io")?;
308-
let set_stdio = |name, fd, mode: &str| {
309-
let stdio = crate::stdlib::io::open(
310-
self.ctx.new_int(fd).into(),
311-
Some(mode),
312-
Default::default(),
313-
self,
314-
)?;
315-
let dunder_name = self.ctx.intern_str(format!("__{name}__"));
316-
self.sys_module.set_attr(
317-
dunder_name, // e.g. __stdin__
318-
stdio.clone(),
319-
self,
320-
)?;
321-
self.sys_module.set_attr(name, stdio, self)?;
322-
Ok(())
323-
};
324-
set_stdio("stdin", 0, "r")?;
325-
set_stdio("stdout", 1, "w")?;
326-
set_stdio("stderr", 2, "w")?;
308+
309+
#[cfg(feature = "stdio")]
310+
{
311+
let set_stdio = |name, fd, mode: &str| {
312+
let stdio = crate::stdlib::io::open(
313+
self.ctx.new_int(fd).into(),
314+
Some(mode),
315+
Default::default(),
316+
self,
317+
)?;
318+
let dunder_name = self.ctx.intern_str(format!("__{name}__"));
319+
self.sys_module.set_attr(
320+
dunder_name, // e.g. __stdin__
321+
stdio.clone(),
322+
self,
323+
)?;
324+
self.sys_module.set_attr(name, stdio, self)?;
325+
Ok(())
326+
};
327+
set_stdio("stdin", 0, "r")?;
328+
set_stdio("stdout", 1, "w")?;
329+
set_stdio("stderr", 2, "w")?;
330+
}
327331

328332
let io_open = io.get_attr("open", self)?;
329333
self.builtins.set_attr("open", io_open, self)?;

0 commit comments

Comments
 (0)