diff --git a/pybricks/util_mp/pb_kwarg_helper.h b/pybricks/util_mp/pb_kwarg_helper.h index 67447e29d..9de0ed3d7 100644 --- a/pybricks/util_mp/pb_kwarg_helper.h +++ b/pybricks/util_mp/pb_kwarg_helper.h @@ -101,4 +101,7 @@ // Optional keyword argument with default None value #define PB_ARG_DEFAULT_NONE(name)(name, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}) +// Test if all parsed arguments are None. +#define PB_PARSE_ARGS_METHOD_ALL_NONE() (pb_obj_parsed_args_all_none(parsed_args, MP_ARRAY_SIZE(parsed_args))) + #endif // PYBRICKS_INCLUDED_PBKWARG_H diff --git a/pybricks/util_mp/pb_obj_helper.c b/pybricks/util_mp/pb_obj_helper.c index c574c2e7b..f5ad41e3f 100644 --- a/pybricks/util_mp/pb_obj_helper.c +++ b/pybricks/util_mp/pb_obj_helper.c @@ -244,3 +244,18 @@ mp_obj_t pb_function_import_helper(qstr module_name, qstr function_name) { } return dest[0]; } + +/** + * Tests that all parsed arguments are None. + * + * @param parsed_args [in] The parsed arguments. + * @param num_parsed_args [in] The number of parsed arguments. + */ +bool pb_obj_parsed_args_all_none(mp_arg_val_t *parsed_args, size_t num_parsed_args) { + for (size_t i = 0; i < num_parsed_args; i++) { + if (parsed_args[i].u_obj != mp_const_none) { + return false; + } + } + return true; +} diff --git a/pybricks/util_mp/pb_obj_helper.h b/pybricks/util_mp/pb_obj_helper.h index 940fc4fb1..339e72286 100644 --- a/pybricks/util_mp/pb_obj_helper.h +++ b/pybricks/util_mp/pb_obj_helper.h @@ -10,6 +10,7 @@ #include #include "py/obj.h" +#include "py/runtime.h" // like mp_obj_get_int() but also allows float #if MICROPY_PY_BUILTINS_FLOAT @@ -68,4 +69,6 @@ void pb_attribute_handler(mp_obj_t self_in, qstr attr, mp_obj_t *dest); mp_obj_t pb_function_import_helper(qstr module_name, qstr function_name); +bool pb_obj_parsed_args_all_none(mp_arg_val_t *parsed_args, size_t num_parsed_args); + #endif // PYBRICKS_INCLUDED_PBOBJ_H