Skip to content

Commit 688e220

Browse files
committed
Merge pull request adafruit#379 from pfalcon/reraise
vm: Implement raise statement w/o args (reraising last exception).
2 parents 9c817b9 + c403076 commit 688e220

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

py/vm.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,14 @@ mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **i
605605

606606
case MP_BC_RAISE_VARARGS:
607607
unum = *ip++;
608-
assert(unum == 1);
609-
obj1 = POP();
608+
assert(unum <= 1);
609+
if (unum == 0) {
610+
// This assumes that nlr.ret_val holds last raised
611+
// exception and is not overwritten since then.
612+
obj1 = nlr.ret_val;
613+
} else {
614+
obj1 = POP();
615+
}
610616
nlr_jump(rt_make_raise_obj(obj1));
611617

612618
case MP_BC_YIELD_VALUE:

tests/basics/try-reraise.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Re-reraising last exception with raise w/o args
2+
3+
def f():
4+
try:
5+
raise ValueError("val", 3)
6+
except:
7+
raise
8+
9+
try:
10+
f()
11+
except ValueError as e:
12+
print(repr(e))

0 commit comments

Comments
 (0)