From ae2c81ff388d9b06bc11ac1e14bd5d71b4f4637c Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 26 Apr 2015 01:20:49 +0300 Subject: [PATCH] vm: On exiting except block, clear sys.exc_info() value. This doesn't handle case fo enclosed except blocks, but once again, sys.exc_info() support is a workaround for software which uses it instead of properly catching exceptions via variable in except clause. --- py/vm.c | 9 ++++++++- tests/misc/sys_exc_info.py | 9 ++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/py/vm.c b/py/vm.c index 3a7191368..997884d4f 100644 --- a/py/vm.c +++ b/py/vm.c @@ -78,6 +78,12 @@ typedef enum { #define TOP() (*sp) #define SET_TOP(val) *sp = (val) +#if MICROPY_PY_SYS_EXC_INFO +#define CLEAR_SYS_EXC_INFO() MP_STATE_VM(cur_exception) = MP_OBJ_NULL; +#else +#define CLEAR_SYS_EXC_INFO() +#endif + #define PUSH_EXC_BLOCK(with_or_finally) do { \ DECODE_ULABEL; /* except labels are always forward */ \ ++exc_sp; \ @@ -89,7 +95,8 @@ typedef enum { #define POP_EXC_BLOCK() \ currently_in_except_block = MP_TAGPTR_TAG0(exc_sp->val_sp); /* restore previous state */ \ - exc_sp--; /* pop back to previous exception handler */ + exc_sp--; /* pop back to previous exception handler */ \ + CLEAR_SYS_EXC_INFO() /* just clear sys.exc_info(), not compliant, but it shouldn't be used in 1st place */ // fastn has items in reverse order (fastn[0] is local[0], fastn[-1] is local[1], etc) // sp points to bottom of stack which grows up diff --git a/tests/misc/sys_exc_info.py b/tests/misc/sys_exc_info.py index 1aad6f189..de5b82562 100644 --- a/tests/misc/sys_exc_info.py +++ b/tests/misc/sys_exc_info.py @@ -14,9 +14,8 @@ except: print(sys.exc_info()[0:2]) f() -# MicroPython currently doesn't reset sys.exc_info() value -# on exit from "except" block. -#f() +# Outside except block, sys.exc_info() should be back to None's +f() -# Recursive except blocks are not handled either - just don't -# use exc_info() at all! +# Recursive except blocks are not handled - just don't +# use exc_info() at all, use explicit variables in "except".