Skip to content

Commit 22d0598

Browse files
committed
objfun: Factor out function to report positional args mismatch.
1 parent e908591 commit 22d0598

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

py/objfun.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ STATIC void dump_args(const mp_obj_t *a, int sz) {
138138
#define dump_args(...) (void)0
139139
#endif
140140

141+
STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, uint expected, uint given) {
142+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
143+
"function takes %d positional arguments but %d were given", expected, given));
144+
}
145+
141146
// If it's possible to call a function without allocating new argument array,
142147
// this function returns true, together with pointers to 2 subarrays to be used
143148
// as arguments. Otherwise, it returns false. It is expected that this fucntion
@@ -172,7 +177,7 @@ bool mp_obj_fun_prepare_simple_args(mp_obj_t self_in, uint n_args, uint n_kw, co
172177
return true;
173178

174179
arg_error:
175-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "function takes %d positional arguments but %d were given", self->n_pos_args, n_args));
180+
fun_pos_args_mismatch(self, self->n_pos_args, n_args);
176181
}
177182

178183
STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
@@ -198,8 +203,7 @@ STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_o
198203
if (n_args > self->n_pos_args) {
199204
// given more than enough arguments
200205
if (!self->takes_var_args) {
201-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
202-
"function takes %d positional arguments but %d were given", self->n_pos_args, n_args));
206+
fun_pos_args_mismatch(self, self->n_pos_args, n_args);
203207
}
204208
// put extra arguments in varargs tuple
205209
*extra_args = mp_obj_new_tuple(n_args - self->n_pos_args, args + self->n_pos_args);
@@ -219,9 +223,7 @@ STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_o
219223
extra_args -= self->n_pos_args - n_args;
220224
n_extra_args += self->n_pos_args - n_args;
221225
} else {
222-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
223-
"function takes at least %d positional arguments but %d were given",
224-
self->n_pos_args - self->n_def_args, n_args));
226+
fun_pos_args_mismatch(self, self->n_pos_args - self->n_def_args, n_args);
225227
}
226228
}
227229
}

0 commit comments

Comments
 (0)