Skip to content

Commit c6ee273

Browse files
pohmeliepfalcon
authored andcommitted
py: Add min/max "default" keyword argument
1 parent 354e688 commit c6ee273

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

py/modbuiltins.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter);
254254

255255
STATIC mp_obj_t mp_builtin_min_max(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs, mp_uint_t op) {
256256
mp_map_elem_t *key_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_key), MP_MAP_LOOKUP);
257+
mp_map_elem_t *default_elem;
257258
mp_obj_t key_fn = key_elem == NULL ? MP_OBJ_NULL : key_elem->value;
258259
if (n_args == 1) {
259260
// given an iterable
@@ -269,7 +270,12 @@ STATIC mp_obj_t mp_builtin_min_max(mp_uint_t n_args, const mp_obj_t *args, mp_ma
269270
}
270271
}
271272
if (best_obj == MP_OBJ_NULL) {
272-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "arg is an empty sequence"));
273+
default_elem = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_default), MP_MAP_LOOKUP);
274+
if (default_elem != NULL) {
275+
best_obj = default_elem->value;
276+
} else {
277+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "arg is an empty sequence"));
278+
}
273279
}
274280
return best_obj;
275281
} else {

py/qstrdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ Q(map)
235235
#if MICROPY_PY_BUILTINS_MIN_MAX
236236
Q(max)
237237
Q(min)
238+
Q(default)
238239
#endif
239240
Q(namedtuple)
240241
Q(next)

0 commit comments

Comments
 (0)