From 467504da01880bbe926f49ab777785dcee61d9a8 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 1 Jan 2016 22:05:30 +0200 Subject: [PATCH] extmod/moduos_dupterm: Handle exceptions in call to dupterm's .write() --- extmod/moduos_dupterm.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/extmod/moduos_dupterm.c b/extmod/moduos_dupterm.c index 7fe8524a56..99022df37a 100644 --- a/extmod/moduos_dupterm.c +++ b/extmod/moduos_dupterm.c @@ -36,10 +36,21 @@ void mp_uos_dupterm_tx_strn(const char *str, size_t len) { if (MP_STATE_PORT(term_obj) != MP_OBJ_NULL) { - mp_obj_t write_m[3]; - mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_write, write_m); - write_m[2] = mp_obj_new_bytearray_by_ref(len, (char*)str); - mp_call_method_n_kw(1, 0, write_m); + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + mp_obj_t write_m[3]; + mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_write, write_m); + write_m[2] = mp_obj_new_bytearray_by_ref(len, (char*)str); + mp_call_method_n_kw(1, 0, write_m); + nlr_pop(); + } else { + // Temporarily disable dupterm to avoid infinite recursion + mp_obj_t save_term = MP_STATE_PORT(term_obj); + MP_STATE_PORT(term_obj) = NULL; + mp_printf(&mp_plat_print, "dupterm: "); + mp_obj_print_exception(&mp_plat_print, nlr.ret_val); + MP_STATE_PORT(term_obj) = save_term; + } } }