Skip to content

Commit d4da84c

Browse files
Set stdio to None if feature is disabled
More in line with what CPython does
1 parent 84bdd8d commit d4da84c

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

vm/src/vm/mod.rs

Lines changed: 26 additions & 22 deletions
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
builtins::{
1921
code::PyCode,
@@ -306,28 +308,30 @@ impl VirtualMachine {
306308
// require the Python stdlib to be present
307309
let io = import::import_builtin(self, "_io")?;
308310

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-
}
311+
let set_stdio = |name, _fd, _mode: &str| {
312+
#[cfg(feature = "stdio")]
313+
let stdio = crate::stdlib::io::open(
314+
self.ctx.new_int(_fd).into(),
315+
Some(_mode),
316+
Default::default(),
317+
self,
318+
)?;
319+
320+
#[cfg(not(feature = "stdio"))]
321+
let stdio = PyNone.into_pyobject(self);
322+
323+
let dunder_name = self.ctx.intern_str(format!("__{name}__"));
324+
self.sys_module.set_attr(
325+
dunder_name, // e.g. __stdin__
326+
stdio.clone(),
327+
self,
328+
)?;
329+
self.sys_module.set_attr(name, stdio, self)?;
330+
Ok(())
331+
};
332+
set_stdio("stdin", 0, "r")?;
333+
set_stdio("stdout", 1, "w")?;
334+
set_stdio("stderr", 2, "w")?;
331335

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

0 commit comments

Comments
 (0)