Print cause on exception

This commit is contained in:
Aviv Palivoda
2019-04-12 15:50:17 +03:00
parent 6c4b092641
commit 908f758cbe
2 changed files with 14 additions and 2 deletions

View File

@@ -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()) {

View File

@@ -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)
}