diff --git a/src/lib.rs b/src/lib.rs index 71a468418..d820c0013 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,7 +45,7 @@ extern crate log; use clap::{App, AppSettings, Arg, ArgMatches}; use rustpython_vm::{ - builtins::PyInt, compile, exceptions::print_exception, match_class, scope::Scope, sysmodule, + builtins::PyInt, compile, exceptions::print_exception, match_class, scope::Scope, stdlib::sys, InitParameter, Interpreter, ItemProtocol, PyResult, PySettings, TypeProtocol, VirtualMachine, }; @@ -158,10 +158,10 @@ where } fn flush_std(vm: &VirtualMachine) { - if let Ok(stdout) = sysmodule::get_stdout(vm) { + if let Ok(stdout) = sys::get_stdout(vm) { let _ = vm.call_method(&stdout, "flush", ()); } - if let Ok(stderr) = sysmodule::get_stderr(vm) { + if let Ok(stderr) = sys::get_stderr(vm) { let _ = vm.call_method(&stderr, "flush", ()); } } diff --git a/vm/src/builtins/make_module.rs b/vm/src/builtins/make_module.rs index a5eba5f44..f798872ea 100644 --- a/vm/src/builtins/make_module.rs +++ b/vm/src/builtins/make_module.rs @@ -29,7 +29,7 @@ mod decl { readline::{Readline, ReadlineResult}, scope::Scope, slots::PyComparisonOp, - sysmodule, + stdlib::sys, utils::Either, IdProtocol, ItemProtocol, PyArithmaticValue, PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, @@ -358,9 +358,9 @@ mod decl { #[pyfunction] fn input(prompt: OptionalArg, vm: &VirtualMachine) -> PyResult { - let stdin = sysmodule::get_stdin(vm)?; - let stdout = sysmodule::get_stdout(vm)?; - let stderr = sysmodule::get_stderr(vm)?; + let stdin = sys::get_stdin(vm)?; + let stdout = sys::get_stdout(vm)?; + let stderr = sys::get_stderr(vm)?; let _ = vm.call_method(&stderr, "flush", ()); @@ -645,7 +645,7 @@ mod decl { pub fn print(objects: PosArgs, options: PrintOptions, vm: &VirtualMachine) -> PyResult<()> { let file = match options.file { Some(f) => f, - None => sysmodule::get_stdout(vm)?, + None => sys::get_stdout(vm)?, }; let write = |obj: PyStrRef| vm.call_method(&file, "write", (obj,)); diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 7e9dc2183..5e8808a86 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -6,22 +6,23 @@ use crate::{ }, function::{ArgIterable, FuncArgs}, py_io::{self, Write}, - sysmodule, IdProtocol, IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, - PyValue, StaticType, TryFromObject, TypeProtocol, VirtualMachine, + stdlib::sys, + IdProtocol, IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, + StaticType, TryFromObject, TypeProtocol, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; use itertools::Itertools; -use std::collections::HashSet; -use std::fmt; -use std::fs::File; -use std::io::{self, BufRead, BufReader}; +use std::{ + collections::HashSet, + io::{self, BufRead, BufReader}, +}; pub trait IntoPyException { fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef; } -impl fmt::Debug for PyBaseException { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { +impl std::fmt::Debug for PyBaseException { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { // TODO: implement more detailed, non-recursive Debug formatter f.write_str("PyBaseException") } @@ -47,7 +48,7 @@ pub fn chain(e1: PyResult<()>, e2: PyResult) -> PyResult { /// Print exception chain by calling sys.excepthook pub fn print_exception(vm: &VirtualMachine, exc: PyBaseExceptionRef) { let write_fallback = |exc, errstr| { - if let Ok(stderr) = sysmodule::get_stderr(vm) { + if let Ok(stderr) = sys::get_stderr(vm) { let mut stderr = py_io::PyWriter(stderr, vm); // if this fails stderr might be closed -- ignore it let _ = writeln!(stderr, "{}", errstr); @@ -84,7 +85,7 @@ fn print_source_line( ) -> Result<(), W::Error> { // TODO: use io.open() method instead, when available, according to https://github.com/python/cpython/blob/main/Python/traceback.c#L393 // TODO: support different encodings - let file = match File::open(filename) { + let file = match std::fs::File::open(filename) { Ok(file) => file, Err(_) => return Ok(()), }; diff --git a/vm/src/lib.rs b/vm/src/lib.rs index a415c55ae..5b089dafe 100644 --- a/vm/src/lib.rs +++ b/vm/src/lib.rs @@ -72,7 +72,6 @@ mod signal; mod sliceable; pub mod slots; pub mod stdlib; -pub mod sysmodule; pub mod types; pub mod utils; mod version; diff --git a/vm/src/stdlib/mod.rs b/vm/src/stdlib/mod.rs index b4cfaa975..8861c444a 100644 --- a/vm/src/stdlib/mod.rs +++ b/vm/src/stdlib/mod.rs @@ -81,6 +81,7 @@ mod select; pub(crate) mod signal; #[cfg(all(not(target_arch = "wasm32"), feature = "ssl"))] mod ssl; +pub mod sys; #[cfg(all(unix, not(target_os = "redox")))] mod termios; #[cfg(windows)] @@ -146,7 +147,7 @@ pub fn get_module_inits() -> StdlibMap { "unicodedata" => unicodedata::make_module, "_warnings" => warnings::make_module, "zlib" => zlib::make_module, - crate::sysmodule::sysconfigdata_name() => sysconfigdata::make_module, + sys::sysconfigdata_name() => sysconfigdata::make_module, } // parser related modules: #[cfg(feature = "rustpython-ast")] diff --git a/vm/src/sysmodule.rs b/vm/src/stdlib/sys.rs similarity index 99% rename from vm/src/sysmodule.rs rename to vm/src/stdlib/sys.rs index b7a8b9226..389d82a5f 100644 --- a/vm/src/sysmodule.rs +++ b/vm/src/stdlib/sys.rs @@ -361,7 +361,7 @@ pub fn get_stderr(vm: &VirtualMachine) -> PyResult { /// # Usage /// /// ```rust,ignore -/// writeln!(sysmodule::PyStderr(vm), "foo bar baz :)"); +/// writeln!(sys::PyStderr(vm), "foo bar baz :)"); /// ``` /// /// Unlike writing to a `std::io::Write` with the `write[ln]!()` macro, there's no error condition here; diff --git a/vm/src/stdlib/sysconfigdata.rs b/vm/src/stdlib/sysconfigdata.rs index 995313a01..c36560424 100644 --- a/vm/src/stdlib/sysconfigdata.rs +++ b/vm/src/stdlib/sysconfigdata.rs @@ -1,4 +1,4 @@ -use crate::sysmodule::MULTIARCH; +use super::sys::MULTIARCH; use crate::{IntoPyObject, ItemProtocol, PyObjectRef, VirtualMachine}; pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 9c14976e8..d9c473c1f 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -26,7 +26,7 @@ use crate::{ scope::Scope, signal::NSIG, slots::PyComparisonOp, - stdlib, sysmodule, + stdlib, utils::Either, IdProtocol, IntoPyObject, ItemProtocol, PyArithmaticValue, PyContext, PyLease, PyMethod, PyObject, PyObjectRef, PyRef, PyRefExact, PyResult, PyValue, TryFromObject, TryIntoRef, @@ -326,7 +326,7 @@ impl VirtualMachine { } builtins::make_module(self, self.builtins.clone()); - sysmodule::make_module(self, self.sys_module.clone(), self.builtins.clone()); + stdlib::sys::make_module(self, self.sys_module.clone(), self.builtins.clone()); let mut inner_init = || -> PyResult<()> { #[cfg(not(target_arch = "wasm32"))] @@ -475,7 +475,10 @@ impl VirtualMachine { if let Err(e) = self.invoke(&func, args) { last_exc = Some(e.clone()); if !e.isinstance(&self.ctx.exceptions.system_exit) { - writeln!(sysmodule::PyStderr(self), "Error in atexit._run_exitfuncs:"); + writeln!( + stdlib::sys::PyStderr(self), + "Error in atexit._run_exitfuncs:" + ); exceptions::print_exception(self, e); } }