From 114e637bc8dc8eec930c22ba5c4becc4ac59505c Mon Sep 17 00:00:00 2001 From: Moreal Date: Sun, 15 Jan 2023 03:54:52 +0900 Subject: [PATCH 1/2] Use `ExitCode` instead of `u8` as exitcode --- examples/generator.rs | 2 +- examples/package_embed.rs | 2 +- src/lib.rs | 3 +-- vm/src/vm/interpreter.rs | 6 +++--- vm/src/vm/mod.rs | 20 ++++++++++---------- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/examples/generator.rs b/examples/generator.rs index 010ccd3797..41f3d99521 100644 --- a/examples/generator.rs +++ b/examples/generator.rs @@ -46,5 +46,5 @@ fn main() -> ExitCode { vm.add_native_modules(rustpython_stdlib::get_module_inits()); }); let result = py_main(&interp); - ExitCode::from(interp.run(|_vm| result)) + interp.run(|_vm| result) } diff --git a/examples/package_embed.rs b/examples/package_embed.rs index fe6b07cec1..530617db30 100644 --- a/examples/package_embed.rs +++ b/examples/package_embed.rs @@ -22,5 +22,5 @@ fn main() -> ExitCode { let result = result.map(|result| { println!("name: {result}"); }); - ExitCode::from(interp.run(|_vm| result)) + interp.run(|_vm| result) } diff --git a/src/lib.rs b/src/lib.rs index 8603c55d81..7c1266ba00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,9 +89,8 @@ pub fn run(init: impl FnOnce(&mut VirtualMachine) + 'static) -> ExitCode { config = config.init_hook(Box::new(init)); let interp = config.interpreter(); - let exitcode = interp.run(move |vm| run_rustpython(vm, run_mode, quiet_var)); - ExitCode::from(exitcode) + interp.run(move |vm| run_rustpython(vm, run_mode, quiet_var)) } fn setup_main_module(vm: &VirtualMachine) -> PyResult { diff --git a/vm/src/vm/interpreter.rs b/vm/src/vm/interpreter.rs index 8d0d85ea5c..1068fec25a 100644 --- a/vm/src/vm/interpreter.rs +++ b/vm/src/vm/interpreter.rs @@ -3,7 +3,7 @@ use crate::{ stdlib::{atexit, sys}, PyResult, }; -use std::sync::atomic::Ordering; +use std::{process::ExitCode, sync::atomic::Ordering}; /// The general interface for the VM /// @@ -59,7 +59,7 @@ impl Interpreter { thread::enter_vm(&self.vm, || f(&self.vm)) } - pub fn run(self, f: F) -> u8 + pub fn run(self, f: F) -> ExitCode where F: FnOnce(&VirtualMachine) -> PyResult, { @@ -69,7 +69,7 @@ impl Interpreter { // See if any exception leaked out: let exit_code = res - .map(|_| 0) + .map(|_| ExitCode::from(0)) .map_err(|exc| vm.handle_exit_exception(exc)) .unwrap_or_else(|code| code); diff --git a/vm/src/vm/mod.rs b/vm/src/vm/mod.rs index c7eea2514a..1edbd360c5 100644 --- a/vm/src/vm/mod.rs +++ b/vm/src/vm/mod.rs @@ -36,12 +36,12 @@ use crate::{ AsObject, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, }; use crossbeam_utils::atomic::AtomicCell; -use std::sync::atomic::AtomicBool; use std::{ borrow::Cow, cell::{Cell, Ref, RefCell}, collections::{HashMap, HashSet}, }; +use std::{process::ExitCode, sync::atomic::AtomicBool}; pub use context::Context; pub use interpreter::Interpreter; @@ -732,19 +732,19 @@ impl VirtualMachine { } } - pub fn handle_exit_exception(&self, exc: PyBaseExceptionRef) -> u8 { + pub fn handle_exit_exception(&self, exc: PyBaseExceptionRef) -> ExitCode { if exc.fast_isinstance(self.ctx.exceptions.system_exit) { let args = exc.args(); let msg = match args.as_slice() { - [] => return 0, + [] => return ExitCode::from(0), [arg] => match_class!(match arg { ref i @ PyInt => { use num_traits::cast::ToPrimitive; - return i.as_bigint().to_u8().unwrap_or(0); + return ExitCode::from(i.as_bigint().to_u8().unwrap_or(0)); } arg => { if self.is_none(arg) { - return 0; + return ExitCode::from(0); } else { arg.str(self).ok() } @@ -756,23 +756,23 @@ impl VirtualMachine { let stderr = stdlib::sys::PyStderr(self); writeln!(stderr, "{msg}"); } - 1 + ExitCode::from(1) } else if exc.fast_isinstance(self.ctx.exceptions.keyboard_interrupt) { #[allow(clippy::if_same_then_else)] { self.print_exception(exc); #[cfg(unix)] { - (libc::SIGINT as u8) + 128u8 + ExitCode::from((libc::SIGINT as u8) + 128u8) } - #[cfg(not(unix))] + #[cfg(not(any(unix, windows)))] { - 1 + ExitCode::from(1) } } } else { self.print_exception(exc); - 1 + ExitCode::from(1) } } From 20f0a2b75add03d2ef4624803531ee59697c58b8 Mon Sep 17 00:00:00 2001 From: Moreal Date: Sun, 15 Jan 2023 18:13:27 +0900 Subject: [PATCH 2/2] Handle `KeyboardInterrupt` event in also Windows --- vm/src/vm/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/vm/src/vm/mod.rs b/vm/src/vm/mod.rs index 1edbd360c5..45f2ba7cc4 100644 --- a/vm/src/vm/mod.rs +++ b/vm/src/vm/mod.rs @@ -36,6 +36,8 @@ use crate::{ AsObject, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, }; use crossbeam_utils::atomic::AtomicCell; +#[cfg(windows)] +use std::os::windows::process::ExitCodeExt; use std::{ borrow::Cow, cell::{Cell, Ref, RefCell}, @@ -765,6 +767,10 @@ impl VirtualMachine { { ExitCode::from((libc::SIGINT as u8) + 128u8) } + #[cfg(windows)] + { + ExitCode::from_raw(winapi::um::winnt::STATUS_CONTROL_C_EXIT) + } #[cfg(not(any(unix, windows)))] { ExitCode::from(1)