modsys.c 8.7 KB

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