From 6d9c1b694cfa4ef9972300d28fc55f798d18b8c8 Mon Sep 17 00:00:00 2001 From: Rahix <rahix@rahix.de> Date: Tue, 15 Aug 2023 17:24:21 +0200 Subject: [PATCH] uctx: Raise exception for unknown fonts Don't return None when a font is not found, but instead raise a ValueError. This is more pythonic along the lines of - Errors should never pass silently. - Ask forgiveness, not permission. --- components/micropython/usermodule/mp_uctx.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/components/micropython/usermodule/mp_uctx.c b/components/micropython/usermodule/mp_uctx.c index c668bf2a52..17d9942a7c 100644 --- a/components/micropython/usermodule/mp_uctx.c +++ b/components/micropython/usermodule/mp_uctx.c @@ -430,8 +430,10 @@ static mp_obj_t mp_ctx_get_font_name(mp_obj_t self_in, mp_obj_t no_in) { mp_ctx_obj_t *self = MP_OBJ_TO_PTR(self_in); int no = mp_obj_get_int(no_in); const char *name = ctx_get_font_name(self->ctx, no); - if (name) return mp_obj_new_str(name, strlen(name)); - return mp_const_none; + if (name) + return mp_obj_new_str(name, strlen(name)); + else + mp_raise_ValueError("font with given index does not exist"); } MP_DEFINE_CONST_FUN_OBJ_2(mp_ctx_get_font_name_obj, mp_ctx_get_font_name); -- GitLab