add additional float validators

This commit is contained in:
Jeff Epler 2023-04-23 09:43:25 -05:00
parent 8e3c28e97d
commit c4104898ce
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
2 changed files with 17 additions and 0 deletions

View File

@ -186,6 +186,21 @@ mp_int_t mp_arg_validate_int_range(mp_int_t i, mp_int_t min, mp_int_t max, qstr
return i; return i;
} }
mp_float_t mp_arg_validate_type_float(mp_obj_t obj, qstr arg_name) {
mp_float_t a_float;
if (!mp_obj_get_float_maybe(obj, &a_float)) {
mp_raise_TypeError_varg(translate("%q must be of type %q, not %q"), arg_name, MP_QSTR_float, mp_obj_get_type(obj)->name);
}
return a_float;
}
void mp_arg_validate_obj_float_range(mp_obj_t float_in, mp_int_t min, mp_int_t max, qstr arg_name) {
const mp_float_t f = mp_arg_validate_type_float(float_in, arg_name);
if (f < (mp_float_t)min || f > (mp_float_t)max) {
mp_raise_ValueError_varg(translate("%q must be %d-%d"), arg_name, min, max);
}
}
mp_float_t mp_arg_validate_obj_float_non_negative(mp_obj_t float_in, mp_float_t default_for_null, qstr arg_name) { mp_float_t mp_arg_validate_obj_float_non_negative(mp_obj_t float_in, mp_float_t default_for_null, qstr arg_name) {
const mp_float_t f = (float_in == MP_OBJ_NULL) const mp_float_t f = (float_in == MP_OBJ_NULL)
? default_for_null ? default_for_null

View File

@ -103,6 +103,8 @@ mp_int_t mp_arg_validate_int_max(mp_int_t i, mp_int_t j, qstr arg_name);
mp_int_t mp_arg_validate_int_range(mp_int_t i, mp_int_t min, mp_int_t max, qstr arg_name); mp_int_t mp_arg_validate_int_range(mp_int_t i, mp_int_t min, mp_int_t max, qstr arg_name);
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_FLOAT
mp_float_t mp_arg_validate_obj_float_non_negative(mp_obj_t float_in, mp_float_t default_for_null, qstr arg_name); mp_float_t mp_arg_validate_obj_float_non_negative(mp_obj_t float_in, mp_float_t default_for_null, qstr arg_name);
void mp_arg_validate_obj_float_range(mp_obj_t float_in, mp_int_t min, mp_int_t max, qstr arg_name);
mp_float_t mp_arg_validate_type_float(mp_obj_t obj, qstr arg_name);
#endif #endif
mp_uint_t mp_arg_validate_length_min(mp_uint_t length, mp_uint_t min, qstr arg_name); mp_uint_t mp_arg_validate_length_min(mp_uint_t length, mp_uint_t min, qstr arg_name);
mp_uint_t mp_arg_validate_length_max(mp_uint_t length, mp_uint_t max, qstr arg_name); mp_uint_t mp_arg_validate_length_max(mp_uint_t length, mp_uint_t max, qstr arg_name);