diff --git a/apps/code/base.universal.i18n b/apps/code/base.universal.i18n index 7fca324ab..f5e54e9e3 100644 --- a/apps/code/base.universal.i18n +++ b/apps/code/base.universal.i18n @@ -1,4 +1,3 @@ CodeApp = "Python" CodeAppCapital = "PYTHON" ConsolePrompt = ">>> " -ConsoleError = "Error" diff --git a/python/port/mpconfigport.h b/python/port/mpconfigport.h index b0184898e..6ba46f43d 100644 --- a/python/port/mpconfigport.h +++ b/python/port/mpconfigport.h @@ -25,9 +25,9 @@ #define MICROPY_REPL_EVENT_DRIVEN (0) #define MICROPY_HELPER_REPL (1) #define MICROPY_HELPER_LEXER_UNIX (0) -#define MICROPY_ENABLE_SOURCE_LINE (0) +#define MICROPY_ENABLE_SOURCE_LINE (1) #define MICROPY_ENABLE_DOC_STRING (0) -#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE) +#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_DETAILED) #define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (0) #define MICROPY_PY_ASYNC_AWAIT (0) #define MICROPY_PY_BUILTINS_BYTEARRAY (0) diff --git a/python/port/port.cpp b/python/port/port.cpp index c8eb25416..d065acebf 100644 --- a/python/port/port.cpp +++ b/python/port/port.cpp @@ -5,15 +5,15 @@ extern "C" { #include "py/builtin.h" +#include "py/compile.h" +#include "py/gc.h" +#include "py/lexer.h" +#include "py/mperrno.h" #include "py/mphal.h" #include "py/nlr.h" -#include "py/compile.h" -#include "py/lexer.h" -#include "py/runtime.h" #include "py/repl.h" +#include "py/runtime.h" #include "py/stackctrl.h" -#include "py/gc.h" -#include "py/mperrno.h" } #include @@ -38,10 +38,40 @@ void MicroPython::ExecutionEnvironment::runCode(const char * str) { mp_call_function_0(module_fun); mp_hal_set_interrupt_char(-1); // Disable interrupt nlr_pop(); - } else { - // Uncaught exception - //return (mp_obj_t) nlr.ret_val; - printText(I18n::translate(I18n::Message::ConsoleError), 5); + } else { // Uncaught exception + /* mp_obj_print_exception is supposed to handle error printing. However, + * because we want to print custom information, we copied and modified the + * content of mp_obj_print_exception instead of calling it. */ + if (mp_obj_is_exception_instance((mp_obj_t)nlr.ret_val)) { + size_t n, *values; + mp_obj_exception_get_traceback((mp_obj_t)nlr.ret_val, &n, &values); + if (n > 0) { + assert(n % 3 == 0); + for (int i = n - 3; i >= 0; i -= 3) { + if (values[i] != 0 || i == 0) { + if (values[i] == 0) { + mp_printf(&mp_plat_print, " Last command\n"); + } else { +#if MICROPY_ENABLE_SOURCE_LINE + mp_printf(&mp_plat_print, " File \"%q\", line %d", values[i], (int)values[i + 1]); +#else + mp_printf(&mp_plat_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(&mp_plat_print, "\n"); + } else { + mp_printf(&mp_plat_print, ", in %q\n", block); + } + } + } + } + } + } + mp_obj_print_helper(&mp_plat_print, (mp_obj_t)nlr.ret_val, PRINT_EXC); + mp_print_str(&mp_plat_print, "\n"); + /* End of mp_obj_print_exception. */ } assert(sCurrentExecutionEnvironment == this);