Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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", ());
}
}
Expand Down
10 changes: 5 additions & 5 deletions vm/src/builtins/make_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -358,9 +358,9 @@ mod decl {

#[pyfunction]
fn input(prompt: OptionalArg<PyStrRef>, 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", ());

Expand Down Expand Up @@ -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,));

Expand Down
21 changes: 11 additions & 10 deletions vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -47,7 +48,7 @@ pub fn chain<T>(e1: PyResult<()>, e2: PyResult<T>) -> PyResult<T> {
/// 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);
Expand Down Expand Up @@ -84,7 +85,7 @@ fn print_source_line<W: Write>(
) -> 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(()),
};
Expand Down
1 change: 0 additions & 1 deletion vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion vm/src/stdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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")]
Expand Down
2 changes: 1 addition & 1 deletion vm/src/sysmodule.rs → vm/src/stdlib/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/sysconfigdata.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
9 changes: 6 additions & 3 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"))]
Expand Down Expand Up @@ -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);
}
}
Expand Down