From 4021b1e1b86a86af9e43c393c447110d36ebb36d Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 12 Oct 2016 18:00:32 +0300 Subject: lib/utils/pyexec: Don't treat SystemExit as "forced exit". "Forced exit" is treated as soft-reboot (Ctrl+D). But expected effect of calling sys.exit() is termination of the current script, not any further and more serious actions like mentioned soft reboot. --- lib/utils/pyexec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/utils/pyexec.c') diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c index 2bc7a00cc..aac211894 100644 --- a/lib/utils/pyexec.c +++ b/lib/utils/pyexec.c @@ -99,7 +99,7 @@ STATIC int parse_compile_execute(void *source, mp_parse_input_kind_t input_kind, // check for SystemExit if (mp_obj_is_subclass_fast(mp_obj_get_type((mp_obj_t)nlr.ret_val), &mp_type_SystemExit)) { // at the moment, the value of SystemExit is unused - ret = PYEXEC_FORCED_EXIT; + ret = 0; } else { mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); ret = 0; -- cgit v1.2.3 From 824f5c5a32d740acad50d23b7ab1d69660dcf3ad Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 14 Oct 2016 16:46:34 +1100 Subject: py/vstr: Combine vstr_new_size with vstr_new since they are rarely used. Now there is just one function to allocate a new vstr, namely vstr_new (in addition to vstr_init etc). The caller of this function should know what initial size to allocate for the buffer, or at least have some policy or config option, instead of leaving it to a default (as it was before). --- lib/utils/pyexec.c | 2 +- py/misc.h | 3 +-- py/objstringio.c | 2 +- py/vstr.c | 14 +------------- teensy/main.c | 2 +- unix/coverage.c | 2 +- 6 files changed, 6 insertions(+), 19 deletions(-) (limited to 'lib/utils/pyexec.c') diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c index aac211894..5824e1403 100644 --- a/lib/utils/pyexec.c +++ b/lib/utils/pyexec.c @@ -148,7 +148,7 @@ STATIC int pyexec_raw_repl_process_char(int c); STATIC int pyexec_friendly_repl_process_char(int c); void pyexec_event_repl_init(void) { - MP_STATE_VM(repl_line) = vstr_new_size(32); + MP_STATE_VM(repl_line) = vstr_new(32); repl.cont_line = false; readline_init(MP_STATE_VM(repl_line), ">>> "); if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) { diff --git a/py/misc.h b/py/misc.h index 3ed227a35..e60665e59 100644 --- a/py/misc.h +++ b/py/misc.h @@ -151,8 +151,7 @@ void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf); struct _mp_print_t; void vstr_init_print(vstr_t *vstr, size_t alloc, struct _mp_print_t *print); void vstr_clear(vstr_t *vstr); -vstr_t *vstr_new(void); -vstr_t *vstr_new_size(size_t alloc); +vstr_t *vstr_new(size_t alloc); void vstr_free(vstr_t *vstr); static inline void vstr_reset(vstr_t *vstr) { vstr->len = 0; } static inline char *vstr_str(vstr_t *vstr) { return vstr->buf; } diff --git a/py/objstringio.c b/py/objstringio.c index be1a7d89c..212d8e314 100644 --- a/py/objstringio.c +++ b/py/objstringio.c @@ -150,7 +150,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(stringio___exit___obj, 4, 4, stringio STATIC mp_obj_stringio_t *stringio_new(const mp_obj_type_t *type) { mp_obj_stringio_t *o = m_new_obj(mp_obj_stringio_t); o->base.type = type; - o->vstr = vstr_new(); + o->vstr = vstr_new(16); o->pos = 0; return o; } diff --git a/py/vstr.c b/py/vstr.c index 5096475f1..6a91552b5 100644 --- a/py/vstr.c +++ b/py/vstr.c @@ -74,20 +74,8 @@ void vstr_clear(vstr_t *vstr) { vstr->buf = NULL; } -vstr_t *vstr_new(void) { +vstr_t *vstr_new(size_t alloc) { vstr_t *vstr = m_new_obj(vstr_t); - if (vstr == NULL) { - return NULL; - } - vstr_init(vstr, 16); - return vstr; -} - -vstr_t *vstr_new_size(size_t alloc) { - vstr_t *vstr = m_new_obj(vstr_t); - if (vstr == NULL) { - return NULL; - } vstr_init(vstr, alloc); return vstr; } diff --git a/teensy/main.c b/teensy/main.c index 890ee8149..ba7207fe8 100644 --- a/teensy/main.c +++ b/teensy/main.c @@ -317,7 +317,7 @@ soft_reset: pyexec_frozen_module("main.py"); #else { - vstr_t *vstr = vstr_new(); + vstr_t *vstr = vstr_new(16); vstr_add_str(vstr, "/"); if (pyb_config_main == MP_OBJ_NULL) { vstr_add_str(vstr, "main.py"); diff --git a/unix/coverage.c b/unix/coverage.c index c84a653f7..6a1b43fdc 100644 --- a/unix/coverage.c +++ b/unix/coverage.c @@ -34,7 +34,7 @@ STATIC mp_obj_t extra_coverage(void) { // vstr { mp_printf(&mp_plat_print, "# vstr\n"); - vstr_t *vstr = vstr_new_size(16); + vstr_t *vstr = vstr_new(16); vstr_hint_size(vstr, 32); vstr_add_str(vstr, "ts"); vstr_ins_byte(vstr, 1, 'e'); -- cgit v1.2.3 From ad3724e0bc87305f9280f65226066a199042d736 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 17 Oct 2016 13:14:59 +1100 Subject: lib/utils/pyexec: Allow behaviour of SystemExit to be configurable. Setting the pyexec_system_exit variable to PYEXEC_FORCED_EXT allows SystemExit exceptions to terminate the pyexec functions. --- lib/utils/pyexec.c | 6 +++++- lib/utils/pyexec.h | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'lib/utils/pyexec.c') diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c index 5824e1403..d7c257024 100644 --- a/lib/utils/pyexec.c +++ b/lib/utils/pyexec.c @@ -45,6 +45,7 @@ #include "genhdr/mpversion.h" pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL; +int pyexec_system_exit = 0; STATIC bool repl_display_debugging_info = 0; #define EXEC_FLAG_PRINT_EOF (1) @@ -61,6 +62,9 @@ STATIC int parse_compile_execute(void *source, mp_parse_input_kind_t input_kind, int ret = 0; uint32_t start = 0; + // by default a SystemExit exception returns 0 + pyexec_system_exit = 0; + nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { mp_obj_t module_fun; @@ -99,7 +103,7 @@ STATIC int parse_compile_execute(void *source, mp_parse_input_kind_t input_kind, // check for SystemExit if (mp_obj_is_subclass_fast(mp_obj_get_type((mp_obj_t)nlr.ret_val), &mp_type_SystemExit)) { // at the moment, the value of SystemExit is unused - ret = 0; + ret = pyexec_system_exit; } else { mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); ret = 0; diff --git a/lib/utils/pyexec.h b/lib/utils/pyexec.h index e0f62440e..ae69a195e 100644 --- a/lib/utils/pyexec.h +++ b/lib/utils/pyexec.h @@ -33,6 +33,11 @@ typedef enum { extern pyexec_mode_kind_t pyexec_mode_kind; +// Set this to the value (eg PYEXEC_FORCED_EXIT) that will be propagated through +// the pyexec functions if a SystemExit exception is raised by the running code. +// It will reset to 0 at the start of each execution (eg each REPL entry). +extern int pyexec_system_exit; + #define PYEXEC_FORCED_EXIT (0x100) #define PYEXEC_SWITCH_MODE (0x200) -- cgit v1.2.3