Skip to content

Commit 84e0cf0

Browse files
committed
py: Change namedtuple error messages to reduce code size.
We are not word-for-word compatible with CPython exceptions, so we are free to make them short but informative in order to reduce code size. Also, try to make messages the same as existing ones where possible.
1 parent 7f23384 commit 84e0cf0

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

py/objnamedtuple.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,14 @@ STATIC mp_obj_t namedtuple_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
8989
if (n_args + n_kw != num_fields) {
9090
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
9191
mp_arg_error_terse_mismatch();
92-
} else {
93-
// Counts include implicit "self"
92+
} else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
9493
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));
94+
"function takes %d positional arguments but %d were given",
95+
num_fields, n_args + n_kw));
96+
} else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED) {
97+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
98+
"%s() takes %d positional arguments but %d were given",
99+
qstr_str(type->base.name), num_fields, n_args + n_kw));
97100
}
98101
}
99102

@@ -117,7 +120,7 @@ STATIC mp_obj_t namedtuple_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
117120
mp_arg_error_terse_mismatch();
118121
} else {
119122
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
120-
"__new__() got an unexpected keyword argument '%s'",
123+
"unexpected keyword argument '%s'",
121124
qstr_str(kw)));
122125
}
123126
}
@@ -126,7 +129,7 @@ STATIC mp_obj_t namedtuple_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
126129
mp_arg_error_terse_mismatch();
127130
} else {
128131
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
129-
"__new__() got multiple values for argument '%s'",
132+
"function got multiple values for argument '%s'",
130133
qstr_str(kw)));
131134
}
132135
}

0 commit comments

Comments
 (0)