modsys.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #include "genhdr/mpversion.h"
  38. // defined per port; type of these is irrelevant, just need pointer
  39. extern struct _mp_dummy_t mp_sys_stdin_obj;
  40. extern struct _mp_dummy_t mp_sys_stdout_obj;
  41. extern struct _mp_dummy_t mp_sys_stderr_obj;
  42. #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
  43. const mp_print_t mp_sys_stdout_print = {&mp_sys_stdout_obj, mp_stream_write_adaptor};
  44. #endif
  45. // version - Python language version that this implementation conforms to, as a string
  46. STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0");
  47. // version_info - Python language version that this implementation conforms to, as a tuple of ints
  48. #define I(n) MP_OBJ_NEW_SMALL_INT(n)
  49. // TODO: CPython is now at 5-element array, but save 2 els so far...
  50. STATIC const mp_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {I(3), I(4), I(0)}};
  51. // sys.implementation object
  52. // this holds the MicroPython version
  53. STATIC const mp_obj_tuple_t mp_sys_implementation_version_info_obj = {
  54. {&mp_type_tuple},
  55. 3,
  56. { I(MICROPY_VERSION_MAJOR), I(MICROPY_VERSION_MINOR), I(MICROPY_VERSION_MICRO) }
  57. };
  58. #if MICROPY_PY_ATTRTUPLE
  59. STATIC const qstr impl_fields[] = { MP_QSTR_name, MP_QSTR_version };
  60. STATIC MP_DEFINE_ATTRTUPLE(
  61. mp_sys_implementation_obj,
  62. impl_fields,
  63. 2,
  64. MP_ROM_QSTR(MP_QSTR_micropython),
  65. MP_ROM_PTR(&mp_sys_implementation_version_info_obj)
  66. );
  67. #else
  68. STATIC const mp_rom_obj_tuple_t mp_sys_implementation_obj = {
  69. {&mp_type_tuple},
  70. 2,
  71. {
  72. MP_ROM_QSTR(MP_QSTR_micropython),
  73. MP_ROM_PTR(&mp_sys_implementation_version_info_obj),
  74. }
  75. };
  76. #endif
  77. #undef I
  78. #ifdef MICROPY_PY_SYS_PLATFORM
  79. // platform - the platform that MicroPython is running on
  80. STATIC const MP_DEFINE_STR_OBJ(platform_obj, MICROPY_PY_SYS_PLATFORM);
  81. #endif
  82. // exit([retval]): raise SystemExit, with optional argument given to the exception
  83. STATIC mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
  84. mp_obj_t exc;
  85. if (n_args == 0) {
  86. exc = mp_obj_new_exception(&mp_type_SystemExit);
  87. } else {
  88. exc = mp_obj_new_exception_arg1(&mp_type_SystemExit, args[0]);
  89. }
  90. nlr_raise(exc);
  91. }
  92. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
  93. STATIC mp_obj_t mp_sys_print_exception(size_t n_args, const mp_obj_t *args) {
  94. #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
  95. void *stream_obj = &mp_sys_stdout_obj;
  96. if (n_args > 1) {
  97. mp_get_stream_raise(args[1], MP_STREAM_OP_WRITE);
  98. stream_obj = MP_OBJ_TO_PTR(args[1]);
  99. }
  100. mp_print_t print = {stream_obj, mp_stream_write_adaptor};
  101. mp_obj_print_exception(&print, args[0]);
  102. #else
  103. (void)n_args;
  104. mp_obj_print_exception(&mp_plat_print, args[0]);
  105. #endif
  106. return mp_const_none;
  107. }
  108. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_print_exception);
  109. #if MICROPY_PY_SYS_EXC_INFO
  110. STATIC mp_obj_t mp_sys_exc_info(void) {
  111. mp_obj_t cur_exc = MP_OBJ_FROM_PTR(MP_STATE_VM(cur_exception));
  112. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
  113. if (cur_exc == MP_OBJ_NULL) {
  114. t->items[0] = mp_const_none;
  115. t->items[1] = mp_const_none;
  116. t->items[2] = mp_const_none;
  117. return MP_OBJ_FROM_PTR(t);
  118. }
  119. t->items[0] = MP_OBJ_FROM_PTR(mp_obj_get_type(cur_exc));
  120. t->items[1] = cur_exc;
  121. t->items[2] = mp_const_none;
  122. return MP_OBJ_FROM_PTR(t);
  123. }
  124. MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info);
  125. #endif
  126. #if MICROPY_PY_SYS_GETSIZEOF
  127. STATIC mp_obj_t mp_sys_getsizeof(mp_obj_t obj) {
  128. return mp_unary_op(MP_UNARY_OP_SIZEOF, obj);
  129. }
  130. STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_getsizeof_obj, mp_sys_getsizeof);
  131. #endif
  132. STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
  133. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys) },
  134. { MP_ROM_QSTR(MP_QSTR_path), MP_ROM_PTR(&MP_STATE_VM(mp_sys_path_obj)) },
  135. { MP_ROM_QSTR(MP_QSTR_argv), MP_ROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)) },
  136. { MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&version_obj) },
  137. { MP_ROM_QSTR(MP_QSTR_version_info), MP_ROM_PTR(&mp_sys_version_info_obj) },
  138. { MP_ROM_QSTR(MP_QSTR_implementation), MP_ROM_PTR(&mp_sys_implementation_obj) },
  139. #ifdef MICROPY_PY_SYS_PLATFORM
  140. { MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&platform_obj) },
  141. #endif
  142. #if MP_ENDIANNESS_LITTLE
  143. { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_little) },
  144. #else
  145. { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_big) },
  146. #endif
  147. #if MICROPY_PY_SYS_MAXSIZE
  148. #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
  149. // Maximum mp_int_t value is not representable as small int, so we have
  150. // little choice but to use MP_SMALL_INT_MAX. Apps also should be careful
  151. // to not try to compare sys.maxsize to some literal number (as this
  152. // number might not fit in available int size), but instead count number
  153. // of "one" bits in sys.maxsize.
  154. { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_INT(MP_SMALL_INT_MAX) },
  155. #else
  156. { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_PTR(&mp_maxsize_obj) },
  157. #endif
  158. #endif
  159. #if MICROPY_PY_SYS_EXIT
  160. { MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&mp_sys_exit_obj) },
  161. #endif
  162. #if MICROPY_PY_SYS_STDFILES
  163. { MP_ROM_QSTR(MP_QSTR_stdin), MP_ROM_PTR(&mp_sys_stdin_obj) },
  164. { MP_ROM_QSTR(MP_QSTR_stdout), MP_ROM_PTR(&mp_sys_stdout_obj) },
  165. { MP_ROM_QSTR(MP_QSTR_stderr), MP_ROM_PTR(&mp_sys_stderr_obj) },
  166. #endif
  167. #if MICROPY_PY_SYS_MODULES
  168. { MP_ROM_QSTR(MP_QSTR_modules), MP_ROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)) },
  169. #endif
  170. #if MICROPY_PY_SYS_EXC_INFO
  171. { MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
  172. #endif
  173. #if MICROPY_PY_SYS_GETSIZEOF
  174. { MP_ROM_QSTR(MP_QSTR_getsizeof), MP_ROM_PTR(&mp_sys_getsizeof_obj) },
  175. #endif
  176. /*
  177. * Extensions to CPython
  178. */
  179. { MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
  180. };
  181. STATIC MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table);
  182. const mp_obj_module_t mp_module_sys = {
  183. .base = { &mp_type_module },
  184. .globals = (mp_obj_dict_t*)&mp_module_sys_globals,
  185. };
  186. #endif