8000 py: Make terse_arg_mismatch a global function and use it elsewhere. · stinos/micropython@7f23384 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit 7f23384

Browse files
committed
py: Make terse_arg_mismatch a global function and use it elsewhere.
Reduces code size when MICROPY_ERROR_REPORTING_TERSE is selected.
1 parent 276159e commit 7f23384

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

py/argcheck.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,12 @@
3434
#include "obj.h"
3535
#include "runtime.h"
3636

37-
STATIC NORETURN void terse_arg_mismatch(void) {
38-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "argument num/types mismatch"));
39-
}
40-
4137
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) {
4238
// TODO maybe take the function name as an argument so we can print nicer error messages
4339

4440
if (n_kw && !takes_kw) {
4541
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
46-
terse_arg_mismatch();
42+
mp_arg_error_terse_mismatch();
4743
} else {
4844
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
4945
"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
5349
if (n_args_min == n_args_max) {
5450
if (n_args != n_args_min) {
5551
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
56-
terse_arg_mismatch();
52+
mp_arg_error_terse_mismatch();
5753
} else {
5854
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
5955
"function takes %d positional arguments but %d were given",
@@ -63,15 +59,15 @@ void mp_arg_check_num(mp_uint_t n_args, mp_uint_t n_kw, mp_uint_t n_args_min, mp
6359
} else {
6460
if (n_args < n_args_min) {
6561
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
66-
terse_arg_mismatch();
62+
mp_arg_error_terse_mismatch();
6763
} else {
6864
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
6965
"function missing %d required positional arguments",
7066
n_args_min - n_args));
7167
}
7268
} else if (n_args > n_args_max) {
7369
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
74-
terse_arg_mismatch();
70+
mp_arg_error_terse_mismatch();
7571
} else {
7672
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
7773
"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
9692
if (kw == NULL) {
9793
if (allowed[i].flags & MP_ARG_REQUIRED) {
9894
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
99-
terse_arg_mismatch();
95+
mp_arg_error_terse_mismatch();
10096
} else {
10197
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
10298
"'%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
123119
if (pos_found < n_pos) {
124120
extra_positional:
125121
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
126-
terse_arg_mismatch();
122+
mp_arg_error_terse_mismatch();
127123
} else {
128124
// TODO better error message
129125
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
132128
}
133129
if (kws_found < kws->used) {
134130
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
135-
terse_arg_mismatch();
131+
mp_arg_error_terse_mismatch();
136132
} else {
137133
// TODO better error message
138134
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 *
147143
mp_arg_parse_all(n_pos, args, &kw_args, n_allowed, allowed, out_vals);
148144
}
149145

146+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
147+
NORETURN void mp_arg_error_terse_mismatch(void) {
148+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "argument num/types mismatch"));
149+
}
150+
#endif
151+
150152
#if MICROPY_CPYTHON_COMPAT
151153
NORETURN void mp_arg_error_unimpl_kw(void) {
152154
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,

py/bc.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ mp_uint_t mp_decode_uint(const byte **ptr) {
6262

6363
STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, mp_uint_t expected, mp_uint_t given) {
6464
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
65-
// Generic message, to be reused for other argument issues
66-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
67-
"argument num/types mismatch"));
65+
// generic message, used also for other argument issues
66+
mp_arg_error_terse_mismatch();
6867
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
6968
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
7069
"function takes %d positional arguments but %d were given", expected, given));

py/objnamedtuple.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "qstr.h"
3434
#include "obj.h"
3535
#include "objtuple.h"
36+
#include "runtime.h"
3637

3738
#if MICROPY_PY_COLLECTIONS
3839

@@ -86,10 +87,14 @@ STATIC mp_obj_t namedtuple_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
8687
mp_obj_namedtuple_type_t *type = type_in;
8788
mp_uint_t num_fields = type->n_fields;
8889
if (n_args + n_kw != num_fields) {
89-
// Counts include implicit "self"
90-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
91-
"__new__() takes %d positional arguments but %d were given",
92-
num_fields + 1, n_args + n_kw + 1));
90+
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
91+
mp_arg_error_terse_mismatch();
92+
} else {
93+
// Counts include implicit "self"
94+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
95+
"__new__() takes %d positional arguments but %d were given",
96+
num_fields + 1, n_args + n_kw + 1));
97+
}
9398
}
9499

95100
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_
108113
qstr kw = MP_OBJ_QSTR_VALUE(args[i]);
109114
int id = namedtuple_find_field(type, kw);
110115
if (id == -1) {
111-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
112-
"__new__() got an unexpected keyword argument '%s'",
113-
qstr_str(kw)));
116+
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
117+
mp_arg_error_terse_mismatch();
118+
} else {
119+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
120+
"__new__() got an unexpected keyword argument '%s'",
121+
qstr_str(kw)));
122+
}
114123
}
115124
if (arg_objects[id] != NULL) {
116-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
117-
"__new__() got multiple values for argument '%s'",
118-
qstr_str(kw)));
125+
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
126+
mp_arg_error_terse_mismatch();
127+
} else {
128+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
129+
"__new__() got multiple values for argument '%s'",
130+
qstr_str(kw)));
131+
}
119132
}
120133
arg_objects[id] = args[i + 1];
121134
}

py/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ void mp_deinit(void);
6161
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);
6262
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);
6363
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);
64+
NORETURN void mp_arg_error_terse_mismatch(void);
6465
NORETURN void mp_arg_error_unimpl_kw(void);
6566

6667
mp_obj_dict_t *mp_locals_get(void);

0 commit comments

Comments
 (0)
0