Skip to content

Commit 58f23de

Browse files
committed
py/bc: Provide better error message for an unexpected keyword argument.
Now, passing a keyword argument that is not expected will correctly report that fact. If normal or detailed error messages are enabled then the name of the unexpected argument will be reported. This patch decreases the code size of bare-arm and stmhal by 12 bytes, and cc3200 by 8 bytes. Other ports (minimal, unix, esp8266) remain the same in code size. For terse error message configuration this is because the new message is shorter than the old one. For normal (and detailed) error message configuration this is because the new error message already exists in py/objnamedtuple.c so there's no extra space in ROM needed for the string.
1 parent 1110c88 commit 58f23de

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

py/bc.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,12 @@ void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw
196196
}
197197
// Didn't find name match with positional args
198198
if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) == 0) {
199-
mp_raise_msg(&mp_type_TypeError, "function does not take keyword arguments");
199+
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
200+
mp_raise_msg(&mp_type_TypeError, "unexpected keyword argument");
201+
} else {
202+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
203+
"unexpected keyword argument '%q'", MP_OBJ_QSTR_VALUE(wanted_arg_name)));
204+
}
200205
}
201206
mp_obj_dict_store(dict, kwargs[2 * i], kwargs[2 * i + 1]);
202207
continue2:;

0 commit comments

Comments
 (0)