modsys.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. * Copyright (c) 2014-2017 Paul Sokolovsky
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "py/builtin.h"
  28. #include "py/objlist.h"
  29. #include "py/objtuple.h"
  30. #include "py/objstr.h"
  31. #include "py/objint.h"
  32. #include "py/objtype.h"
  33. #include "py/stream.h"
  34. #include "py/smallint.h"
  35. #include "py/runtime.h"
  36. #if MICROPY_PY_SYS
  37. // defined per port; type of these is irrelevant, just need pointer
  38. extern struct _mp_dummy_t mp_sys_stdin_obj;
  39. extern struct _mp_dummy_t mp_sys_stdout_obj;
  40. extern struct _mp_dummy_t mp_sys_stderr_obj;
  41. #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
  42. const mp_print_t mp_sys_stdout_print = {&mp_sys_stdout_obj, mp_stream_write_adaptor};
  43. #endif
  44. // version - Python language version that this implementation conforms to, as a string
  45. STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0");
  46. // version_info - Python language version that this implementation conforms to, as a tuple of ints
  47. #define I(n) MP_OBJ_NEW_SMALL_INT(n)
  48. // TODO: CPython is now at 5-element array, but save 2 els so far...
  49. STATIC const mp_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {I(3), I(4), I(0)}};
  50. // sys.implementation object
  51. // this holds the MicroPython version
  52. STATIC const mp_obj_tuple_t mp_sys_implementation_version_info_obj = {
  53. {&mp_type_tuple},
  54. 3,
  55. { I(MICROPY_VERSION_MAJOR), I(MICROPY_VERSION_MINOR), I(MICROPY_VERSION_MICRO) }
  56. };
  57. #if MICROPY_PY_ATTRTUPLE
  58. STATIC const qstr impl_fields[] = { MP_QSTR_name, MP_QSTR_version };
  59. STATIC MP_DEFINE_ATTRTUPLE(
  60. mp_sys_implementation_obj,
  61. impl_fields,
  62. 2,
  63. MP_ROM_QSTR(MP_QSTR_micropython),
  64. MP_ROM_PTR(&mp_sys_implementation_version_info_obj)
  65. );
  66. #else
  67. STATIC const mp_rom_obj_tuple_t mp_sys_implementation_obj = {
  68. {&mp_type_tuple},
  69. 2,
  70. {
  71. MP_ROM_QSTR(MP_QSTR_micropython),
  72. MP_ROM_PTR(&mp_sys_implementation_version_info_obj),
  73. }
  74. };
  75. #endif
  76. #undef I
  77. #ifdef MICROPY_PY_SYS_PLATFORM
  78. // platform - the platform that MicroPython is running on
  79. STATIC const MP_DEFINE_STR_OBJ(platform_obj, MICROPY_PY_SYS_PLATFORM);
  80. #endif
  81. // exit([retval]): raise SystemExit, with optional argument given to the exception
  82. STATIC mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
  83. mp_obj_t exc;
  84. if (n_args == 0) {
  85. exc = mp_obj_new_exception(&mp_type_SystemExit);
  86. } else {
  87. exc = mp_obj_new_exception_arg1(&mp_type_SystemExit, args[0]);
  88. }
  89. nlr_raise(exc);
  90. }
  91. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
  92. STATIC mp_obj_t mp_sys_print_exception(size_t n_args, const mp_obj_t *args) {
  93. #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
  94. void *stream_obj = &mp_sys_stdout_obj;
  95. if (n_args > 1) {
  96. mp_get_stream_raise(args[1], MP_STREAM_OP_WRITE);
  97. stream_obj = MP_OBJ_TO_PTR(args[1]);
  98. }
  99. mp_print_t print = {stream_obj, mp_stream_write_adaptor};
  100. mp_obj_print_exception(&print, args[0]);
  101. #else
  102. (void)n_args;
  103. mp_obj_print_exception(&mp_plat_print, args[0]);
  104. #endif
  105. return mp_const_none;
  106. }
  107. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_print_exception);
  108. #if MICROPY_PY_SYS_EXC_INFO
  109. STATIC mp_obj_t mp_sys_exc_info(void) {
  110. mp_obj_t cur_exc = MP_OBJ_FROM_PTR(MP_STATE_VM(cur_exception));
  111. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
  112. if (cur_exc == MP_OBJ_NULL) {
  113. t->items[0] = mp_const_none;
  114. t->items[1] = mp_const_none;
  115. t->items[2] = mp_const_none;
  116. return MP_OBJ_FROM_PTR(t);
  117. }
  118. t->items[0] = MP_OBJ_FROM_PTR(mp_obj_get_type(cur_exc));
  119. t->items[1] = cur_exc;
  120. t->items[2] = mp_const_none;
  121. return MP_OBJ_FROM_PTR(t);
  122. }
  123. MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info);
  124. #endif
  125. #if MICROPY_PY_SYS_GETSIZEOF
  126. STATIC mp_obj_t mp_sys_getsizeof(mp_obj_t obj) {
  127. return mp_unary_op(MP_UNARY_OP_SIZEOF, obj);
  128. }
  129. STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_getsizeof_obj, mp_sys_getsizeof);
  130. #endif
  131. STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
  132. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys) },
  133. { MP_ROM_QSTR(MP_QSTR_path), MP_ROM_PTR(&MP_STATE_VM(mp_sys_path_obj)) },
  134. { MP_ROM_QSTR(MP_QSTR_argv), MP_ROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)) },
  135. { MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&version_obj) },
  136. { MP_ROM_QSTR(MP_QSTR_version_info), MP_ROM_PTR(&mp_sys_version_info_obj) },
  137. { MP_ROM_QSTR(MP_QSTR_implementation), MP_ROM_PTR(&mp_sys_implementation_obj) },
  138. #ifdef MICROPY_PY_SYS_PLATFORM
  139. { MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&platform_obj) },
  140. #endif
  141. #if MP_ENDIANNESS_LITTLE
  142. { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_little) },
  143. #else
  144. { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_big) },
  145. #endif
  146. #if MICROPY_PY_SYS_MAXSIZE
  147. #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
  148. // Maximum mp_int_t value is not representable as small int, so we have
  149. // little choice but to use MP_SMALL_INT_MAX. Apps also should be careful
  150. // to not try to compare sys.maxsize to some literal number (as this
  151. // number might not fit in available int size), but instead count number
  152. // of "one" bits in sys.maxsize.
  153. { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_INT(MP_SMALL_INT_MAX) },
  154. #else
  155. { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_PTR(&mp_maxsize_obj) },
  156. #endif
  157. #endif
  158. #if MICROPY_PY_SYS_EXIT
  159. { MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&mp_sys_exit_obj) },
  160. #endif
  161. #if MICROPY_PY_SYS_STDFILES
  162. { MP_ROM_QSTR(MP_QSTR_stdin), MP_ROM_PTR(&mp_sys_stdin_obj) },
  163. { MP_ROM_QSTR(MP_QSTR_stdout), MP_ROM_PTR(&mp_sys_stdout_obj) },
  164. { MP_ROM_QSTR(MP_QSTR_stderr), MP_ROM_PTR(&mp_sys_stderr_obj) },
  165. #endif
  166. #if MICROPY_PY_SYS_MODULES
  167. { MP_ROM_QSTR(MP_QSTR_modules), MP_ROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)) },
  168. #endif
  169. #if MICROPY_PY_SYS_EXC_INFO
  170. { MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
  171. #endif
  172. #if MICROPY_PY_SYS_GETSIZEOF
  173. { MP_ROM_QSTR(MP_QSTR_getsizeof), MP_ROM_PTR(&mp_sys_getsizeof_obj) },
  174. #endif
  175. /*
  176. * Extensions to CPython
  177. */
  178. { MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
  179. };
  180. STATIC MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table);
  181. const mp_obj_module_t mp_module_sys = {
  182. .base = { &mp_type_module },
  183. .globals = (mp_obj_dict_t*)&mp_module_sys_globals,
  184. };
  185. #endif