Skip to content

Commit 433671f

Browse files
authored
Merge pull request adafruit#2584 from dhalbert/oserror-with-filename
Include filename for 'No such file/directory', etc.
2 parents 65045eb + 280e20a commit 433671f

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

extmod/vfs_fat_file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#if MICROPY_VFS && MICROPY_VFS_FAT
2929

3030
#include <stdio.h>
31+
#include <string.h>
3132

3233
#include "py/runtime.h"
3334
#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
199200
FRESULT res = f_open(&vfs->fatfs, &o->fp, fname, mode);
200201
if (res != FR_OK) {
201202
m_del_obj(pyb_file_obj_t, o);
202-
mp_raise_OSError(fresult_to_errno_table[res]);
203+
mp_raise_OSError_errno_str(fresult_to_errno_table[res], args[0].u_obj);
203204
}
204205
// If we're reading, turn on fast seek.
205206
if (mode == FA_READ) {

py/objexcept.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,23 @@ void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kin
112112
if (o->args == NULL || o->args->len == 0) {
113113
mp_print_str(print, "");
114114
return;
115-
} else if (o->args->len == 1) {
115+
}
116+
if (MP_OBJ_IS_SMALL_INT(o->args->items[0]) &&
117+
mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(o->base.type), MP_OBJ_FROM_PTR(&mp_type_OSError)) &&
118+
o->args->len <= 2) {
116119
// try to provide a nice OSError error message
117-
if (MP_OBJ_IS_SMALL_INT(o->args->items[0]) &&
118-
mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(o->base.type), MP_OBJ_FROM_PTR(&mp_type_OSError))) {
119-
char decompressed[50];
120-
const char *msg = mp_common_errno_to_str(o->args->items[0], decompressed, sizeof(decompressed));
121-
if (msg != NULL) {
122-
mp_printf(print, "[Errno " INT_FMT "] %s", MP_OBJ_SMALL_INT_VALUE(o->args->items[0]), msg);
123-
return;
120+
char decompressed[50];
121+
const char *msg = mp_common_errno_to_str(o->args->items[0], decompressed, sizeof(decompressed));
122+
if (msg != NULL) {
123+
mp_printf(print, "[Errno " INT_FMT "] %s", MP_OBJ_SMALL_INT_VALUE(o->args->items[0]), msg);
124+
// if second arg exists, it is filename.
125+
if (o->args->len == 2) {
126+
mp_printf(print, ": '%s'", mp_obj_str_get_str(o->args->items[1]));
124127
}
128+
return;
125129
}
130+
}
131+
if (o->args->len == 1) {
126132
mp_obj_print_helper(print, o->args->items[0], PRINT_STR);
127133
return;
128134
}

py/runtime.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
#include <string.h>
3030
#include <assert.h>
3131

32+
3233
#include "extmod/vfs.h"
3334

3435
#include "py/parsenum.h"
3536
#include "py/compile.h"
37+
#include "py/mperrno.h"
3638
#include "py/objstr.h"
3739
#include "py/objtuple.h"
3840
#include "py/objtype.h"
@@ -1576,6 +1578,14 @@ NORETURN void mp_raise_OSError_msg(const compressed_string_t *msg) {
15761578
mp_raise_msg(&mp_type_OSError, msg);
15771579
}
15781580

1581+
NORETURN void mp_raise_OSError_errno_str(int errno_, mp_obj_t str) {
1582+
mp_obj_t args[2] = {
1583+
MP_OBJ_NEW_SMALL_INT(errno_),
1584+
str,
1585+
};
1586+
nlr_raise(mp_obj_new_exception_args(&mp_type_OSError, 2, args));
1587+
}
1588+
15791589
NORETURN void mp_raise_OSError_msg_varg(const compressed_string_t *fmt, ...) {
15801590
va_list argptr;
15811591
va_start(argptr,fmt);

py/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ NORETURN void mp_raise_RuntimeError(const compressed_string_t *msg);
161161
NORETURN void mp_raise_ImportError(const compressed_string_t *msg);
162162
NORETURN void mp_raise_IndexError(const compressed_string_t *msg);
163163
NORETURN void mp_raise_OSError(int errno_);
164+
NORETURN void mp_raise_OSError_errno_str(int errno_, mp_obj_t str);
164165
NORETURN void mp_raise_OSError_msg(const compressed_string_t *msg);
165166
NORETURN void mp_raise_OSError_msg_varg(const compressed_string_t *fmt, ...);
166167
NORETURN void mp_raise_NotImplementedError(const compressed_string_t *msg);

0 commit comments

Comments
 (0)
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