blob: ecc0b6065fb8d9c9a73565ae5fb4b533b5ad1fbc [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 Georgee1e359f2015-02-07 17:24:10 +000028#include "py/mpstate.h"
Damien George51dfcb42015-01-01 20:27:54 +000029#include "py/nlr.h"
30#include "py/builtin.h"
31#include "py/objlist.h"
32#include "py/objtuple.h"
33#include "py/objstr.h"
34#include "py/objint.h"
Paul Sokolovskybfc20922017-08-11 09:42:39 +030035#include "py/objtype.h"
Damien George51dfcb42015-01-01 20:27:54 +000036#include "py/stream.h"
Paul Sokolovsky96aa3a32017-03-06 12:15:25 +010037#include "py/smallint.h"
Paul Sokolovskybfc20922017-08-11 09:42:39 +030038#include "py/runtime0.h"
39#include "py/runtime.h"
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030040
Damien Georgeee3fd462014-05-24 23:03:12 +010041#if MICROPY_PY_SYS
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030042
Damien George95f53462015-04-22 17:38:05 +010043#include "genhdr/mpversion.h"
Damien Georgec3184ae2015-04-21 14:45:04 +000044
Damien Georgecd342072015-01-12 22:30:49 +000045// defined per port; type of these is irrelevant, just need pointer
stijne50cff62015-04-09 12:27:15 +020046extern struct _mp_dummy_t mp_sys_stdin_obj;
47extern struct _mp_dummy_t mp_sys_stdout_obj;
48extern struct _mp_dummy_t mp_sys_stderr_obj;
Damien Georgecd342072015-01-12 22:30:49 +000049
Tom Collinsf06d0832017-01-12 16:08:51 -080050#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
Damien George999cedb2015-11-27 17:01:44 +000051const mp_print_t mp_sys_stdout_print = {&mp_sys_stdout_obj, mp_stream_write_adaptor};
Damien George5ae5ec92015-04-11 12:01:39 +010052#endif
53
Damien George0102ee02017-08-30 21:02:00 +100054// version - Python language version that this implementation conforms to, as a string
Damien George30dd23a2014-08-10 17:50:28 +010055STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0");
56
Damien George0102ee02017-08-30 21:02:00 +100057// version_info - Python language version that this implementation conforms to, as a tuple of ints
Paul Sokolovskybaaaf652014-04-13 09:46:58 +030058#define I(n) MP_OBJ_NEW_SMALL_INT(n)
59// TODO: CPython is now at 5-element array, but save 2 els so far...
Paul Sokolovskybbae42d2014-04-14 01:46:45 +030060STATIC 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 +000061
62// sys.implementation object
63// this holds the MicroPython version
64STATIC const mp_obj_tuple_t mp_sys_implementation_version_info_obj = {
65 {&mp_type_tuple},
66 3,
67 { I(MICROPY_VERSION_MAJOR), I(MICROPY_VERSION_MINOR), I(MICROPY_VERSION_MICRO) }
68};
69#if MICROPY_PY_ATTRTUPLE
70STATIC const qstr impl_fields[] = { MP_QSTR_name, MP_QSTR_version };
71STATIC MP_DEFINE_ATTRTUPLE(
72 mp_sys_implementation_obj,
73 impl_fields,
74 2,
Damien Georgecbf76742015-11-27 13:38:15 +000075 MP_ROM_QSTR(MP_QSTR_micropython),
76 MP_ROM_PTR(&mp_sys_implementation_version_info_obj)
Damien Georgec3184ae2015-04-21 14:45:04 +000077);
78#else
Damien Georgee1cda002017-06-08 00:41:27 +100079STATIC const mp_rom_obj_tuple_t mp_sys_implementation_obj = {
Damien Georgec3184ae2015-04-21 14:45:04 +000080 {&mp_type_tuple},
81 2,
82 {
Damien Georgee1cda002017-06-08 00:41:27 +100083 MP_ROM_QSTR(MP_QSTR_micropython),
84 MP_ROM_PTR(&mp_sys_implementation_version_info_obj),
Damien Georgec3184ae2015-04-21 14:45:04 +000085 }
86};
87#endif
88
Paul Sokolovskybaaaf652014-04-13 09:46:58 +030089#undef I
Damien George30dd23a2014-08-10 17:50:28 +010090
Paul Sokolovskyb9b93542014-06-07 23:40:04 +030091#ifdef MICROPY_PY_SYS_PLATFORM
Damien George0102ee02017-08-30 21:02:00 +100092// platform - the platform that MicroPython is running on
Paul Sokolovskyb9b93542014-06-07 23:40:04 +030093STATIC const MP_DEFINE_STR_OBJ(platform_obj, MICROPY_PY_SYS_PLATFORM);
94#endif
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030095
Damien George0102ee02017-08-30 21:02:00 +100096// exit([retval]): raise SystemExit, with optional argument given to the exception
Damien George4b72b3a2016-01-03 14:21:40 +000097STATIC mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
Damien Georgeb92cbe62014-09-15 15:53:09 +010098 mp_obj_t exc;
99 if (n_args == 0) {
100 exc = mp_obj_new_exception(&mp_type_SystemExit);
101 } else {
102 exc = mp_obj_new_exception_arg1(&mp_type_SystemExit, args[0]);
103 }
104 nlr_raise(exc);
105}
106MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
107
Damien George4b72b3a2016-01-03 14:21:40 +0000108STATIC mp_obj_t mp_sys_print_exception(size_t n_args, const mp_obj_t *args) {
Tom Collinsf06d0832017-01-12 16:08:51 -0800109 #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
Damien George999cedb2015-11-27 17:01:44 +0000110 void *stream_obj = &mp_sys_stdout_obj;
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200111 if (n_args > 1) {
Damien George999cedb2015-11-27 17:01:44 +0000112 stream_obj = MP_OBJ_TO_PTR(args[1]); // XXX may fail
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200113 }
114
Damien George999cedb2015-11-27 17:01:44 +0000115 mp_print_t print = {stream_obj, mp_stream_write_adaptor};
Damien George7f9d1d62015-04-09 23:56:15 +0100116 mp_obj_print_exception(&print, args[0]);
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200117 #else
Damien George3a2171e2015-09-04 16:53:46 +0100118 (void)n_args;
Damien George7f9d1d62015-04-09 23:56:15 +0100119 mp_obj_print_exception(&mp_plat_print, args[0]);
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200120 #endif
121
122 return mp_const_none;
123}
124MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_print_exception);
125
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300126#if MICROPY_PY_SYS_EXC_INFO
127STATIC mp_obj_t mp_sys_exc_info(void) {
Damien George999cedb2015-11-27 17:01:44 +0000128 mp_obj_t cur_exc = MP_OBJ_FROM_PTR(MP_STATE_VM(cur_exception));
129 mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300130
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;
Damien George999cedb2015-11-27 17:01:44 +0000135 return MP_OBJ_FROM_PTR(t);
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300136 }
137
Damien George999cedb2015-11-27 17:01:44 +0000138 t->items[0] = MP_OBJ_FROM_PTR(mp_obj_get_type(cur_exc));
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300139 t->items[1] = cur_exc;
140 t->items[2] = mp_const_none;
Damien George999cedb2015-11-27 17:01:44 +0000141 return MP_OBJ_FROM_PTR(t);
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300142}
143MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info);
144#endif
145
Paul Sokolovskybfc20922017-08-11 09:42:39 +0300146STATIC mp_obj_t mp_sys_getsizeof(mp_obj_t obj) {
147 return mp_unary_op(MP_UNARY_OP_SIZEOF, obj);
148}
149MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_getsizeof_obj, mp_sys_getsizeof);
150
Damien Georgecbf76742015-11-27 13:38:15 +0000151STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
152 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys) },
Paul Sokolovskyd99e9082014-05-10 16:50:45 +0300153
Damien Georgecbf76742015-11-27 13:38:15 +0000154 { MP_ROM_QSTR(MP_QSTR_path), MP_ROM_PTR(&MP_STATE_VM(mp_sys_path_obj)) },
155 { MP_ROM_QSTR(MP_QSTR_argv), MP_ROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)) },
156 { MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&version_obj) },
157 { MP_ROM_QSTR(MP_QSTR_version_info), MP_ROM_PTR(&mp_sys_version_info_obj) },
158 { MP_ROM_QSTR(MP_QSTR_implementation), MP_ROM_PTR(&mp_sys_implementation_obj) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200159 #ifdef MICROPY_PY_SYS_PLATFORM
Damien Georgecbf76742015-11-27 13:38:15 +0000160 { MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&platform_obj) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200161 #endif
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200162 #if MP_ENDIANNESS_LITTLE
Damien Georgecbf76742015-11-27 13:38:15 +0000163 { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_little) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200164 #else
Damien Georgecbf76742015-11-27 13:38:15 +0000165 { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_big) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200166 #endif
167
168 #if MICROPY_PY_SYS_MAXSIZE
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300169 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
Paul Sokolovsky96aa3a32017-03-06 12:15:25 +0100170 // Maximum mp_int_t value is not representable as small int, so we have
171 // little choice but to use MP_SMALL_INT_MAX. Apps also should be careful
172 // to not try to compare sys.maxsize to some literal number (as this
173 // number might not fit in available int size), but instead count number
174 // of "one" bits in sys.maxsize.
Damien Georgeb62bb532017-07-31 12:59:39 +1000175 { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_INT(MP_SMALL_INT_MAX) },
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300176 #else
Damien Georgecbf76742015-11-27 13:38:15 +0000177 { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_PTR(&mp_maxsize_obj) },
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300178 #endif
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200179 #endif
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300180
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200181 #if MICROPY_PY_SYS_EXIT
Damien Georgecbf76742015-11-27 13:38:15 +0000182 { MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&mp_sys_exit_obj) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200183 #endif
Damien George89755ae2014-05-11 17:35:43 +0100184
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200185 #if MICROPY_PY_SYS_STDFILES
Damien Georgecbf76742015-11-27 13:38:15 +0000186 { MP_ROM_QSTR(MP_QSTR_stdin), MP_ROM_PTR(&mp_sys_stdin_obj) },
187 { MP_ROM_QSTR(MP_QSTR_stdout), MP_ROM_PTR(&mp_sys_stdout_obj) },
188 { MP_ROM_QSTR(MP_QSTR_stderr), MP_ROM_PTR(&mp_sys_stderr_obj) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200189 #endif
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200190
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200191 #if MICROPY_PY_SYS_MODULES
Damien George3911d5a2015-12-16 19:40:14 -0500192 { MP_ROM_QSTR(MP_QSTR_modules), MP_ROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)) },
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200193 #endif
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300194 #if MICROPY_PY_SYS_EXC_INFO
Damien Georgecbf76742015-11-27 13:38:15 +0000195 { MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300196 #endif
Paul Sokolovskybfc20922017-08-11 09:42:39 +0300197 #if MICROPY_PY_SYS_GETSIZEOF
198 { MP_ROM_QSTR(MP_QSTR_getsizeof), MP_ROM_PTR(&mp_sys_getsizeof_obj) },
199 #endif
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300200
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200201 /*
202 * Extensions to CPython
203 */
204
Damien Georgecbf76742015-11-27 13:38:15 +0000205 { MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300206};
207
Damien George3b603f22014-11-29 14:39:27 +0000208STATIC MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table);
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300209
210const mp_obj_module_t mp_module_sys = {
211 .base = { &mp_type_module },
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300212 .globals = (mp_obj_dict_t*)&mp_module_sys_globals,
213};
214
215#endif