diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 3ea9cee529..422f057a85 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -28,6 +28,7 @@ #if MICROPY_VFS && MICROPY_VFS_FAT #include +#include #include "py/runtime.h" #include "py/stream.h" @@ -199,7 +200,7 @@ STATIC mp_obj_t file_open(fs_user_mount_t *vfs, const mp_obj_type_t *type, mp_ar FRESULT res = f_open(&vfs->fatfs, &o->fp, fname, mode); if (res != FR_OK) { m_del_obj(pyb_file_obj_t, o); - mp_raise_OSError(fresult_to_errno_table[res]); + mp_raise_OSError_errno_str(fresult_to_errno_table[res], args[0].u_obj); } // If we're reading, turn on fast seek. if (mode == FA_READ) { diff --git a/py/objexcept.c b/py/objexcept.c index c33ada0d69..b7a536c5e3 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -112,17 +112,23 @@ 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) { mp_print_str(print, ""); return; - } else if (o->args->len == 1) { + } + 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)) && + o->args->len <= 2) { // try to provide a nice OSError error message - 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))) { - char decompressed[50]; - const char *msg = mp_common_errno_to_str(o->args->items[0], decompressed, sizeof(decompressed)); - if (msg != NULL) { - mp_printf(print, "[Errno " INT_FMT "] %s", MP_OBJ_SMALL_INT_VALUE(o->args->items[0]), msg); - return; + char decompressed[50]; + const char *msg = mp_common_errno_to_str(o->args->items[0], decompressed, sizeof(decompressed)); + if (msg != NULL) { + mp_printf(print, "[Errno " INT_FMT "] %s", MP_OBJ_SMALL_INT_VALUE(o->args->items[0]), msg); + // if second arg exists, it is filename. + if (o->args->len == 2) { + mp_printf(print, ": '%s'", mp_obj_str_get_str(o->args->items[1])); } + return; } + } + if (o->args->len == 1) { mp_obj_print_helper(print, o->args->items[0], PRINT_STR); return; } diff --git a/py/runtime.c b/py/runtime.c index 48416bab26..c1c311ae4c 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -29,10 +29,12 @@ #include #include + #include "extmod/vfs.h" #include "py/parsenum.h" #include "py/compile.h" +#include "py/mperrno.h" #include "py/objstr.h" #include "py/objtuple.h" #include "py/objtype.h" @@ -1576,6 +1578,14 @@ NORETURN void mp_raise_OSError_msg(const compressed_string_t *msg) { mp_raise_msg(&mp_type_OSError, msg); } +NORETURN void mp_raise_OSError_errno_str(int errno_, mp_obj_t str) { + mp_obj_t args[2] = { + MP_OBJ_NEW_SMALL_INT(errno_), + str, + }; + nlr_raise(mp_obj_new_exception_args(&mp_type_OSError, 2, args)); +} + NORETURN void mp_raise_OSError_msg_varg(const compressed_string_t *fmt, ...) { va_list argptr; va_start(argptr,fmt); diff --git a/py/runtime.h b/py/runtime.h index 4121352dfd..f811035571 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -161,6 +161,7 @@ NORETURN void mp_raise_RuntimeError(const compressed_string_t *msg); NORETURN void mp_raise_ImportError(const compressed_string_t *msg); NORETURN void mp_raise_IndexError(const compressed_string_t *msg); NORETURN void mp_raise_OSError(int errno_); +NORETURN void mp_raise_OSError_errno_str(int errno_, mp_obj_t str); NORETURN void mp_raise_OSError_msg(const compressed_string_t *msg); NORETURN void mp_raise_OSError_msg_varg(const compressed_string_t *fmt, ...); NORETURN void mp_raise_NotImplementedError(const compressed_string_t *msg);