fix multi-arg exception printing

This commit is contained in:
Dan Halbert 2020-02-04 16:41:25 -05:00
parent 5164719e67
commit 7fe959ab39

View File

@ -112,20 +112,22 @@ void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kin
if (o->args == NULL || o->args->len == 0) { if (o->args == NULL || o->args->len == 0) {
mp_print_str(print, ""); mp_print_str(print, "");
return; return;
} else if (o->args->len <= 2) { }
// try to provide a nice OSError error message; second arg might exist (e.g. filename). if (MP_OBJ_IS_SMALL_INT(o->args->items[0]) &&
if (MP_OBJ_IS_SMALL_INT(o->args->items[0]) && mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(o->base.type), MP_OBJ_FROM_PTR(&mp_type_OSError)) &&
mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(o->base.type), MP_OBJ_FROM_PTR(&mp_type_OSError))) { o->args->len <= 2) {
char decompressed[50]; // try to provide a nice OSError error message
const char *msg = mp_common_errno_to_str(o->args->items[0], decompressed, sizeof(decompressed)); char decompressed[50];
if (msg != NULL) { const char *msg = mp_common_errno_to_str(o->args->items[0], decompressed, sizeof(decompressed));
mp_printf(print, "[Errno " INT_FMT "] %s", MP_OBJ_SMALL_INT_VALUE(o->args->items[0]), msg); if (msg != NULL) {
if (o->args->len == 2) { mp_printf(print, "[Errno " INT_FMT "] %s", MP_OBJ_SMALL_INT_VALUE(o->args->items[0]), msg);
mp_printf(print, ": '%s'", mp_obj_str_get_str(o->args->items[1])); // if second arg exists, it is filename.
} if (o->args->len == 2) {
return; mp_printf(print, ": '%s'", mp_obj_str_get_str(o->args->items[1]));
} }
return;
} }
} else if (o->args->len == 1) {
mp_obj_print_helper(print, o->args->items[0], PRINT_STR); mp_obj_print_helper(print, o->args->items[0], PRINT_STR);
return; return;
} }