Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/snippets/try_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 8 additions & 7 deletions vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(),
},
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -983,7 +983,7 @@ impl Frame {
}
}
BlockType::ExceptHandler => {
vm.pop_exception();
vm.pop_exception().expect("Should have exception in stack");
}
}
}
Expand All @@ -1009,7 +1009,7 @@ impl Frame {
}
},
BlockType::ExceptHandler => {
vm.pop_exception();
vm.pop_exception().expect("Should have exception in stack");
}
}

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,10 @@ impl VirtualMachine {
pub fn pop_exception(&self) -> Option<PyObjectRef> {
self.exceptions.borrow_mut().pop()
}

pub fn current_exception(&self) -> Option<PyObjectRef> {
self.exceptions.borrow().last().cloned()
}
}

impl Default for VirtualMachine {
Expand Down