summaryrefslogtreecommitdiff
path: root/py/moduerrno.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-05-12 13:20:40 +0100
committerDamien George <damien.p.george@gmail.com>2016-05-12 13:20:40 +0100
commitd45e5f8c357d13e66aeac644e2b5ead8c734025f (patch)
treec6d9ab833d5a9b528e1b3cb05c47a31b2331a4c2 /py/moduerrno.c
parent47bf6ba61a2f332b44471badd1e50658ae7d264c (diff)
py: Add mp_errno_to_str() and use it to provide nicer OSError msgs.
If an OSError is raised with an integer argument, and that integer corresponds to an errno, then the string for the errno is used as the argument to the exception, instead of the integer. Only works if the uerrno module is enabled.
Diffstat (limited to 'py/moduerrno.c')
-rw-r--r--py/moduerrno.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/py/moduerrno.c b/py/moduerrno.c
index 826f52d2e..0b543a193 100644
--- a/py/moduerrno.c
+++ b/py/moduerrno.c
@@ -89,4 +89,13 @@ const mp_obj_module_t mp_module_uerrno = {
.globals = (mp_obj_dict_t*)&mp_module_uerrno_globals,
};
+mp_obj_t mp_errno_to_str(mp_obj_t errno_val) {
+ mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&errorcode_dict.map, errno_val, MP_MAP_LOOKUP);
+ if (elem == NULL) {
+ return errno_val;
+ } else {
+ return elem->value;
+ }
+}
+
#endif //MICROPY_PY_UERRNO