diff options
author | Damien George <damien.p.george@gmail.com> | 2017-02-22 12:53:42 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-02-22 12:58:11 +1100 |
commit | f563406d2e342be1535592f80680c510f9aade9a (patch) | |
tree | c9157ff8964d41a501e0dd151cf41de5c571db43 /py | |
parent | 22a6344ebe032e9e66024614ecbe7d9608fa9288 (diff) |
py/moduerrno: Make uerrno.errorcode dict configurable.
It's configured by MICROPY_PY_UERRNO_ERRORCODE and enabled by default
(since that's the behaviour before this patch).
Without this dict the lookup of errno codes to strings must use the
uerrno module itself.
Diffstat (limited to 'py')
-rw-r--r-- | py/moduerrno.c | 15 | ||||
-rw-r--r-- | py/mpconfig.h | 5 |
2 files changed, 20 insertions, 0 deletions
diff --git a/py/moduerrno.c b/py/moduerrno.c index 4a5e87419..ad166ced3 100644 --- a/py/moduerrno.c +++ b/py/moduerrno.c @@ -58,6 +58,7 @@ X(EALREADY) \ X(EINPROGRESS) \ +#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 @@ -75,10 +76,13 @@ 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 @@ -93,12 +97,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 diff --git a/py/mpconfig.h b/py/mpconfig.h index 1b47d822f..7e3bd8bdb 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -917,6 +917,11 @@ typedef double mp_float_t; #define MICROPY_PY_UERRNO (0) #endif +// Whether to provide the uerrno.errorcode dict +#ifndef MICROPY_PY_UERRNO_ERRORCODE +#define MICROPY_PY_UERRNO_ERRORCODE (1) +#endif + // Whether to provide "uselect" module (baremetal implementation) #ifndef MICROPY_PY_USELECT #define MICROPY_PY_USELECT (0) |