Skip to content

Commit

Permalink
pybricks/util_mp: Unify all args None test.
Browse files Browse the repository at this point in the history
Since this is done in quite a few functions, this should make the build size smaller and the code easier to follow.
  • Loading branch information
laurensvalk committed Oct 21, 2024
1 parent 60273d6 commit 4fa2174
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pybricks/util_mp/pb_kwarg_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions pybricks/util_mp/pb_obj_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions pybricks/util_mp/pb_obj_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <pbio/color.h>

#include "py/obj.h"
#include "py/runtime.h"

// like mp_obj_get_int() but also allows float
#if MICROPY_PY_BUILTINS_FLOAT
Expand Down Expand Up @@ -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

0 comments on commit 4fa2174

Please sign in to comment.