diff options
Diffstat (limited to 'py/moduerrno.c')
-rw-r--r-- | py/moduerrno.c | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/py/moduerrno.c b/py/moduerrno.c index 4a5e87419f..de66c941b0 100644 --- a/py/moduerrno.c +++ b/py/moduerrno.c @@ -32,9 +32,10 @@ #if MICROPY_PY_UERRNO -// This list could be defined per port in mpconfigport.h to tailor it to a -// specific port's needs. But for now we have a common list. -#define ERRNO_LIST \ +// This list can be defined per port in mpconfigport.h to tailor it to a +// specific port's needs. If it's not defined then we provide a default. +#ifndef MICROPY_PY_UERRNO_LIST +#define MICROPY_PY_UERRNO_LIST \ X(EPERM) \ X(ENOENT) \ X(EIO) \ @@ -58,9 +59,12 @@ X(EALREADY) \ X(EINPROGRESS) \ +#endif + +#if MICROPY_PY_UERRNO_ERRORCODE STATIC const mp_rom_map_elem_t errorcode_table[] = { #define X(e) { MP_ROM_INT(MP_ ## e), MP_ROM_QSTR(MP_QSTR_## e) }, - ERRNO_LIST + MICROPY_PY_UERRNO_LIST #undef X }; @@ -75,13 +79,16 @@ STATIC const mp_obj_dict_t errorcode_dict = { .table = (mp_map_elem_t*)(mp_rom_map_elem_t*)errorcode_table, }, }; +#endif STATIC const mp_rom_map_elem_t mp_module_uerrno_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uerrno) }, + #if MICROPY_PY_UERRNO_ERRORCODE { MP_ROM_QSTR(MP_QSTR_errorcode), MP_ROM_PTR(&errorcode_dict) }, + #endif #define X(e) { MP_ROM_QSTR(MP_QSTR_## e), MP_ROM_INT(MP_ ## e) }, - ERRNO_LIST + MICROPY_PY_UERRNO_LIST #undef X }; @@ -93,12 +100,23 @@ const mp_obj_module_t mp_module_uerrno = { }; qstr mp_errno_to_str(mp_obj_t errno_val) { + #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); if (elem == NULL) { return MP_QSTR_NULL; } else { return MP_OBJ_QSTR_VALUE(elem->value); } + #else + // We don't have the errorcode dict so do a simple search in the modules dict + for (size_t i = 0; i < MP_ARRAY_SIZE(mp_module_uerrno_globals_table); ++i) { + if (errno_val == mp_module_uerrno_globals_table[i].value) { + return MP_OBJ_QSTR_VALUE(mp_module_uerrno_globals_table[i].key); + } + } + return MP_QSTR_NULL; + #endif } #endif //MICROPY_PY_UERRNO |