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