modsys.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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/objmodule.h"
  30. #include "py/objtuple.h"
  31. #include "py/objstr.h"
  32. #include "py/objint.h"
  33. #include "py/objtype.h"
  34. #include "py/stream.h"
  35. #include "py/smallint.h"
  36. #include "py/runtime.h"
  37. #include "py/persistentcode.h"
  38. #include "extmod/modplatform.h"
  39. #include "genhdr/mpversion.h"
  40. #if MICROPY_PY_SYS_SETTRACE
  41. #include "py/objmodule.h"
  42. #include "py/profile.h"
  43. #endif
  44. #if MICROPY_PY_SYS
  45. // defined per port; type of these is irrelevant, just need pointer
  46. extern struct _mp_dummy_t mp_sys_stdin_obj;
  47. extern struct _mp_dummy_t mp_sys_stdout_obj;
  48. extern struct _mp_dummy_t mp_sys_stderr_obj;
  49. #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
  50. const mp_print_t mp_sys_stdout_print = {&mp_sys_stdout_obj, mp_stream_write_adaptor};
  51. #endif
  52. // version - Python language version that this implementation conforms to, as a string
  53. static const MP_DEFINE_STR_OBJ(mp_sys_version_obj, "3.4.0; " MICROPY_BANNER_NAME_AND_VERSION);
  54. // version_info - Python language version that this implementation conforms to, as a tuple of ints
  55. // TODO: CPython is now at 5-element array (major, minor, micro, releaselevel, serial), but save 2 els so far...
  56. static const mp_rom_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {MP_ROM_INT(3), MP_ROM_INT(4), MP_ROM_INT(0)}};
  57. // sys.implementation object
  58. // this holds the MicroPython version
  59. static const mp_rom_obj_tuple_t mp_sys_implementation_version_info_obj = {
  60. {&mp_type_tuple},
  61. 4,
  62. {
  63. MP_ROM_INT(MICROPY_VERSION_MAJOR),
  64. MP_ROM_INT(MICROPY_VERSION_MINOR),
  65. MP_ROM_INT(MICROPY_VERSION_MICRO),
  66. #if MICROPY_VERSION_PRERELEASE
  67. MP_ROM_QSTR(MP_QSTR_preview),
  68. #else
  69. MP_ROM_QSTR(MP_QSTR_),
  70. #endif
  71. }
  72. };
  73. static const MP_DEFINE_STR_OBJ(mp_sys_implementation_machine_obj, MICROPY_BANNER_MACHINE);
  74. #define SYS_IMPLEMENTATION_ELEMS_BASE \
  75. MP_ROM_QSTR(MP_QSTR_micropython), \
  76. MP_ROM_PTR(&mp_sys_implementation_version_info_obj), \
  77. MP_ROM_PTR(&mp_sys_implementation_machine_obj)
  78. #if MICROPY_PERSISTENT_CODE_LOAD
  79. #define SYS_IMPLEMENTATION_ELEMS__MPY \
  80. , MP_ROM_INT(MPY_FILE_HEADER_INT)
  81. #else
  82. #define SYS_IMPLEMENTATION_ELEMS__MPY
  83. #endif
  84. #if MICROPY_PY_ATTRTUPLE
  85. #if MICROPY_PREVIEW_VERSION_2
  86. #define SYS_IMPLEMENTATION_ELEMS__V2 \
  87. , MP_ROM_TRUE
  88. #else
  89. #define SYS_IMPLEMENTATION_ELEMS__V2
  90. #endif
  91. static const qstr impl_fields[] = {
  92. MP_QSTR_name,
  93. MP_QSTR_version,
  94. MP_QSTR__machine,
  95. #if MICROPY_PERSISTENT_CODE_LOAD
  96. MP_QSTR__mpy,
  97. #endif
  98. #if MICROPY_PREVIEW_VERSION_2
  99. MP_QSTR__v2,
  100. #endif
  101. };
  102. static MP_DEFINE_ATTRTUPLE(
  103. mp_sys_implementation_obj,
  104. impl_fields,
  105. 3 + MICROPY_PERSISTENT_CODE_LOAD + MICROPY_PREVIEW_VERSION_2,
  106. SYS_IMPLEMENTATION_ELEMS_BASE
  107. SYS_IMPLEMENTATION_ELEMS__MPY
  108. SYS_IMPLEMENTATION_ELEMS__V2
  109. );
  110. #else
  111. static const mp_rom_obj_tuple_t mp_sys_implementation_obj = {
  112. {&mp_type_tuple},
  113. 3 + MICROPY_PERSISTENT_CODE_LOAD,
  114. // Do not include SYS_IMPLEMENTATION_ELEMS__V2 because
  115. // SYS_IMPLEMENTATION_ELEMS__MPY may be empty if
  116. // MICROPY_PERSISTENT_CODE_LOAD is disabled, which means they'll share
  117. // the same index. Cannot query _v2 if MICROPY_PY_ATTRTUPLE is
  118. // disabled.
  119. {
  120. SYS_IMPLEMENTATION_ELEMS_BASE
  121. SYS_IMPLEMENTATION_ELEMS__MPY
  122. }
  123. };
  124. #endif
  125. #undef I
  126. #ifdef MICROPY_PY_SYS_PLATFORM
  127. // platform - the platform that MicroPython is running on
  128. static const MP_DEFINE_STR_OBJ(mp_sys_platform_obj, MICROPY_PY_SYS_PLATFORM);
  129. #endif
  130. #ifdef MICROPY_PY_SYS_EXECUTABLE
  131. // executable - the path to the micropython binary
  132. // This object is non-const and is populated at startup in main()
  133. MP_DEFINE_STR_OBJ(mp_sys_executable_obj, "");
  134. #endif
  135. #if MICROPY_PY_SYS_INTERN
  136. MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_intern_obj, mp_obj_str_intern_checked);
  137. #endif
  138. // exit([retval]): raise SystemExit, with optional argument given to the exception
  139. static mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
  140. if (n_args == 0) {
  141. mp_raise_type(&mp_type_SystemExit);
  142. } else {
  143. mp_raise_type_arg(&mp_type_SystemExit, args[0]);
  144. }
  145. }
  146. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
  147. static mp_obj_t mp_sys_print_exception(size_t n_args, const mp_obj_t *args) {
  148. #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
  149. void *stream_obj = &mp_sys_stdout_obj;
  150. if (n_args > 1) {
  151. mp_get_stream_raise(args[1], MP_STREAM_OP_WRITE);
  152. stream_obj = MP_OBJ_TO_PTR(args[1]);
  153. }
  154. mp_print_t print = {stream_obj, mp_stream_write_adaptor};
  155. mp_obj_print_exception(&print, args[0]);
  156. #else
  157. (void)n_args;
  158. mp_obj_print_exception(&mp_plat_print, args[0]);
  159. #endif
  160. return mp_const_none;
  161. }
  162. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_print_exception);
  163. #if MICROPY_PY_SYS_EXC_INFO
  164. static mp_obj_t mp_sys_exc_info(void) {
  165. mp_obj_t cur_exc = MP_OBJ_FROM_PTR(MP_STATE_VM(cur_exception));
  166. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
  167. if (cur_exc == MP_OBJ_NULL) {
  168. t->items[0] = mp_const_none;
  169. t->items[1] = mp_const_none;
  170. t->items[2] = mp_const_none;
  171. return MP_OBJ_FROM_PTR(t);
  172. }
  173. t->items[0] = MP_OBJ_FROM_PTR(mp_obj_get_type(cur_exc));
  174. t->items[1] = cur_exc;
  175. t->items[2] = mp_const_none;
  176. return MP_OBJ_FROM_PTR(t);
  177. }
  178. MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info);
  179. #endif
  180. #if MICROPY_PY_SYS_GETSIZEOF
  181. static mp_obj_t mp_sys_getsizeof(mp_obj_t obj) {
  182. return mp_unary_op(MP_UNARY_OP_SIZEOF, obj);
  183. }
  184. static MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_getsizeof_obj, mp_sys_getsizeof);
  185. #endif
  186. #if MICROPY_PY_SYS_ATEXIT
  187. // atexit(callback): Callback is called when sys.exit is called.
  188. static mp_obj_t mp_sys_atexit(mp_obj_t obj) {
  189. mp_obj_t old = MP_STATE_VM(sys_exitfunc);
  190. MP_STATE_VM(sys_exitfunc) = obj;
  191. return old;
  192. }
  193. static MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_atexit_obj, mp_sys_atexit);
  194. #endif
  195. #if MICROPY_PY_SYS_SETTRACE
  196. // settrace(tracefunc): Set the system's trace function.
  197. static mp_obj_t mp_sys_settrace(mp_obj_t obj) {
  198. return mp_prof_settrace(obj);
  199. }
  200. MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_settrace_obj, mp_sys_settrace);
  201. #endif // MICROPY_PY_SYS_SETTRACE
  202. #if MICROPY_PY_SYS_PATH && !MICROPY_PY_SYS_ATTR_DELEGATION
  203. #error "MICROPY_PY_SYS_PATH requires MICROPY_PY_SYS_ATTR_DELEGATION"
  204. #endif
  205. #if MICROPY_PY_SYS_PS1_PS2 && !MICROPY_PY_SYS_ATTR_DELEGATION
  206. #error "MICROPY_PY_SYS_PS1_PS2 requires MICROPY_PY_SYS_ATTR_DELEGATION"
  207. #endif
  208. #if MICROPY_PY_SYS_TRACEBACKLIMIT && !MICROPY_PY_SYS_ATTR_DELEGATION
  209. #error "MICROPY_PY_SYS_TRACEBACKLIMIT requires MICROPY_PY_SYS_ATTR_DELEGATION"
  210. #endif
  211. #if MICROPY_PY_SYS_ATTR_DELEGATION && !MICROPY_MODULE_ATTR_DELEGATION
  212. #error "MICROPY_PY_SYS_ATTR_DELEGATION requires MICROPY_MODULE_ATTR_DELEGATION"
  213. #endif
  214. #if MICROPY_PY_SYS_ATTR_DELEGATION
  215. // Must be kept in sync with the enum at the top of mpstate.h.
  216. static const uint16_t sys_mutable_keys[] = {
  217. #if MICROPY_PY_SYS_PATH
  218. // Code should access this (as an mp_obj_t) for use with e.g.
  219. // mp_obj_list_append by using the `mp_sys_path` macro defined in runtime.h.
  220. MP_QSTR_path,
  221. #endif
  222. #if MICROPY_PY_SYS_PS1_PS2
  223. MP_QSTR_ps1,
  224. MP_QSTR_ps2,
  225. #endif
  226. #if MICROPY_PY_SYS_TRACEBACKLIMIT
  227. MP_QSTR_tracebacklimit,
  228. #endif
  229. MP_QSTRnull,
  230. };
  231. void mp_module_sys_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  232. MP_STATIC_ASSERT(MP_ARRAY_SIZE(sys_mutable_keys) == MP_SYS_MUTABLE_NUM + 1);
  233. MP_STATIC_ASSERT(MP_ARRAY_SIZE(MP_STATE_VM(sys_mutable)) == MP_SYS_MUTABLE_NUM);
  234. mp_module_generic_attr(attr, dest, sys_mutable_keys, MP_STATE_VM(sys_mutable));
  235. }
  236. #endif
  237. static const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
  238. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys) },
  239. #if MICROPY_PY_SYS_ARGV
  240. { MP_ROM_QSTR(MP_QSTR_argv), MP_ROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)) },
  241. #endif
  242. { MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&mp_sys_version_obj) },
  243. { MP_ROM_QSTR(MP_QSTR_version_info), MP_ROM_PTR(&mp_sys_version_info_obj) },
  244. { MP_ROM_QSTR(MP_QSTR_implementation), MP_ROM_PTR(&mp_sys_implementation_obj) },
  245. #ifdef MICROPY_PY_SYS_PLATFORM
  246. { MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&mp_sys_platform_obj) },
  247. #endif
  248. #if MP_ENDIANNESS_LITTLE
  249. { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_little) },
  250. #else
  251. { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_big) },
  252. #endif
  253. #if MICROPY_PY_SYS_MAXSIZE
  254. #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
  255. // Maximum mp_int_t value is not representable as small int, so we have
  256. // little choice but to use MP_SMALL_INT_MAX. Apps also should be careful
  257. // to not try to compare sys.maxsize to some literal number (as this
  258. // number might not fit in available int size), but instead count number
  259. // of "one" bits in sys.maxsize.
  260. { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_INT(MP_SMALL_INT_MAX) },
  261. #else
  262. { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_PTR(&mp_sys_maxsize_obj) },
  263. #endif
  264. #endif
  265. #if MICROPY_PY_SYS_INTERN
  266. { MP_ROM_QSTR(MP_QSTR_intern), MP_ROM_PTR(&mp_sys_intern_obj) },
  267. #endif
  268. #if MICROPY_PY_SYS_EXIT
  269. { MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&mp_sys_exit_obj) },
  270. #endif
  271. #if MICROPY_PY_SYS_SETTRACE
  272. { MP_ROM_QSTR(MP_QSTR_settrace), MP_ROM_PTR(&mp_sys_settrace_obj) },
  273. #endif
  274. #if MICROPY_PY_SYS_STDFILES
  275. { MP_ROM_QSTR(MP_QSTR_stdin), MP_ROM_PTR(&mp_sys_stdin_obj) },
  276. { MP_ROM_QSTR(MP_QSTR_stdout), MP_ROM_PTR(&mp_sys_stdout_obj) },
  277. { MP_ROM_QSTR(MP_QSTR_stderr), MP_ROM_PTR(&mp_sys_stderr_obj) },
  278. #endif
  279. #if MICROPY_PY_SYS_MODULES
  280. { MP_ROM_QSTR(MP_QSTR_modules), MP_ROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)) },
  281. #endif
  282. #if MICROPY_PY_SYS_EXC_INFO
  283. { MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
  284. #endif
  285. #if MICROPY_PY_SYS_GETSIZEOF
  286. { MP_ROM_QSTR(MP_QSTR_getsizeof), MP_ROM_PTR(&mp_sys_getsizeof_obj) },
  287. #endif
  288. #if MICROPY_PY_SYS_EXECUTABLE
  289. { MP_ROM_QSTR(MP_QSTR_executable), MP_ROM_PTR(&mp_sys_executable_obj) },
  290. #endif
  291. /*
  292. * Extensions to CPython
  293. */
  294. { MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
  295. #if MICROPY_PY_SYS_ATEXIT
  296. { MP_ROM_QSTR(MP_QSTR_atexit), MP_ROM_PTR(&mp_sys_atexit_obj) },
  297. #endif
  298. };
  299. static MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table);
  300. const mp_obj_module_t mp_module_sys = {
  301. .base = { &mp_type_module },
  302. .globals = (mp_obj_dict_t *)&mp_module_sys_globals,
  303. };
  304. // Unlike the other CPython-compatible modules, sys is not extensible from the
  305. // filesystem. We rely on it to work so that things like sys.path are always
  306. // available.
  307. MP_REGISTER_MODULE(MP_QSTR_sys, mp_module_sys);
  308. #if MICROPY_PY_SYS_ARGV
  309. // Code should access this (as an mp_obj_t) for use with e.g.
  310. // mp_obj_list_append by using the `mp_sys_argv` macro defined in runtime.h.
  311. MP_REGISTER_ROOT_POINTER(mp_obj_list_t mp_sys_argv_obj);
  312. #endif
  313. #if MICROPY_PY_SYS_EXC_INFO
  314. // current exception being handled, for sys.exc_info()
  315. MP_REGISTER_ROOT_POINTER(mp_obj_base_t * cur_exception);
  316. #endif
  317. #if MICROPY_PY_SYS_ATEXIT
  318. // exposed through sys.atexit function
  319. MP_REGISTER_ROOT_POINTER(mp_obj_t sys_exitfunc);
  320. #endif
  321. #if MICROPY_PY_SYS_ATTR_DELEGATION
  322. // Contains mutable sys attributes.
  323. MP_REGISTER_ROOT_POINTER(mp_obj_t sys_mutable[MP_SYS_MUTABLE_NUM]);
  324. MP_REGISTER_MODULE_DELEGATION(mp_module_sys, mp_module_sys_attr);
  325. #endif
  326. #endif // MICROPY_PY_SYS