diff options
author | Damien George <damien.p.george@gmail.com> | 2016-05-12 13:20:40 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2016-05-12 13:20:40 +0100 |
commit | d45e5f8c357d13e66aeac644e2b5ead8c734025f (patch) | |
tree | c6d9ab833d5a9b528e1b3cb05c47a31b2331a4c2 /py/objexcept.c | |
parent | 47bf6ba61a2f332b44471badd1e50658ae7d264c (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/objexcept.c')
-rw-r--r-- | py/objexcept.c | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/py/objexcept.c b/py/objexcept.c index adf17b08d..4c1da1b38 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -36,6 +36,7 @@ #include "py/objtype.h" #include "py/runtime.h" #include "py/gc.h" +#include "py/mperrno.h" // Instance of MemoryError exception - needed by mp_malloc_fail const mp_obj_exception_t mp_const_MemoryError_obj = {{&mp_type_MemoryError}, 0, 0, NULL, (mp_obj_tuple_t*)&mp_const_empty_tuple_obj}; @@ -288,6 +289,10 @@ mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type) { // "Optimized" version for common(?) case of having 1 exception arg mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg) { + // try to provide a nice string instead of numeric value for errno's + if (exc_type == &mp_type_OSError && MP_OBJ_IS_SMALL_INT(arg)) { + arg = mp_errno_to_str(arg); + } return mp_obj_new_exception_args(exc_type, 1, &arg); } |