modsys.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. stream_obj = MP_OBJ_TO_PTR(args[1]); // XXX may fail
  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. STATIC mp_obj_t mp_sys_getsizeof(mp_obj_t obj) {
  126. return mp_unary_op(MP_UNARY_OP_SIZEOF, obj);
  127. }
  128. MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_getsizeof_obj, mp_sys_getsizeof);
  129. STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
  130. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys) },
  131. { MP_ROM_QSTR(MP_QSTR_path), MP_ROM_PTR(&MP_STATE_VM(mp_sys_path_obj)) },
  132. { MP_ROM_QSTR(MP_QSTR_argv), MP_ROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)) },
  133. { MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&version_obj) },
  134. { MP_ROM_QSTR(MP_QSTR_version_info), MP_ROM_PTR(&mp_sys_version_info_obj) },
  135. { MP_ROM_QSTR(MP_QSTR_implementation), MP_ROM_PTR(&mp_sys_implementation_obj) },
  136. #ifdef MICROPY_PY_SYS_PLATFORM
  137. { MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&platform_obj) },
  138. #endif
  139. #if MP_ENDIANNESS_LITTLE
  140. { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_little) },
  141. #else
  142. { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_big) },
  143. #endif
  144. #if MICROPY_PY_SYS_MAXSIZE
  145. #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
  146. // Maximum mp_int_t value is not representable as small int, so we have
  147. // little choice but to use MP_SMALL_INT_MAX. Apps also should be careful
  148. // to not try to compare sys.maxsize to some literal number (as this
  149. // number might not fit in available int size), but instead count number
  150. // of "one" bits in sys.maxsize.
  151. { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_INT(MP_SMALL_INT_MAX) },
  152. #else
  153. { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_PTR(&mp_maxsize_obj) },
  154. #endif
  155. #endif
  156. #if MICROPY_PY_SYS_EXIT
  157. { MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&mp_sys_exit_obj) },
  158. #endif
  159. #if MICROPY_PY_SYS_STDFILES
  160. { MP_ROM_QSTR(MP_QSTR_stdin), MP_ROM_PTR(&mp_sys_stdin_obj) },
  161. { MP_ROM_QSTR(MP_QSTR_stdout), MP_ROM_PTR(&mp_sys_stdout_obj) },
  162. { MP_ROM_QSTR(MP_QSTR_stderr), MP_ROM_PTR(&mp_sys_stderr_obj) },
  163. #endif
  164. #if MICROPY_PY_SYS_MODULES
  165. { MP_ROM_QSTR(MP_QSTR_modules), MP_ROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)) },
  166. #endif
  167. #if MICROPY_PY_SYS_EXC_INFO
  168. { MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
  169. #endif
  170. #if MICROPY_PY_SYS_GETSIZEOF
  171. { MP_ROM_QSTR(MP_QSTR_getsizeof), MP_ROM_PTR(&mp_sys_getsizeof_obj) },
  172. #endif
  173. /*
  174. * Extensions to CPython
  175. */
  176. { MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
  177. };
  178. STATIC MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table);
  179. const mp_obj_module_t mp_module_sys = {
  180. .base = { &mp_type_module },
  181. .globals = (mp_obj_dict_t*)&mp_module_sys_globals,
  182. };
  183. #endif