// helper function to print an exception with traceback void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) { if (mp_obj_is_exception_instance(exc)) { size_t n, *values; mp_obj_exception_get_traceback(exc, &n, &values); if (n > 0) { assert(n % 3 == 0); mp_print_str(print, "Traceback (most recent call last):\n"); for (int i = n - 3; i >= 0; i -= 3) { #if MICROPY_ENABLE_SOURCE_LINE mp_printf(print, " File \"%q\", line %d", values[i], (int)values[i + 1]); #else mp_printf(print, " File \"%q\"", values[i]); #endif // the block name can be NULL if it's unknown qstr block = values[i + 2]; if (block == MP_QSTR_NULL) { mp_print_str(print, "\n"); } else { mp_printf(print, ", in %q\n", block); } } } } mp_obj_print_helper(print, exc, PRINT_EXC); mp_print_str(print, "\n"); }
// helper function to print an exception with traceback void mp_obj_print_exception(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t exc) { if (mp_obj_is_exception_instance(exc)) { mp_uint_t n, *values; mp_obj_exception_get_traceback(exc, &n, &values); if (n > 0) { assert(n % 3 == 0); print(env, "Traceback (most recent call last):\n"); for (int i = n - 3; i >= 0; i -= 3) { #if MICROPY_ENABLE_SOURCE_LINE print(env, " File \"%s\", line %d", qstr_str(values[i]), (int)values[i + 1]); #else print(env, " File \"%s\"", qstr_str(values[i])); #endif // the block name can be NULL if it's unknown qstr block = values[i + 2]; if (block == MP_QSTR_NULL) { print(env, "\n"); } else { print(env, ", in %s\n", qstr_str(block)); } } } } mp_obj_print_helper(print, env, exc, PRINT_EXC); print(env, "\n"); }
// helper function to print an exception with traceback void mp_obj_print_exception(mp_obj_t exc) { if (mp_obj_is_exception_instance(exc)) { machine_uint_t n, *values; mp_obj_exception_get_traceback(exc, &n, &values); if (n > 0) { assert(n % 3 == 0); printf("Traceback (most recent call last):\n"); for (int i = n - 3; i >= 0; i -= 3) { #if MICROPY_ENABLE_SOURCE_LINE printf(" File \"%s\", line %d, in %s\n", qstr_str(values[i]), (int)values[i + 1], qstr_str(values[i + 2])); #else printf(" File \"%s\", in %s\n", qstr_str(values[i]), qstr_str(values[i + 2])); #endif } } } mp_obj_print(exc, PRINT_EXC); printf("\n"); }
void microbit_display_exception(mp_obj_t exc_in) { mp_uint_t n, *values; mp_obj_exception_get_traceback(exc_in, &n, &values); if (n >= 3) { vstr_t vstr; mp_print_t print; vstr_init_print(&vstr, 50, &print); #if MICROPY_ENABLE_SOURCE_LINE mp_printf(&print, "line %u ", values[1]); #endif if (mp_obj_is_native_exception_instance(exc_in)) { mp_obj_exception_t *exc = exc_in; mp_printf(&print, "%q ", exc->base.type->name); if (exc->args != NULL && exc->args->len != 0) { mp_obj_print_helper(&print, exc->args->items[0], PRINT_STR); } } mp_hal_display_string(vstr_null_terminated_str(&vstr)); vstr_clear(&vstr); } }