Merge pull request #2584 from dhalbert/oserror-with-filename

Include filename for 'No such file/directory', etc.
This commit is contained in:
Dan Halbert 2020-02-04 18:41:17 -05:00 committed by GitHub
commit 433671f5eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 9 deletions

View File

@ -28,6 +28,7 @@
#if MICROPY_VFS && MICROPY_VFS_FAT #if MICROPY_VFS && MICROPY_VFS_FAT
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "py/runtime.h" #include "py/runtime.h"
#include "py/stream.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); FRESULT res = f_open(&vfs->fatfs, &o->fp, fname, mode);
if (res != FR_OK) { if (res != FR_OK) {
m_del_obj(pyb_file_obj_t, o); 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 we're reading, turn on fast seek.
if (mode == FA_READ) { if (mode == FA_READ) {

View File

@ -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) { if (o->args == NULL || o->args->len == 0) {
mp_print_str(print, ""); mp_print_str(print, "");
return; 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 // try to provide a nice OSError error message
if (MP_OBJ_IS_SMALL_INT(o->args->items[0]) && char decompressed[50];
mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(o->base.type), MP_OBJ_FROM_PTR(&mp_type_OSError))) { 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 second arg exists, it is filename.
mp_printf(print, "[Errno " INT_FMT "] %s", MP_OBJ_SMALL_INT_VALUE(o->args->items[0]), msg); if (o->args->len == 2) {
return; 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); mp_obj_print_helper(print, o->args->items[0], PRINT_STR);
return; return;
} }

View File

@ -29,10 +29,12 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "extmod/vfs.h" #include "extmod/vfs.h"
#include "py/parsenum.h" #include "py/parsenum.h"
#include "py/compile.h" #include "py/compile.h"
#include "py/mperrno.h"
#include "py/objstr.h" #include "py/objstr.h"
#include "py/objtuple.h" #include "py/objtuple.h"
#include "py/objtype.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); 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, ...) { NORETURN void mp_raise_OSError_msg_varg(const compressed_string_t *fmt, ...) {
va_list argptr; va_list argptr;
va_start(argptr,fmt); va_start(argptr,fmt);

View File

@ -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_ImportError(const compressed_string_t *msg);
NORETURN void mp_raise_IndexError(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(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(const compressed_string_t *msg);
NORETURN void mp_raise_OSError_msg_varg(const compressed_string_t *fmt, ...); NORETURN void mp_raise_OSError_msg_varg(const compressed_string_t *fmt, ...);
NORETURN void mp_raise_NotImplementedError(const compressed_string_t *msg); NORETURN void mp_raise_NotImplementedError(const compressed_string_t *msg);