From 58f23def55a705357b352ce8af642e03cd5c49f0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 22 Mar 2017 13:40:27 +1100 Subject: [PATCH] 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. --- py/bc.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/py/bc.c b/py/bc.c index e7a1a333f7..12887645b9 100644 --- a/py/bc.c +++ b/py/bc.c @@ -196,7 +196,12 @@ void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw } // Didn't find name match with positional args if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) == 0) { - mp_raise_msg(&mp_type_TypeError, "function does not take keyword arguments"); + if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { + mp_raise_msg(&mp_type_TypeError, "unexpected keyword argument"); + } else { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, + "unexpected keyword argument '%q'", MP_OBJ_QSTR_VALUE(wanted_arg_name))); + } } mp_obj_dict_store(dict, kwargs[2 * i], kwargs[2 * i + 1]); continue2:;