Skip to content

Commit 8b85d14

Browse files
committed
modsys: Add basic sys.exc_info() implementation.
The implementation is very basic and non-compliant and provided solely for CPython compatibility. The function itself is bad Python2 heritage, its usage is discouraged.
1 parent cf5b6f6 commit 8b85d14

8 files changed

Lines changed: 66 additions & 0 deletions

File tree

py/modsys.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,26 @@ STATIC mp_obj_t mp_sys_print_exception(mp_uint_t n_args, const mp_obj_t *args) {
121121
}
122122
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_print_exception);
123123

124+
#if MICROPY_PY_SYS_EXC_INFO
125+
STATIC mp_obj_t mp_sys_exc_info(void) {
126+
mp_obj_t cur_exc = MP_STATE_VM(cur_exception);
127+
mp_obj_tuple_t *t = mp_obj_new_tuple(3, NULL);
128+
129+
if (cur_exc == MP_OBJ_NULL) {
130+
t->items[0] = mp_const_none;
131+
t->items[1] = mp_const_none;
132+
t->items[2] = mp_const_none;
133+
return t;
134+
}
135+
136+
t->items[0] = mp_obj_get_type(cur_exc);
137+
t->items[1] = cur_exc;
138+
t->items[2] = mp_const_none;
139+
return t;
140+
}
141+
MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info);
142+
#endif
143+
124144
STATIC const mp_map_elem_t mp_module_sys_globals_table[] = {
125145
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_sys) },
126146

@@ -163,6 +183,10 @@ STATIC const mp_map_elem_t mp_module_sys_globals_table[] = {
163183
{ MP_OBJ_NEW_QSTR(MP_QSTR_stderr), (mp_obj_t)&mp_sys_stderr_obj },
164184
#endif
165185

186+
#if MICROPY_PY_SYS_EXC_INFO
187+
{ MP_OBJ_NEW_QSTR(MP_QSTR_exc_info), (mp_obj_t)&mp_sys_exc_info_obj },
188+
#endif
189+
166190
/*
167191
* Extensions to CPython
168192
*/

py/mpconfig.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,12 @@ typedef double mp_float_t;
573573
#define MICROPY_PY_SYS_MAXSIZE (0)
574574
#endif
575575

576+
// Whether to provide "sys.exc_info" function
577+
// Avoid enabling this, this function is Python2 heritage
578+
#ifndef MICROPY_PY_SYS_EXC_INFO
579+
#define MICROPY_PY_SYS_EXC_INFO (0)
580+
#endif
581+
576582
// Whether to provide "sys.exit" function
577583
#ifndef MICROPY_PY_SYS_EXIT
578584
#define MICROPY_PY_SYS_EXIT (0)

py/mpstate.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ typedef struct _mp_state_vm_t {
107107
// pending exception object (MP_OBJ_NULL if not pending)
108108
mp_obj_t mp_pending_exception;
109109

110+
// current exception being handled, for sys.exc_info()
111+
#if MICROPY_PY_SYS_EXC_INFO
112+
mp_obj_t cur_exception;
113+
#endif
114+
110115
// dictionary for the __main__ module
111116
mp_obj_dict_t dict_main;
112117

py/qstrdefs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,9 @@ Q(implementation)
457457
#if MICROPY_PY_SYS_MAXSIZE
458458
Q(maxsize)
459459
#endif
460+
#if MICROPY_PY_SYS_EXC_INFO
461+
Q(exc_info)
462+
#endif
460463
Q(print_exception)
461464
#endif
462465

py/vm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,10 @@ unwind_jump:;
12281228
exception_handler:
12291229
// exception occurred
12301230

1231+
#if MICROPY_PY_SYS_EXC_INFO
1232+
MP_STATE_VM(cur_exception) = nlr.ret_val;
1233+
#endif
1234+
12311235
#if SELECTIVE_EXC_IP
12321236
// with selective ip, we store the ip 1 byte past the opcode, so move ptr back
12331237
code_state->ip -= 1;

tests/misc/sys_exc_info.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import sys
2+
try:
3+
sys.exc_info
4+
except:
5+
print("SKIP")
6+
sys.exit()
7+
8+
def f():
9+
print(sys.exc_info()[0:2])
10+
11+
try:
12+
1/0
13+
except:
14+
print(sys.exc_info()[0:2])
15+
f()
16+
17+
# MicroPython currently doesn't reset sys.exc_info() value
18+
# on exit from "except" block.
19+
#f()
20+
21+
# Recursive except blocks are not handled either - just don't
22+
# use exc_info() at all!

tests/run-tests

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ def run_tests(pyb, tests, args):
171171
skip_tests.add('misc/features.py') # requires raise_varargs
172172
skip_tests.add('misc/rge_sm.py') # requires yield
173173
skip_tests.add('misc/print_exception.py') # because native doesn't have proper traceback info
174+
skip_tests.add('misc/sys_exc_info.py') # sys.exc_info() is not supported for native
174175

175176
for test_file in tests:
176177
test_basename = os.path.basename(test_file)

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#define MICROPY_PY_SYS_PLATFORM "linux"
7272
#define MICROPY_PY_SYS_MAXSIZE (1)
7373
#define MICROPY_PY_SYS_STDFILES (1)
74+
#define MICROPY_PY_SYS_EXC_INFO (1)
7475
#define MICROPY_PY_COLLECTIONS_ORDEREDDICT (1)
7576
#define MICROPY_PY_MATH_SPECIAL_FUNCTIONS (1)
7677
#define MICROPY_PY_CMATH (1)

0 commit comments

Comments
 (0)