Skip to content

Commit f563406

Browse files
committed
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.
1 parent 22a6344 commit f563406

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

py/moduerrno.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
X(EALREADY) \
5959
X(EINPROGRESS) \
6060

61+
#if MICROPY_PY_UERRNO_ERRORCODE
6162
STATIC const mp_rom_map_elem_t errorcode_table[] = {
6263
#define X(e) { MP_ROM_INT(MP_ ## e), MP_ROM_QSTR(MP_QSTR_## e) },
6364
ERRNO_LIST
@@ -75,10 +76,13 @@ STATIC const mp_obj_dict_t errorcode_dict = {
7576
.table = (mp_map_elem_t*)(mp_rom_map_elem_t*)errorcode_table,
7677
},
7778
};
79+
#endif
7880

7981
STATIC const mp_rom_map_elem_t mp_module_uerrno_globals_table[] = {
8082
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uerrno) },
83+
#if MICROPY_PY_UERRNO_ERRORCODE
8184
{ MP_ROM_QSTR(MP_QSTR_errorcode), MP_ROM_PTR(&errorcode_dict) },
85+
#endif
8286

8387
#define X(e) { MP_ROM_QSTR(MP_QSTR_## e), MP_ROM_INT(MP_ ## e) },
8488
ERRNO_LIST
@@ -93,12 +97,23 @@ const mp_obj_module_t mp_module_uerrno = {
9397
};
9498

9599
qstr mp_errno_to_str(mp_obj_t errno_val) {
100+
#if MICROPY_PY_UERRNO_ERRORCODE
101+
// We have the errorcode dict so can do a lookup using the hash map
96102
mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&errorcode_dict.map, errno_val, MP_MAP_LOOKUP);
97103
if (elem == NULL) {
98104
return MP_QSTR_NULL;
99105
} else {
100106
return MP_OBJ_QSTR_VALUE(elem->value);
101107
}
108+
#else
109+
// We don't have the errorcode dict so do a simple search in the modules dict
110+
for (size_t i = 0; i < MP_ARRAY_SIZE(mp_module_uerrno_globals_table); ++i) {
111+
if (errno_val == mp_module_uerrno_globals_table[i].value) {
112+
return MP_OBJ_QSTR_VALUE(mp_module_uerrno_globals_table[i].key);
113+
}
114+
}
115+
return MP_QSTR_NULL;
116+
#endif
102117
}
103118

104119
#endif //MICROPY_PY_UERRNO

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,11 @@ typedef double mp_float_t;
917917
#define MICROPY_PY_UERRNO (0)
918918
#endif
919919

920+
// Whether to provide the uerrno.errorcode dict
921+
#ifndef MICROPY_PY_UERRNO_ERRORCODE
922+
#define MICROPY_PY_UERRNO_ERRORCODE (1)
923+
#endif
924+
920925
// Whether to provide "uselect" module (baremetal implementation)
921926
#ifndef MICROPY_PY_USELECT
922927
#define MICROPY_PY_USELECT (0)

0 commit comments

Comments
 (0)