Commit 7f23384d authored by Damien George's avatar Damien George
Browse files

py: Make terse_arg_mismatch a global function and use it elsewhere.

Reduces code size when MICROPY_ERROR_REPORTING_TERSE is selected.
parent 276159e5
......@@ -34,16 +34,12 @@
#include "obj.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) {
// TODO maybe take the function name as an argument so we can print nicer error messages
if (n_kw && !takes_kw) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_arg_mismatch();
mp_arg_error_terse_mismatch();
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
"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 != n_args_min) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_arg_mismatch();
mp_arg_error_terse_mismatch();
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"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 {
if (n_args < n_args_min) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_arg_mismatch();
mp_arg_error_terse_mismatch();
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"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) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_arg_mismatch();
mp_arg_error_terse_mismatch();
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"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 (allowed[i].flags & MP_ARG_REQUIRED) {
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_arg_mismatch();
mp_arg_error_terse_mismatch();
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"'%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) {
extra_positional:
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_arg_mismatch();
mp_arg_error_terse_mismatch();
} else {
// TODO better error message
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 (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
terse_arg_mismatch();
mp_arg_error_terse_mismatch();
} else {
// TODO better error message
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);
}
#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
NORETURN void mp_arg_error_unimpl_kw(void) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
......
......@@ -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) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
// Generic message, to be reused for other argument issues
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
"argument num/types mismatch"));
// generic message, used also for other argument issues
mp_arg_error_terse_mismatch();
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function takes %d positional arguments but %d were given", expected, given));
......
......@@ -33,6 +33,7 @@
#include "qstr.h"
#include "obj.h"
#include "objtuple.h"
#include "runtime.h"
#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_uint_t num_fields = type->n_fields;
if (n_args + n_kw != num_fields) {
// 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));
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
mp_arg_error_terse_mismatch();
} else {
// 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;
......@@ -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]);
int id = namedtuple_find_field(type, kw);
if (id == -1) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"__new__() got an unexpected keyword argument '%s'",
qstr_str(kw)));
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
mp_arg_error_terse_mismatch();
} 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) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"__new__() got multiple values for argument '%s'",
qstr_str(kw)));
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
mp_arg_error_terse_mismatch();
} 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];
}
......
......@@ -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_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);
NORETURN void mp_arg_error_terse_mismatch(void);
NORETURN void mp_arg_error_unimpl_kw(void);
mp_obj_dict_t *mp_locals_get(void);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment