summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2018-05-15 10:12:09 -0400
committerGitHub <noreply@github.com>2018-05-15 10:12:09 -0400
commit54293397c5736e7e571a933c92dccf375d529b2b (patch)
treec6412a30152b775a2e8454b2032a51bf07a3d5b8
parentd655e2194c8cc5d17a8e86c78104912228f09550 (diff)
parent086bffb5940eb0450b4213934c6c1ed12d124e7c (diff)
Merge pull request #837 from godlygeek/human_readable_oserror
Human readable OSError messages
-rw-r--r--ports/atmel-samd/mpconfigport.h2
-rw-r--r--py/moduerrno.c14
-rw-r--r--tests/basics/errno1.py5
-rw-r--r--tests/basics/errno1.py.exp3
4 files changed, 22 insertions, 2 deletions
diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h
index e6c3a201f..27c248001 100644
--- a/ports/atmel-samd/mpconfigport.h
+++ b/ports/atmel-samd/mpconfigport.h
@@ -188,6 +188,8 @@ extern const struct _mp_obj_module_t usb_hid_module;
#define MICROPY_PY_BUILTINS_FROZENSET (1)
#define MICROPY_PY_BUILTINS_STR_SPLITLINES (1)
#define MICROPY_PY_BUILTINS_REVERSED (1)
+ #define MICROPY_PY_UERRNO (1)
+ #define MICROPY_PY_UERRNO_ERRORCODE (0)
#define MICROPY_PY_URE (1)
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
#define MICROPY_PY_FRAMEBUF (1)
diff --git a/py/moduerrno.c b/py/moduerrno.c
index de66c941b..ce8a6bf89 100644
--- a/py/moduerrno.c
+++ b/py/moduerrno.c
@@ -100,6 +100,20 @@ const mp_obj_module_t mp_module_uerrno = {
};
qstr mp_errno_to_str(mp_obj_t errno_val) {
+ // For commonly encountered errors, return human readable strings
+ if (MP_OBJ_IS_SMALL_INT(errno_val)) {
+ switch (MP_OBJ_SMALL_INT_VALUE(errno_val)) {
+ case EPERM: return MP_QSTR_Permission_space_denied;
+ case ENOENT: return MP_QSTR_No_space_such_space_file_slash_directory;
+ case EIO: return MP_QSTR_Input_slash_output_space_error;
+ case EACCES: return MP_QSTR_Permission_space_denied;
+ case EEXIST: return MP_QSTR_File_space_exists;
+ case ENODEV: return MP_QSTR_Unsupported_space_operation;
+ case EINVAL: return MP_QSTR_Invalid_space_argument;
+ }
+ }
+
+ // Otherwise, return the Exxxx string for that error code
#if MICROPY_PY_UERRNO_ERRORCODE
// We have the errorcode dict so can do a lookup using the hash map
mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&errorcode_dict.map, errno_val, MP_MAP_LOOKUP);
diff --git a/tests/basics/errno1.py b/tests/basics/errno1.py
index 63930b767..bbbfac10c 100644
--- a/tests/basics/errno1.py
+++ b/tests/basics/errno1.py
@@ -11,7 +11,10 @@ print(type(uerrno.EIO))
# check that errors are rendered in a nice way
msg = str(OSError(uerrno.EIO))
-print(msg[:7], msg[-5:])
+print(msg[:7], msg[msg.find(']'):])
+
+msg = str(OSError(uerrno.ENOBUFS))
+print(msg[:7], msg[msg.find(']'):])
# check that unknown errno is still rendered
print(str(OSError(9999)))
diff --git a/tests/basics/errno1.py.exp b/tests/basics/errno1.py.exp
index c3703df4a..b5dd529e4 100644
--- a/tests/basics/errno1.py.exp
+++ b/tests/basics/errno1.py.exp
@@ -1,3 +1,4 @@
<class 'int'>
-[Errno ] EIO
+[Errno ] Input/output error
+[Errno ] ENOBUFS
9999