py: mp_call_function_*_protected(): Pass-thru return value if possible.
Return the result of called function. If exception happened, return MP_OBJ_NULL. Allows to use mp_call_function_*_protected() with callbacks returning values, etc.
This commit is contained in:
parent
3ff7040c8a
commit
62b96147e6
@ -106,8 +106,9 @@ mp_obj_t mp_call_method_n_kw(size_t n_args, size_t n_kw, const mp_obj_t *args);
|
|||||||
mp_obj_t mp_call_method_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args);
|
mp_obj_t mp_call_method_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args);
|
||||||
mp_obj_t mp_call_method_self_n_kw(mp_obj_t meth, mp_obj_t self, size_t n_args, size_t n_kw, const mp_obj_t *args);
|
mp_obj_t mp_call_method_self_n_kw(mp_obj_t meth, mp_obj_t self, size_t n_args, size_t n_kw, const mp_obj_t *args);
|
||||||
// Call function and catch/dump exception - for Python callbacks from C code
|
// Call function and catch/dump exception - for Python callbacks from C code
|
||||||
void mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg);
|
// (return MP_OBJ_NULL in case of exception).
|
||||||
void mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
|
mp_obj_t mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg);
|
||||||
|
mp_obj_t mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
|
||||||
|
|
||||||
typedef struct _mp_call_args_t {
|
typedef struct _mp_call_args_t {
|
||||||
mp_obj_t fun;
|
mp_obj_t fun;
|
||||||
|
@ -27,22 +27,26 @@
|
|||||||
|
|
||||||
#include "py/runtime.h"
|
#include "py/runtime.h"
|
||||||
|
|
||||||
void mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg) {
|
mp_obj_t mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg) {
|
||||||
nlr_buf_t nlr;
|
nlr_buf_t nlr;
|
||||||
if (nlr_push(&nlr) == 0) {
|
if (nlr_push(&nlr) == 0) {
|
||||||
mp_call_function_1(fun, arg);
|
mp_obj_t ret = mp_call_function_1(fun, arg);
|
||||||
nlr_pop();
|
nlr_pop();
|
||||||
|
return ret;
|
||||||
} else {
|
} else {
|
||||||
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
|
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
|
||||||
|
return MP_OBJ_NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
|
mp_obj_t mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
|
||||||
nlr_buf_t nlr;
|
nlr_buf_t nlr;
|
||||||
if (nlr_push(&nlr) == 0) {
|
if (nlr_push(&nlr) == 0) {
|
||||||
mp_call_function_2(fun, arg1, arg2);
|
mp_obj_t ret = mp_call_function_2(fun, arg1, arg2);
|
||||||
nlr_pop();
|
nlr_pop();
|
||||||
|
return ret;
|
||||||
} else {
|
} else {
|
||||||
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
|
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
|
||||||
|
return MP_OBJ_NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user