diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-05-20 22:20:37 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-05-20 22:20:37 +0300 |
commit | 0ab372585f57c80202a432bd757d52088de80850 (patch) | |
tree | d00e04f3962bd1dc3c1023af368396841c0a5b7c /extmod/moduos_dupterm.c | |
parent | 3a29db8e58d963747697025e847444430c9f8b9e (diff) |
extmod/moduos_dupterm: Dumpterm subsystem is responsible for closing stream.
Make dupterm subsystem close a term stream object when EOF or error occurs.
There's no other party than dupterm itself in a better position to do this,
and this is required to properly reclaim stream resources, especially if
multiple dupterm sessions may be established (e.g. as networking
connections).
Diffstat (limited to 'extmod/moduos_dupterm.c')
-rw-r--r-- | extmod/moduos_dupterm.c | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/extmod/moduos_dupterm.c b/extmod/moduos_dupterm.c index 41b8b3c81..4c9f9e494 100644 --- a/extmod/moduos_dupterm.c +++ b/extmod/moduos_dupterm.c @@ -31,9 +31,20 @@ #include "py/nlr.h" #include "py/runtime.h" #include "py/objtuple.h" +#include "py/stream.h" #if MICROPY_PY_OS_DUPTERM +void mp_uos_deactivate(const char *msg, mp_obj_t exc) { + mp_obj_t term = MP_STATE_PORT(term_obj); + MP_STATE_PORT(term_obj) = NULL; + mp_printf(&mp_plat_print, msg); + if (exc != MP_OBJ_NULL) { + mp_obj_print_exception(&mp_plat_print, exc); + } + mp_stream_close(term); +} + void mp_uos_dupterm_tx_strn(const char *str, size_t len) { if (MP_STATE_PORT(term_obj) != MP_OBJ_NULL) { nlr_buf_t nlr; @@ -44,9 +55,7 @@ void mp_uos_dupterm_tx_strn(const char *str, size_t len) { mp_call_method_n_kw(1, 0, write_m); nlr_pop(); } else { - MP_STATE_PORT(term_obj) = NULL; - mp_printf(&mp_plat_print, "dupterm: Exception in write() method, deactivating: "); - mp_obj_print_exception(&mp_plat_print, nlr.ret_val); + mp_uos_deactivate("dupterm: Exception in write() method, deactivating: ", nlr.ret_val); } } } |