mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Print cause on exception
This commit is contained in:
@@ -2,7 +2,7 @@ use crate::function::PyFuncArgs;
|
||||
use crate::obj::objsequence;
|
||||
use crate::obj::objtype;
|
||||
use crate::obj::objtype::PyClassRef;
|
||||
use crate::pyobject::{create_type, PyContext, PyObjectRef, PyResult, TypeProtocol};
|
||||
use crate::pyobject::{create_type, IdProtocol, PyContext, PyObjectRef, PyResult, TypeProtocol};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
fn exception_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
@@ -18,8 +18,19 @@ fn exception_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
Ok(vm.get_none())
|
||||
}
|
||||
|
||||
// Print exception including traceback:
|
||||
// print excption chain
|
||||
pub fn print_exception(vm: &VirtualMachine, exc: &PyObjectRef) {
|
||||
if let Ok(cause) = vm.get_attribute(exc.clone(), "__cause__") {
|
||||
if !vm.get_none().is(&cause) {
|
||||
print_exception(vm, &cause);
|
||||
println!("\nThe above exception was the direct cause of the following exception:\n");
|
||||
}
|
||||
}
|
||||
print_exception_inner(vm, exc)
|
||||
}
|
||||
|
||||
// Print exception including traceback:
|
||||
pub fn print_exception_inner(vm: &VirtualMachine, exc: &PyObjectRef) {
|
||||
if let Ok(tb) = vm.get_attribute(exc.clone(), "__traceback__") {
|
||||
println!("Traceback (most recent call last):");
|
||||
if objtype::isinstance(&tb, &vm.ctx.list_type()) {
|
||||
|
||||
@@ -686,6 +686,7 @@ impl Frame {
|
||||
0 | 3 => panic!("Not implemented!"),
|
||||
_ => panic!("Invalid parameter for RAISE_VARARGS, must be between 0 to 3"),
|
||||
};
|
||||
info!("Exception raised: {:?} with cause: {:?}", exception, cause);
|
||||
vm.set_attr(&exception, vm.new_str("__cause__".to_string()), cause)?;
|
||||
Err(exception)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user