From 86cbc2e66de5d4a39e9d59432aad6518b3d41dd1 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Tue, 9 Jul 2019 17:48:27 +0300 Subject: [PATCH 1/2] Fix exception stack --- tests/snippets/try_exceptions.py | 18 ++++++++++++++++++ vm/src/frame.rs | 15 ++++++++------- vm/src/vm.rs | 4 ++++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/tests/snippets/try_exceptions.py b/tests/snippets/try_exceptions.py index f37101b6110..e520ebdf212 100644 --- a/tests/snippets/try_exceptions.py +++ b/tests/snippets/try_exceptions.py @@ -220,3 +220,21 @@ def y(): raise NameError except NameError as ex: assert ex.__context__ == None + + +try: + {}[1] +except KeyError: + try: + raise RuntimeError() + except RuntimeError: + pass + + +try: + try: + raise ZeroDivisionError + except ZeroDivisionError as ex: + raise NameError from ex +except NameError as ex2: + pass diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 27df11114a5..a95facbdf74 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -752,7 +752,7 @@ impl Frame { _ => vm.get_none(), }; let exception = match argc { - 0 => match vm.pop_exception() { + 0 => match vm.current_exception() { Some(exc) => exc, None => { return Err(vm.new_exception( @@ -767,7 +767,7 @@ impl Frame { }; let context = match argc { 0 => vm.get_none(), // We have already got the exception, - _ => match vm.pop_exception() { + _ => match vm.current_exception() { Some(exc) => exc, None => vm.get_none(), }, @@ -891,7 +891,7 @@ impl Frame { bytecode::Instruction::PopException {} => { let block = self.pop_block().unwrap(); // this asserts that the block is_some. if let BlockType::ExceptHandler = block.typ { - assert!(vm.pop_exception().is_some()); + vm.pop_exception().expect("Should have exception in stack"); Ok(None) } else { panic!("Block type must be ExceptHandler here.") @@ -983,7 +983,7 @@ impl Frame { } } BlockType::ExceptHandler => { - vm.pop_exception(); + vm.pop_exception().expect("Should have exception in stack"); } } } @@ -1009,7 +1009,7 @@ impl Frame { } }, BlockType::ExceptHandler => { - vm.pop_exception(); + vm.pop_exception().expect("Should have exception in stack"); } } @@ -1057,8 +1057,9 @@ impl Frame { } } BlockType::Loop { .. } => {} - // Exception was already popped on Raised. - BlockType::ExceptHandler => {} + BlockType::ExceptHandler => { + vm.pop_exception().expect("Should have exception in stack"); + } } } Some(exc) diff --git a/vm/src/vm.rs b/vm/src/vm.rs index a1d08fa5506..c1d38c8fc4e 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -1014,6 +1014,10 @@ impl VirtualMachine { pub fn pop_exception(&self) -> Option { self.exceptions.borrow_mut().pop() } + + pub fn current_exception(&self) -> Option { + self.exceptions.borrow().last().map(|x| x.clone()) + } } impl Default for VirtualMachine { From b815d47d2a28bb54f4f72b2a793e21a2ac0f3df6 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Tue, 9 Jul 2019 18:38:43 +0300 Subject: [PATCH 2/2] Use cloned --- vm/src/vm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/vm.rs b/vm/src/vm.rs index c1d38c8fc4e..68e53e6527f 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -1016,7 +1016,7 @@ impl VirtualMachine { } pub fn current_exception(&self) -> Option { - self.exceptions.borrow().last().map(|x| x.clone()) + self.exceptions.borrow().last().cloned() } }