8000 always print exceptions when panic by expect_pyresult (#5215) · moreal/RustPython@220594f · GitHub
[go: up one dir, main page]

Skip to content

Commit 220594f

Browse files
authored
always print exceptions when panic by expect_pyresult (RustPython#5215)
if users want simple panic, Result::expect could be used.
1 parent f541ca9 commit 220594f

File tree

3 files changed

+18
-26
lines changed

3 files changed

+18
-26
lines changed

vm/src/vm/interpreter.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
use super::{setting::Settings, thread, Context, VirtualMachine};
2-
use crate::{
3-
stdlib::{atexit, sys},
4-
vm::PyBaseExceptionRef,
5-
PyResult,
6-
};
2+
use crate::{stdlib::atexit, vm::PyBaseExceptionRef, PyResult};
73
use std::sync::atomic::Ordering;
84

95
/// The general interface for the VM
@@ -120,7 +116,7 @@ impl Interpreter {
120116
/// Note that calling `finalize` is not necessary by purpose though.
121117
pub fn finalize(self, exc: Option<PyBaseExceptionRef>) -> u8 {
122118
self.enter(|vm| {
123-
flush_std(vm);
119+
vm.flush_std();
124120

125121
// See if any exception leaked out:
126122
let exit_code = if let Some(exc) = exc {
@@ -133,22 +129,13 @@ impl Interpreter {
133129

134130
vm.state.finalizing.store(true, Ordering::Release);
135131

136-
flush_std(vm);
132+
vm.flush_std();
137133

138134
exit_code
139135
})
140136
}
141137
}
142138

143-
pub(crate) fn flush_std(vm: &VirtualMachine) {
144-
if let Ok(stdout) = sys::get_stdout(vm) {
145-
let _ = vm.call_method(&stdout, identifier!(vm, flush).as_str(), ());
146-
}
147-
if let Ok(stderr) = sys::get_stderr(vm) {
148-
let _ = vm.call_method(&stderr, identifier!(vm, flush).as_str(), ());
149-
}
150-
}
151-
152139
#[cfg(test)]
153140
mod tests {
154141
use super::*;

vm/src/vm/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ impl VirtualMachine {
830830
);
831831
let result = unsafe { sigaction(SIGINT, &action) };
832832
if result.is_ok() {
833-
interpreter::flush_std(self);
833+
self.flush_std();
834834
kill(getpid(), SIGINT).expect("Expect to be killed.");
835835
}
836836

vm/src/vm/vm_object.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
function::IntoFuncArgs,
55
identifier,
66
object::{AsObject, PyObject, PyObjectRef, PyResult},
7+
stdlib::sys,
78
vm::VirtualMachine,
89
};
910

@@ -14,15 +15,9 @@ impl VirtualMachine {
1415
fn _py_panic_failed(&self, exc: PyBaseExceptionRef, msg: &str) -> ! {
1516
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
1617
{
17-
let show_backtrace =
18-
std::env::var_os("RUST_BACKTRACE").map_or(cfg!(target_os = "wasi"), |v| &v != "0");
19-
let after = if show_backtrace {
20-
self.print_exception(exc);
21-
"exception backtrace above"
22-
} else {
23-
"run with RUST_BACKTRACE=1 to see Python backtrace"
24-
};
25-
panic!("{msg}; {after}")
18+
self.print_exception(exc);
19+
self.flush_std();
20+
panic!("{msg}")
2621
}
2722
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
2823
{
@@ -39,6 +34,16 @@ impl VirtualMachine {
3934
}
4035
}
4136

37+
pub(crate) fn flush_std(&self) {
38+
let vm = self;
39+
if let Ok(stdout) = sys::get_stdout(vm) {
40+
let _ = vm.call_method(&stdout, identifier!(vm, flush).as_str(), ());
41+
}
42+
if let Ok(stderr) = sys::get_stderr(vm) {
43+
let _ = vm.call_method(&stderr, identifier!(vm, flush).as_str(), ());
44+
}
45+
}
46+
4247
#[track_caller]
4348
pub fn unwrap_pyresult<T>(&self, result: PyResult<T>) -> T {
4449
match result {

0 commit comments

Comments
 (0)
0