Skip to content

Commit ae2c81f

Browse files
committed
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.
1 parent 0f553fe commit ae2c81f

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

py/vm.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ typedef enum {
7878
#define TOP() (*sp)
7979
#define SET_TOP(val) *sp = (val)
8080

81+
#if MICROPY_PY_SYS_EXC_INFO
82+
#define CLEAR_SYS_EXC_INFO() MP_STATE_VM(cur_exception) = MP_OBJ_NULL;
83+
#else
84+
#define CLEAR_SYS_EXC_INFO()
85+
#endif
86+
8187
#define PUSH_EXC_BLOCK(with_or_finally) do { \
8288
DECODE_ULABEL; /* except labels are always forward */ \
8389
++exc_sp; \
@@ -89,7 +95,8 @@ typedef enum {
8995

9096
#define POP_EXC_BLOCK() \
9197
currently_in_except_block = MP_TAGPTR_TAG0(exc_sp->val_sp); /* restore previous state */ \
92-
exc_sp--; /* pop back to previous exception handler */
98+
exc_sp--; /* pop back to previous exception handler */ \
99+
CLEAR_SYS_EXC_INFO() /* just clear sys.exc_info(), not compliant, but it shouldn't be used in 1st place */
93100

94101
// fastn has items in reverse order (fastn[0] is local[0], fastn[-1] is local[1], etc)
95102
// sp points to bottom of stack which grows up

tests/misc/sys_exc_info.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ def f():
1414
print(sys.exc_info()[0:2])
1515
f()
1616

17-
# MicroPython currently doesn't reset sys.exc_info() value
18-
# on exit from "except" block.
19-
#f()
17+
# Outside except block, sys.exc_info() should be back to None's
18+
f()
2019

21-
# Recursive except blocks are not handled either - just don't
22-
# use exc_info() at all!
20+
# Recursive except blocks are not handled - just don't
21+
# use exc_info() at all, use explicit variables in "except".

0 commit comments

Comments
 (0)