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.
This commit is contained in:
Damien George
2016-05-12 13:20:40 +01:00
parent 47bf6ba61a
commit d45e5f8c35
3 changed files with 20 additions and 0 deletions

View File

@@ -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);
}