py: Make terse_arg_mismatch a global function and use it elsewhere.
Reduces code size when MICROPY_ERROR_REPORTING_TERSE is selected.
This commit is contained in:
parent
276159e5dd
commit
7f23384d49
@ -34,16 +34,12 @@
|
|||||||
#include "obj.h"
|
#include "obj.h"
|
||||||
#include "runtime.h"
|
#include "runtime.h"
|
||||||
|
|
||||||
STATIC NORETURN void terse_arg_mismatch(void) {
|
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "argument num/types mismatch"));
|
|
||||||
}
|
|
||||||
|
|
||||||
void mp_arg_check_num(mp_uint_t n_args, mp_uint_t n_kw, mp_uint_t n_args_min, mp_uint_t n_args_max, bool takes_kw) {
|
void mp_arg_check_num(mp_uint_t n_args, mp_uint_t n_kw, mp_uint_t n_args_min, mp_uint_t n_args_max, bool takes_kw) {
|
||||||
// TODO maybe take the function name as an argument so we can print nicer error messages
|
// TODO maybe take the function name as an argument so we can print nicer error messages
|
||||||
|
|
||||||
if (n_kw && !takes_kw) {
|
if (n_kw && !takes_kw) {
|
||||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_arg_mismatch();
|
mp_arg_error_terse_mismatch();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
||||||
"function does not take keyword arguments"));
|
"function does not take keyword arguments"));
|
||||||
@ -53,7 +49,7 @@ void mp_arg_check_num(mp_uint_t n_args, mp_uint_t n_kw, mp_uint_t n_args_min, mp
|
|||||||
if (n_args_min == n_args_max) {
|
if (n_args_min == n_args_max) {
|
||||||
if (n_args != n_args_min) {
|
if (n_args != n_args_min) {
|
||||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_arg_mismatch();
|
mp_arg_error_terse_mismatch();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
||||||
"function takes %d positional arguments but %d were given",
|
"function takes %d positional arguments but %d were given",
|
||||||
@ -63,7 +59,7 @@ void mp_arg_check_num(mp_uint_t n_args, mp_uint_t n_kw, mp_uint_t n_args_min, mp
|
|||||||
} else {
|
} else {
|
||||||
if (n_args < n_args_min) {
|
if (n_args < n_args_min) {
|
||||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_arg_mismatch();
|
mp_arg_error_terse_mismatch();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
||||||
"function missing %d required positional arguments",
|
"function missing %d required positional arguments",
|
||||||
@ -71,7 +67,7 @@ void mp_arg_check_num(mp_uint_t n_args, mp_uint_t n_kw, mp_uint_t n_args_min, mp
|
|||||||
}
|
}
|
||||||
} else if (n_args > n_args_max) {
|
} else if (n_args > n_args_max) {
|
||||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_arg_mismatch();
|
mp_arg_error_terse_mismatch();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
||||||
"function expected at most %d arguments, got %d",
|
"function expected at most %d arguments, got %d",
|
||||||
@ -96,7 +92,7 @@ void mp_arg_parse_all(mp_uint_t n_pos, const mp_obj_t *pos, mp_map_t *kws, mp_ui
|
|||||||
if (kw == NULL) {
|
if (kw == NULL) {
|
||||||
if (allowed[i].flags & MP_ARG_REQUIRED) {
|
if (allowed[i].flags & MP_ARG_REQUIRED) {
|
||||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_arg_mismatch();
|
mp_arg_error_terse_mismatch();
|
||||||
} else {
|
} else {
|
||||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
||||||
"'%s' argument required",
|
"'%s' argument required",
|
||||||
@ -123,7 +119,7 @@ void mp_arg_parse_all(mp_uint_t n_pos, const mp_obj_t *pos, mp_map_t *kws, mp_ui
|
|||||||
if (pos_found < n_pos) {
|
if (pos_found < n_pos) {
|
||||||
extra_positional:
|
extra_positional:
|
||||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_arg_mismatch();
|
mp_arg_error_terse_mismatch();
|
||||||
} else {
|
} else {
|
||||||
// TODO better error message
|
// TODO better error message
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
||||||
@ -132,7 +128,7 @@ void mp_arg_parse_all(mp_uint_t n_pos, const mp_obj_t *pos, mp_map_t *kws, mp_ui
|
|||||||
}
|
}
|
||||||
if (kws_found < kws->used) {
|
if (kws_found < kws->used) {
|
||||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
terse_arg_mismatch();
|
mp_arg_error_terse_mismatch();
|
||||||
} else {
|
} else {
|
||||||
// TODO better error message
|
// TODO better error message
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
||||||
@ -147,6 +143,12 @@ void mp_arg_parse_all_kw_array(mp_uint_t n_pos, mp_uint_t n_kw, const mp_obj_t *
|
|||||||
mp_arg_parse_all(n_pos, args, &kw_args, n_allowed, allowed, out_vals);
|
mp_arg_parse_all(n_pos, args, &kw_args, n_allowed, allowed, out_vals);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
||||||
|
NORETURN void mp_arg_error_terse_mismatch(void) {
|
||||||
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "argument num/types mismatch"));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#if MICROPY_CPYTHON_COMPAT
|
#if MICROPY_CPYTHON_COMPAT
|
||||||
NORETURN void mp_arg_error_unimpl_kw(void) {
|
NORETURN void mp_arg_error_unimpl_kw(void) {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
|
||||||
|
5
py/bc.c
5
py/bc.c
@ -62,9 +62,8 @@ mp_uint_t mp_decode_uint(const byte **ptr) {
|
|||||||
|
|
||||||
STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, mp_uint_t expected, mp_uint_t given) {
|
STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, mp_uint_t expected, mp_uint_t given) {
|
||||||
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
||||||
// Generic message, to be reused for other argument issues
|
// generic message, used also for other argument issues
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
|
mp_arg_error_terse_mismatch();
|
||||||
"argument num/types mismatch"));
|
|
||||||
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
|
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
|
||||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
||||||
"function takes %d positional arguments but %d were given", expected, given));
|
"function takes %d positional arguments but %d were given", expected, given));
|
||||||
|
@ -33,6 +33,7 @@
|
|||||||
#include "qstr.h"
|
#include "qstr.h"
|
||||||
#include "obj.h"
|
#include "obj.h"
|
||||||
#include "objtuple.h"
|
#include "objtuple.h"
|
||||||
|
#include "runtime.h"
|
||||||
|
|
||||||
#if MICROPY_PY_COLLECTIONS
|
#if MICROPY_PY_COLLECTIONS
|
||||||
|
|
||||||
@ -86,10 +87,14 @@ STATIC mp_obj_t namedtuple_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
|
|||||||
mp_obj_namedtuple_type_t *type = type_in;
|
mp_obj_namedtuple_type_t *type = type_in;
|
||||||
mp_uint_t num_fields = type->n_fields;
|
mp_uint_t num_fields = type->n_fields;
|
||||||
if (n_args + n_kw != num_fields) {
|
if (n_args + n_kw != num_fields) {
|
||||||
// Counts include implicit "self"
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
mp_arg_error_terse_mismatch();
|
||||||
"__new__() takes %d positional arguments but %d were given",
|
} else {
|
||||||
num_fields + 1, n_args + n_kw + 1));
|
// Counts include implicit "self"
|
||||||
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
||||||
|
"__new__() takes %d positional arguments but %d were given",
|
||||||
|
num_fields + 1, n_args + n_kw + 1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mp_obj_t *arg_objects;
|
mp_obj_t *arg_objects;
|
||||||
@ -108,14 +113,22 @@ STATIC mp_obj_t namedtuple_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
|
|||||||
qstr kw = MP_OBJ_QSTR_VALUE(args[i]);
|
qstr kw = MP_OBJ_QSTR_VALUE(args[i]);
|
||||||
int id = namedtuple_find_field(type, kw);
|
int id = namedtuple_find_field(type, kw);
|
||||||
if (id == -1) {
|
if (id == -1) {
|
||||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
"__new__() got an unexpected keyword argument '%s'",
|
mp_arg_error_terse_mismatch();
|
||||||
qstr_str(kw)));
|
} else {
|
||||||
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
||||||
|
"__new__() got an unexpected keyword argument '%s'",
|
||||||
|
qstr_str(kw)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (arg_objects[id] != NULL) {
|
if (arg_objects[id] != NULL) {
|
||||||
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||||
"__new__() got multiple values for argument '%s'",
|
mp_arg_error_terse_mismatch();
|
||||||
qstr_str(kw)));
|
} else {
|
||||||
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
|
||||||
|
"__new__() got multiple values for argument '%s'",
|
||||||
|
qstr_str(kw)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
arg_objects[id] = args[i + 1];
|
arg_objects[id] = args[i + 1];
|
||||||
}
|
}
|
||||||
|
@ -61,6 +61,7 @@ void mp_deinit(void);
|
|||||||
void mp_arg_check_num(mp_uint_t n_args, mp_uint_t n_kw, mp_uint_t n_args_min, mp_uint_t n_args_max, bool takes_kw);
|
void mp_arg_check_num(mp_uint_t n_args, mp_uint_t n_kw, mp_uint_t n_args_min, mp_uint_t n_args_max, bool takes_kw);
|
||||||
void mp_arg_parse_all(mp_uint_t n_pos, const mp_obj_t *pos, mp_map_t *kws, mp_uint_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
|
void mp_arg_parse_all(mp_uint_t n_pos, const mp_obj_t *pos, mp_map_t *kws, mp_uint_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
|
||||||
void mp_arg_parse_all_kw_array(mp_uint_t n_pos, mp_uint_t n_kw, const mp_obj_t *args, mp_uint_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
|
void mp_arg_parse_all_kw_array(mp_uint_t n_pos, mp_uint_t n_kw, const mp_obj_t *args, mp_uint_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
|
||||||
|
NORETURN void mp_arg_error_terse_mismatch(void);
|
||||||
NORETURN void mp_arg_error_unimpl_kw(void);
|
NORETURN void mp_arg_error_unimpl_kw(void);
|
||||||
|
|
||||||
mp_obj_dict_t *mp_locals_get(void);
|
mp_obj_dict_t *mp_locals_get(void);
|
||||||
|
Loading…
Reference in New Issue
Block a user