aboutsummaryrefslogtreecommitdiff
path: root/py/argcheck.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-11-06 17:36:16 +0000
committerDamien George <damien.p.george@gmail.com>2014-11-06 17:36:16 +0000
commit1e9a92f84fb58db610e20b766052292edc28d25b (patch)
tree0005072b03a413c4b01df72f61066e0aaeca23f3 /py/argcheck.c
parentb6b34cd3f6585eed455473bc149e9db758a45d9c (diff)
py: Use shorter, static error msgs when ERROR_REPORTING_TERSE enabled.
Going from MICROPY_ERROR_REPORTING_NORMAL to MICROPY_ERROR_REPORTING_TERSE now saves 2020 bytes ROM for ARM Thumb2, and 2200 bytes ROM for 32-bit x86. This is about a 2.5% code size reduction for bare-arm.
Diffstat (limited to 'py/argcheck.c')
-rw-r--r--py/argcheck.c67
1 files changed, 52 insertions, 15 deletions
diff --git a/py/argcheck.c b/py/argcheck.c
index c8daa6033..f151eb02f 100644
--- a/py/argcheck.c
+++ b/py/argcheck.c
@@ -34,28 +34,49 @@
#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) {
- nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ terse_arg_mismatch();
+ } else {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
+ "function does not take keyword arguments"));
+ }
}
if (n_args_min == n_args_max) {
if (n_args != n_args_min) {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
- "function takes %d positional arguments but %d were given",
- n_args_min, n_args));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ terse_arg_mismatch();
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
+ "function takes %d positional arguments but %d were given",
+ n_args_min, n_args));
+ }
}
} else {
if (n_args < n_args_min) {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
- "function missing %d required positional arguments",
- n_args_min - n_args));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ terse_arg_mismatch();
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
+ "function missing %d required positional arguments",
+ n_args_min - n_args));
+ }
} else if (n_args > n_args_max) {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
- "function expected at most %d arguments, got %d",
- n_args_max, n_args));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ terse_arg_mismatch();
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
+ "function expected at most %d arguments, got %d",
+ n_args_max, n_args));
+ }
}
}
}
@@ -74,7 +95,13 @@ void mp_arg_parse_all(mp_uint_t n_pos, const mp_obj_t *pos, mp_map_t *kws, mp_ui
mp_map_elem_t *kw = mp_map_lookup(kws, MP_OBJ_NEW_QSTR(allowed[i].qstr), MP_MAP_LOOKUP);
if (kw == NULL) {
if (allowed[i].flags & MP_ARG_REQUIRED) {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' argument required", qstr_str(allowed[i].qstr)));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ terse_arg_mismatch();
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
+ "'%s' argument required",
+ qstr_str(allowed[i].qstr)));
+ }
}
out_vals[i] = allowed[i].defval;
continue;
@@ -94,13 +121,23 @@ 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) {
- // TODO better error message
extra_positional:
- nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "extra positional arguments given"));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ terse_arg_mismatch();
+ } else {
+ // TODO better error message
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
+ "extra positional arguments given"));
+ }
}
if (kws_found < kws->used) {
- // TODO better error message
- nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "extra keyword arguments given"));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ terse_arg_mismatch();
+ } else {
+ // TODO better error message
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
+ "extra keyword arguments given"));
+ }
}
}