[code] Print errors in the Python console.

Change-Id: If3a096ee46105229b6c4c77906826e78666cdc11
This commit is contained in:
Léa Saviot
2017-11-14 13:33:41 +01:00
committed by Romain Goyet
parent 07c6acada4
commit b73170b77b
3 changed files with 41 additions and 12 deletions

View File

@@ -1,4 +1,3 @@
CodeApp = "Python"
CodeAppCapital = "PYTHON"
ConsolePrompt = ">>> "
ConsoleError = "Error"

View File

@@ -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)

View File

@@ -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 <apps/i18n.h>
@@ -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);