blob: 84a4eb0f47169a2b03404ad24e4995765f294a90 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
Paul Sokolovskybfc20922017-08-11 09:42:39 +03007 * Copyright (c) 2014-2017 Paul Sokolovsky
Damien George04b91472014-05-03 23:27:38 +01008 *
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
Damien George51dfcb42015-01-01 20:27:54 +000028#include "py/builtin.h"
29#include "py/objlist.h"
30#include "py/objtuple.h"
31#include "py/objstr.h"
32#include "py/objint.h"
Paul Sokolovskybfc20922017-08-11 09:42:39 +030033#include "py/objtype.h"
Damien George51dfcb42015-01-01 20:27:54 +000034#include "py/stream.h"
Paul Sokolovsky96aa3a32017-03-06 12:15:25 +010035#include "py/smallint.h"
Paul Sokolovskybfc20922017-08-11 09:42:39 +030036#include "py/runtime.h"
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030037
Damien Georgeee3fd462014-05-24 23:03:12 +010038#if MICROPY_PY_SYS
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030039
Damien George95f53462015-04-22 17:38:05 +010040#include "genhdr/mpversion.h"
Damien Georgec3184ae2015-04-21 14:45:04 +000041
Damien Georgecd342072015-01-12 22:30:49 +000042// defined per port; type of these is irrelevant, just need pointer
stijne50cff62015-04-09 12:27:15 +020043extern struct _mp_dummy_t mp_sys_stdin_obj;
44extern struct _mp_dummy_t mp_sys_stdout_obj;
45extern struct _mp_dummy_t mp_sys_stderr_obj;
Damien Georgecd342072015-01-12 22:30:49 +000046
Tom Collinsf06d0832017-01-12 16:08:51 -080047#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
Damien George999cedb2015-11-27 17:01:44 +000048const mp_print_t mp_sys_stdout_print = {&mp_sys_stdout_obj, mp_stream_write_adaptor};
Damien George5ae5ec92015-04-11 12:01:39 +010049#endif
50
Damien George0102ee02017-08-30 21:02:00 +100051// version - Python language version that this implementation conforms to, as a string
Damien George30dd23a2014-08-10 17:50:28 +010052STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0");
53
Damien George0102ee02017-08-30 21:02:00 +100054// version_info - Python language version that this implementation conforms to, as a tuple of ints
Paul Sokolovskybaaaf652014-04-13 09:46:58 +030055#define I(n) MP_OBJ_NEW_SMALL_INT(n)
56// TODO: CPython is now at 5-element array, but save 2 els so far...
Paul Sokolovskybbae42d2014-04-14 01:46:45 +030057STATIC const mp_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {I(3), I(4), I(0)}};
Damien Georgec3184ae2015-04-21 14:45:04 +000058
59// sys.implementation object
60// this holds the MicroPython version
61STATIC const mp_obj_tuple_t mp_sys_implementation_version_info_obj = {
62 {&mp_type_tuple},
63 3,
64 { I(MICROPY_VERSION_MAJOR), I(MICROPY_VERSION_MINOR), I(MICROPY_VERSION_MICRO) }
65};
66#if MICROPY_PY_ATTRTUPLE
67STATIC const qstr impl_fields[] = { MP_QSTR_name, MP_QSTR_version };
68STATIC MP_DEFINE_ATTRTUPLE(
69 mp_sys_implementation_obj,
70 impl_fields,
71 2,
Damien Georgecbf76742015-11-27 13:38:15 +000072 MP_ROM_QSTR(MP_QSTR_micropython),
73 MP_ROM_PTR(&mp_sys_implementation_version_info_obj)
Damien Georgec3184ae2015-04-21 14:45:04 +000074);
75#else
Damien Georgee1cda002017-06-08 00:41:27 +100076STATIC const mp_rom_obj_tuple_t mp_sys_implementation_obj = {
Damien Georgec3184ae2015-04-21 14:45:04 +000077 {&mp_type_tuple},
78 2,
79 {
Damien Georgee1cda002017-06-08 00:41:27 +100080 MP_ROM_QSTR(MP_QSTR_micropython),
81 MP_ROM_PTR(&mp_sys_implementation_version_info_obj),
Damien Georgec3184ae2015-04-21 14:45:04 +000082 }
83};
84#endif
85
Paul Sokolovskybaaaf652014-04-13 09:46:58 +030086#undef I
Damien George30dd23a2014-08-10 17:50:28 +010087
Paul Sokolovskyb9b93542014-06-07 23:40:04 +030088#ifdef MICROPY_PY_SYS_PLATFORM
Damien George0102ee02017-08-30 21:02:00 +100089// platform - the platform that MicroPython is running on
Paul Sokolovskyb9b93542014-06-07 23:40:04 +030090STATIC const MP_DEFINE_STR_OBJ(platform_obj, MICROPY_PY_SYS_PLATFORM);
91#endif
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030092
Damien George0102ee02017-08-30 21:02:00 +100093// exit([retval]): raise SystemExit, with optional argument given to the exception
Damien George4b72b3a2016-01-03 14:21:40 +000094STATIC mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
Damien Georgeb92cbe62014-09-15 15:53:09 +010095 mp_obj_t exc;
96 if (n_args == 0) {
97 exc = mp_obj_new_exception(&mp_type_SystemExit);
98 } else {
99 exc = mp_obj_new_exception_arg1(&mp_type_SystemExit, args[0]);
100 }
101 nlr_raise(exc);
102}
103MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
104
Damien George4b72b3a2016-01-03 14:21:40 +0000105STATIC mp_obj_t mp_sys_print_exception(size_t n_args, const mp_obj_t *args) {
Tom Collinsf06d0832017-01-12 16:08:51 -0800106 #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
Damien George999cedb2015-11-27 17:01:44 +0000107 void *stream_obj = &mp_sys_stdout_obj;
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200108 if (n_args > 1) {
Damien George999cedb2015-11-27 17:01:44 +0000109 stream_obj = MP_OBJ_TO_PTR(args[1]); // XXX may fail
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200110 }
111
Damien George999cedb2015-11-27 17:01:44 +0000112 mp_print_t print = {stream_obj, mp_stream_write_adaptor};
Damien George7f9d1d62015-04-09 23:56:15 +0100113 mp_obj_print_exception(&print, args[0]);
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200114 #else
Damien George3a2171e2015-09-04 16:53:46 +0100115 (void)n_args;
Damien George7f9d1d62015-04-09 23:56:15 +0100116 mp_obj_print_exception(&mp_plat_print, args[0]);
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200117 #endif
118
119 return mp_const_none;
120}
121MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_print_exception);
122
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300123#if MICROPY_PY_SYS_EXC_INFO
124STATIC mp_obj_t mp_sys_exc_info(void) {
Damien George999cedb2015-11-27 17:01:44 +0000125 mp_obj_t cur_exc = MP_OBJ_FROM_PTR(MP_STATE_VM(cur_exception));
126 mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300127
128 if (cur_exc == MP_OBJ_NULL) {
129 t->items[0] = mp_const_none;
130 t->items[1] = mp_const_none;
131 t->items[2] = mp_const_none;
Damien George999cedb2015-11-27 17:01:44 +0000132 return MP_OBJ_FROM_PTR(t);
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300133 }
134
Damien George999cedb2015-11-27 17:01:44 +0000135 t->items[0] = MP_OBJ_FROM_PTR(mp_obj_get_type(cur_exc));
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300136 t->items[1] = cur_exc;
137 t->items[2] = mp_const_none;
Damien George999cedb2015-11-27 17:01:44 +0000138 return MP_OBJ_FROM_PTR(t);
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300139}
140MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info);
141#endif
142
Paul Sokolovskybfc20922017-08-11 09:42:39 +0300143STATIC mp_obj_t mp_sys_getsizeof(mp_obj_t obj) {
144 return mp_unary_op(MP_UNARY_OP_SIZEOF, obj);
145}
146MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_getsizeof_obj, mp_sys_getsizeof);
147
Damien Georgecbf76742015-11-27 13:38:15 +0000148STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
149 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys) },
Paul Sokolovskyd99e9082014-05-10 16:50:45 +0300150
Damien Georgecbf76742015-11-27 13:38:15 +0000151 { MP_ROM_QSTR(MP_QSTR_path), MP_ROM_PTR(&MP_STATE_VM(mp_sys_path_obj)) },
152 { MP_ROM_QSTR(MP_QSTR_argv), MP_ROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)) },
153 { MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&version_obj) },
154 { MP_ROM_QSTR(MP_QSTR_version_info), MP_ROM_PTR(&mp_sys_version_info_obj) },
155 { MP_ROM_QSTR(MP_QSTR_implementation), MP_ROM_PTR(&mp_sys_implementation_obj) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200156 #ifdef MICROPY_PY_SYS_PLATFORM
Damien Georgecbf76742015-11-27 13:38:15 +0000157 { MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&platform_obj) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200158 #endif
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200159 #if MP_ENDIANNESS_LITTLE
Damien Georgecbf76742015-11-27 13:38:15 +0000160 { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_little) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200161 #else
Damien Georgecbf76742015-11-27 13:38:15 +0000162 { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_big) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200163 #endif
164
165 #if MICROPY_PY_SYS_MAXSIZE
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300166 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
Paul Sokolovsky96aa3a32017-03-06 12:15:25 +0100167 // Maximum mp_int_t value is not representable as small int, so we have
168 // little choice but to use MP_SMALL_INT_MAX. Apps also should be careful
169 // to not try to compare sys.maxsize to some literal number (as this
170 // number might not fit in available int size), but instead count number
171 // of "one" bits in sys.maxsize.
Damien Georgeb62bb532017-07-31 12:59:39 +1000172 { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_INT(MP_SMALL_INT_MAX) },
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300173 #else
Damien Georgecbf76742015-11-27 13:38:15 +0000174 { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_PTR(&mp_maxsize_obj) },
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300175 #endif
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200176 #endif
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300177
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200178 #if MICROPY_PY_SYS_EXIT
Damien Georgecbf76742015-11-27 13:38:15 +0000179 { MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&mp_sys_exit_obj) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200180 #endif
Damien George89755ae2014-05-11 17:35:43 +0100181
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200182 #if MICROPY_PY_SYS_STDFILES
Damien Georgecbf76742015-11-27 13:38:15 +0000183 { MP_ROM_QSTR(MP_QSTR_stdin), MP_ROM_PTR(&mp_sys_stdin_obj) },
184 { MP_ROM_QSTR(MP_QSTR_stdout), MP_ROM_PTR(&mp_sys_stdout_obj) },
185 { MP_ROM_QSTR(MP_QSTR_stderr), MP_ROM_PTR(&mp_sys_stderr_obj) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200186 #endif
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200187
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200188 #if MICROPY_PY_SYS_MODULES
Damien George3911d5a2015-12-16 19:40:14 -0500189 { MP_ROM_QSTR(MP_QSTR_modules), MP_ROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)) },
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200190 #endif
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300191 #if MICROPY_PY_SYS_EXC_INFO
Damien Georgecbf76742015-11-27 13:38:15 +0000192 { MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300193 #endif
Paul Sokolovskybfc20922017-08-11 09:42:39 +0300194 #if MICROPY_PY_SYS_GETSIZEOF
195 { MP_ROM_QSTR(MP_QSTR_getsizeof), MP_ROM_PTR(&mp_sys_getsizeof_obj) },
196 #endif
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300197
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200198 /*
199 * Extensions to CPython
200 */
201
Damien Georgecbf76742015-11-27 13:38:15 +0000202 { MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300203};
204
Damien George3b603f22014-11-29 14:39:27 +0000205STATIC MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table);
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300206
207const mp_obj_module_t mp_module_sys = {
208 .base = { &mp_type_module },
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300209 .globals = (mp_obj_dict_t*)&mp_module_sys_globals,
210};
211
212#endif