From 908f758cbedcb0bddb3f5b32c99aa4855da668eb Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Fri, 12 Apr 2019 15:50:17 +0300 Subject: [PATCH] Print cause on exception --- vm/src/exceptions.rs | 15 +++++++++++++-- vm/src/frame.rs | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index d81764e25..abd670a14 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -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()) { diff --git a/vm/src/frame.rs b/vm/src/frame.rs index a3dfd6032..a1da85d3a 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -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) }