pull/11244/merge
David Grayson 2024-04-04 07:11:48 +08:00 zatwierdzone przez GitHub
commit e4bd387619
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
3 zmienionych plików z 50 dodań i 0 usunięć

Wyświetl plik

@ -52,6 +52,28 @@ Functions
present in pre-built firmware (due to it affecting performance). The relevant
configuration option is *MICROPY_PY_SYS_SETTRACE*.
.. function:: _exc_traceback(exc)
Retrieves traceback information from an exception object, including
the filename, line number, and code block name for every code location
on the call stack when the exception was thrown.
.. admonition:: Difference to CPython
:class: attention
This function is a MicroPython extension intended to provide similar
functionality to the ``__traceback__`` attribute of exception
objects in CPython.
.. admonition:: Unstable
:class: attention
This function directly exposes the internal traceback data used by
MicroPython. Future versions might introduce incompatible changes to
the format.
Constants
---------

Wyświetl plik

@ -180,6 +180,26 @@ static mp_obj_t mp_sys_print_exception(size_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_TRACEBACK
STATIC mp_obj_t mp_sys_exc_traceback(mp_obj_t exc) {
if (!mp_obj_is_exception_instance(exc)) {
mp_raise_TypeError(MP_ERROR_TEXT("not an exception"));
}
size_t n, *values;
mp_obj_exception_get_traceback(exc, &n, &values);
// Assumption: n is a multiple of 3.
mp_obj_t obj = mp_obj_new_list(n, NULL);
mp_obj_list_t *list = MP_OBJ_TO_PTR(obj);
for (size_t i = 0; i < list->len; i += 3) {
list->items[i + 0] = MP_OBJ_NEW_QSTR(values[i + 0]); // filename
list->items[i + 1] = MP_OBJ_NEW_SMALL_INT(values[i + 1]); // line
list->items[i + 2] = MP_OBJ_NEW_QSTR(values[i + 2]); // block
}
return obj;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_exc_traceback_obj, mp_sys_exc_traceback);
#endif
#if MICROPY_PY_SYS_EXC_INFO
static mp_obj_t mp_sys_exc_info(void) {
mp_obj_t cur_exc = MP_OBJ_FROM_PTR(MP_STATE_VM(cur_exception));
@ -334,6 +354,9 @@ static const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
*/
{ MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
#if MICROPY_PY_SYS_EXC_TRACEBACK
{ MP_ROM_QSTR(MP_QSTR__exc_traceback), MP_ROM_PTR(&mp_sys_exc_traceback_obj) },
#endif
#if MICROPY_PY_SYS_ATEXIT
{ MP_ROM_QSTR(MP_QSTR_atexit), MP_ROM_PTR(&mp_sys_atexit_obj) },
#endif

Wyświetl plik

@ -1447,6 +1447,11 @@ typedef double mp_float_t;
#define MICROPY_PY_SYS_MODULES (1)
#endif
// Whether to provide "sys._exc_traceback" function.
#ifndef MICROPY_PY_SYS_EXC_TRACEBACK
#define MICROPY_PY_SYS_EXC_TRACEBACK (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)
#endif
// Whether to provide "sys.exc_info" function
// Avoid enabling this, this function is Python2 heritage
#ifndef MICROPY_PY_SYS_EXC_INFO