8000 Merge pull request #837 from godlygeek/human_readable_oserror · adafruit/circuitpython@5429339 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5429339

Browse files
authored
Merge pull request #837 from godlygeek/human_readable_oserror
Human readable OSError messages
2 parents d655e21 + 086bffb commit 5429339

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

ports/atmel-samd/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ extern const struct _mp_obj_module_t usb_hid_module;
188188
#define MICROPY_PY_BUILTINS_FROZENSET (1)
189189
#define MICROPY_PY_BUILTINS_STR_SPLITLINES (1)
190190
#define MICROPY_PY_BUILTINS_REVERSED (1)
191+
#define MICROPY_PY_UERRNO (1)
192+
#define MICROPY_PY_UERRNO_ERRORCODE (0)
191193
#define MICROPY_PY_URE (1)
192194
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
193195
#define MICROPY_PY_FRAMEBUF (1)

py/moduerrno.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ const mp_obj_module_t mp_module_uerrno = {
100100
};
101101

102102
qstr mp_errno_to_str(mp_obj_t errno_val) {
103+
// For commonly encountered errors, return human readable strings
104+
if (MP_OBJ_IS_SMALL_INT(errno_val)) {
105+
switch (MP_OBJ_SMALL_INT_VALUE(errno_val)) {
106+
case EPERM: return MP_QSTR_Permission_space_denied;
107+
case ENOENT: return MP_QSTR_No_space_such_space_file_slash_directory;
108+
case EIO: return MP_QSTR_Input_slash_output_space_error;
109+
case EACCES: return MP_QSTR_Permission_space_denied;
110+
case EEXIST: return MP_QSTR_File_space_exists;
111+
case ENODEV: return MP_QSTR_Unsupported_space_operation;
112+
case EINVAL: return MP_QSTR_Invalid_space_argument;
113+
}
114+
}
115+
116+
// Otherwise, return the Exxxx string for that error code
103117
#if MICROPY_PY_UERRNO_ERRORCODE
104118
// We have the errorcode dict so can do a lookup using the hash map
105119
mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&errorcode_dict.map, errno_val, MP_MAP_LOOKUP);

tests/basics/errno1.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
# check that errors are rendered in a nice way
1313
msg = str(OSError(uerrno.EIO))
14-
print(msg[:7], msg[-5:])
14+
print(msg[:7], msg[msg.find(']'):])
15+
16+
msg = str(OSError(uerrno.ENOBUFS))
17+
print(msg[:7], msg[msg.find(']'):])
1518

1619
# check that unknown errno is still rendered
1720
print(str(OSError(9999)))

tests/basics/errno1.py.exp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<class 'int'>
2-
[Errno ] EIO
2+
[Errno ] Input/output error
3+
[Errno ] ENOBUFS
34
9999

0 commit comments

Comments
 (0)
0