moderrno.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2016 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <assert.h>
  27. #include <string.h>
  28. #include "py/obj.h"
  29. #include "py/mperrno.h"
  30. #if MICROPY_PY_ERRNO
  31. // This list can be defined per port in mpconfigport.h to tailor it to a
  32. // specific port's needs. If it's not defined then we provide a default.
  33. #ifndef MICROPY_PY_ERRNO_LIST
  34. #define MICROPY_PY_ERRNO_LIST \
  35. X(EPERM) \
  36. X(ENOENT) \
  37. X(EIO) \
  38. X(EBADF) \
  39. X(EAGAIN) \
  40. X(ENOMEM) \
  41. X(EACCES) \
  42. X(EEXIST) \
  43. X(ENODEV) \
  44. X(EISDIR) \
  45. X(EINVAL) \
  46. X(EOPNOTSUPP) \
  47. X(EADDRINUSE) \
  48. X(ECONNABORTED) \
  49. X(ECONNRESET) \
  50. X(ENOBUFS) \
  51. X(ENOTCONN) \
  52. X(ETIMEDOUT) \
  53. X(ECONNREFUSED) \
  54. X(EHOSTUNREACH) \
  55. X(EALREADY) \
  56. X(EINPROGRESS) \
  57. #endif
  58. #if MICROPY_PY_ERRNO_ERRORCODE
  59. static const mp_rom_map_elem_t errorcode_table[] = {
  60. #define X(e) { MP_ROM_INT(MP_##e), MP_ROM_QSTR(MP_QSTR_##e) },
  61. MICROPY_PY_ERRNO_LIST
  62. #undef X
  63. };
  64. static const mp_obj_dict_t errorcode_dict = {
  65. .base = {&mp_type_dict},
  66. .map = {
  67. .all_keys_are_qstrs = 0, // keys are integers
  68. .is_fixed = 1,
  69. .is_ordered = 1,
  70. .used = MP_ARRAY_SIZE(errorcode_table),
  71. .alloc = MP_ARRAY_SIZE(errorcode_table),
  72. .table = (mp_map_elem_t *)(mp_rom_map_elem_t *)errorcode_table,
  73. },
  74. };
  75. #endif
  76. static const mp_rom_map_elem_t mp_module_errno_globals_table[] = {
  77. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_errno) },
  78. #if MICROPY_PY_ERRNO_ERRORCODE
  79. { MP_ROM_QSTR(MP_QSTR_errorcode), MP_ROM_PTR(&errorcode_dict) },
  80. #endif
  81. #define X(e) { MP_ROM_QSTR(MP_QSTR_##e), MP_ROM_INT(MP_##e) },
  82. MICROPY_PY_ERRNO_LIST
  83. #undef X
  84. };
  85. static MP_DEFINE_CONST_DICT(mp_module_errno_globals, mp_module_errno_globals_table);
  86. const mp_obj_module_t mp_module_errno = {
  87. .base = { &mp_type_module },
  88. .globals = (mp_obj_dict_t *)&mp_module_errno_globals,
  89. };
  90. MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_errno, mp_module_errno);
  91. qstr mp_errno_to_str(mp_obj_t errno_val) {
  92. #if MICROPY_PY_ERRNO_ERRORCODE
  93. // We have the errorcode dict so can do a lookup using the hash map
  94. mp_map_elem_t *elem = mp_map_lookup((mp_map_t *)&errorcode_dict.map, errno_val, MP_MAP_LOOKUP);
  95. if (elem == NULL) {
  96. return MP_QSTRnull;
  97. } else {
  98. return MP_OBJ_QSTR_VALUE(elem->value);
  99. }
  100. #else
  101. // We don't have the errorcode dict so do a simple search in the modules dict
  102. for (size_t i = 0; i < MP_ARRAY_SIZE(mp_module_errno_globals_table); ++i) {
  103. if (errno_val == mp_module_errno_globals_table[i].value) {
  104. return MP_OBJ_QSTR_VALUE(mp_module_errno_globals_table[i].key);
  105. }
  106. }
  107. return MP_QSTRnull;
  108. #endif
  109. }
  110. #endif // MICROPY_PY_ERRNO