Skip to content

Include filename for 'No such file/directory', etc. #2584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion extmod/vfs_fat_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#if MICROPY_VFS && MICROPY_VFS_FAT

#include <stdio.h>
#include <string.h>

#include "py/runtime.h"
#include "py/stream.h"
Expand Down Expand Up @@ -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) {
Expand Down
22 changes: 14 additions & 8 deletions py/objexcept.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions py/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
#include <string.h>
#include <assert.h>


#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"
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions py/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy