From c403076ef8c534836a441374ef0366027946de50 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 26 Mar 2014 14:42:17 +0200 Subject: [PATCH] vm: Implement raise statement w/o args (reraising last exception). --- py/vm.c | 10 ++++++++-- tests/basics/try-reraise.py | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 tests/basics/try-reraise.py diff --git a/py/vm.c b/py/vm.c index 22243c403e..98d1250720 100644 --- a/py/vm.c +++ b/py/vm.c @@ -604,8 +604,14 @@ unwind_return: case MP_BC_RAISE_VARARGS: unum = *ip++; - assert(unum == 1); - obj1 = POP(); + assert(unum <= 1); + if (unum == 0) { + // This assumes that nlr.ret_val holds last raised + // exception and is not overwritten since then. + obj1 = nlr.ret_val; + } else { + obj1 = POP(); + } nlr_jump(rt_make_raise_obj(obj1)); case MP_BC_YIELD_VALUE: diff --git a/tests/basics/try-reraise.py b/tests/basics/try-reraise.py new file mode 100644 index 0000000000..bc817fc386 --- /dev/null +++ b/tests/basics/try-reraise.py @@ -0,0 +1,12 @@ +# Re-reraising last exception with raise w/o args + +def f(): + try: + raise ValueError("val", 3) + except: + raise + +try: + f() +except ValueError as e: + print(repr(e))