diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2015-04-25 03:17:41 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2015-04-25 03:49:23 +0300 |
commit | 8b85d14b92a65e92862861038c0fe96e6c616c3e (patch) | |
tree | 237c7a0bad20fb8a31944a6bc7bbc51a97689d3a /py | |
parent | cf5b6f6974a1ca7d835803a744d1daae12876dac (diff) | |
download | micropython-8b85d14b92a65e92862861038c0fe96e6c616c3e.tar.gz micropython-8b85d14b92a65e92862861038c0fe96e6c616c3e.zip |
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.
Diffstat (limited to 'py')
-rw-r--r-- | py/modsys.c | 24 | ||||
-rw-r--r-- | py/mpconfig.h | 6 | ||||
-rw-r--r-- | py/mpstate.h | 5 | ||||
-rw-r--r-- | py/qstrdefs.h | 3 | ||||
-rw-r--r-- | py/vm.c | 4 |
5 files changed, 42 insertions, 0 deletions
diff --git a/py/modsys.c b/py/modsys.c index 182ae13ecc..aad0fb8d7f 100644 --- a/py/modsys.c +++ b/py/modsys.c @@ -121,6 +121,26 @@ STATIC mp_obj_t mp_sys_print_exception(mp_uint_t n_args, const mp_obj_t *args) { } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_print_exception); +#if MICROPY_PY_SYS_EXC_INFO +STATIC mp_obj_t mp_sys_exc_info(void) { + mp_obj_t cur_exc = MP_STATE_VM(cur_exception); + mp_obj_tuple_t *t = mp_obj_new_tuple(3, NULL); + + if (cur_exc == MP_OBJ_NULL) { + t->items[0] = mp_const_none; + t->items[1] = mp_const_none; + t->items[2] = mp_const_none; + return t; + } + + t->items[0] = mp_obj_get_type(cur_exc); + t->items[1] = cur_exc; + t->items[2] = mp_const_none; + return t; +} +MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info); +#endif + STATIC const mp_map_elem_t mp_module_sys_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_sys) }, @@ -163,6 +183,10 @@ STATIC const mp_map_elem_t mp_module_sys_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_stderr), (mp_obj_t)&mp_sys_stderr_obj }, #endif + #if MICROPY_PY_SYS_EXC_INFO + { MP_OBJ_NEW_QSTR(MP_QSTR_exc_info), (mp_obj_t)&mp_sys_exc_info_obj }, + #endif + /* * Extensions to CPython */ diff --git a/py/mpconfig.h b/py/mpconfig.h index df18290a20..59d6532d04 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -573,6 +573,12 @@ typedef double mp_float_t; #define MICROPY_PY_SYS_MAXSIZE (0) #endif +// Whether to provide "sys.exc_info" function +// Avoid enabling this, this function is Python2 heritage +#ifndef MICROPY_PY_SYS_EXC_INFO +#define MICROPY_PY_SYS_EXC_INFO (0) +#endif + // Whether to provide "sys.exit" function #ifndef MICROPY_PY_SYS_EXIT #define MICROPY_PY_SYS_EXIT (0) diff --git a/py/mpstate.h b/py/mpstate.h index a2366df7f4..42593e4c40 100644 --- a/py/mpstate.h +++ b/py/mpstate.h @@ -107,6 +107,11 @@ typedef struct _mp_state_vm_t { // pending exception object (MP_OBJ_NULL if not pending) mp_obj_t mp_pending_exception; + // current exception being handled, for sys.exc_info() + #if MICROPY_PY_SYS_EXC_INFO + mp_obj_t cur_exception; + #endif + // dictionary for the __main__ module mp_obj_dict_t dict_main; diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 5623c22084..8b1c9297dd 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -457,6 +457,9 @@ Q(implementation) #if MICROPY_PY_SYS_MAXSIZE Q(maxsize) #endif +#if MICROPY_PY_SYS_EXC_INFO +Q(exc_info) +#endif Q(print_exception) #endif @@ -1228,6 +1228,10 @@ pending_exception_check: exception_handler: // exception occurred + #if MICROPY_PY_SYS_EXC_INFO + MP_STATE_VM(cur_exception) = nlr.ret_val; + #endif + #if SELECTIVE_EXC_IP // with selective ip, we store the ip 1 byte past the opcode, so move ptr back code_state->ip -= 1; |