blob: b7d55fdcc66a8195afa84765266278706f8b9c5b [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 George30dd23a2014-08-10 17:50:28 +010045/// \module sys - system specific functions
46
Damien Georgecd342072015-01-12 22:30:49 +000047// defined per port; type of these is irrelevant, just need pointer
stijne50cff62015-04-09 12:27:15 +020048extern struct _mp_dummy_t mp_sys_stdin_obj;
49extern struct _mp_dummy_t mp_sys_stdout_obj;
50extern struct _mp_dummy_t mp_sys_stderr_obj;
Damien Georgecd342072015-01-12 22:30:49 +000051
Tom Collinsf06d0832017-01-12 16:08:51 -080052#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
Damien George999cedb2015-11-27 17:01:44 +000053const mp_print_t mp_sys_stdout_print = {&mp_sys_stdout_obj, mp_stream_write_adaptor};
Damien George5ae5ec92015-04-11 12:01:39 +010054#endif
55
Damien George30dd23a2014-08-10 17:50:28 +010056/// \constant version - Python language version that this implementation conforms to, as a string
57STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0");
58
Damien George4ef26c12014-08-10 17:53:43 +010059/// \constant version_info - Python language version that this implementation conforms to, as a tuple of ints
Paul Sokolovskybaaaf652014-04-13 09:46:58 +030060#define I(n) MP_OBJ_NEW_SMALL_INT(n)
61// TODO: CPython is now at 5-element array, but save 2 els so far...
Paul Sokolovskybbae42d2014-04-14 01:46:45 +030062STATIC 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 +000063
64// sys.implementation object
65// this holds the MicroPython version
66STATIC const mp_obj_tuple_t mp_sys_implementation_version_info_obj = {
67 {&mp_type_tuple},
68 3,
69 { I(MICROPY_VERSION_MAJOR), I(MICROPY_VERSION_MINOR), I(MICROPY_VERSION_MICRO) }
70};
71#if MICROPY_PY_ATTRTUPLE
72STATIC const qstr impl_fields[] = { MP_QSTR_name, MP_QSTR_version };
73STATIC MP_DEFINE_ATTRTUPLE(
74 mp_sys_implementation_obj,
75 impl_fields,
76 2,
Damien Georgecbf76742015-11-27 13:38:15 +000077 MP_ROM_QSTR(MP_QSTR_micropython),
78 MP_ROM_PTR(&mp_sys_implementation_version_info_obj)
Damien Georgec3184ae2015-04-21 14:45:04 +000079);
80#else
Damien Georgee1cda002017-06-08 00:41:27 +100081STATIC const mp_rom_obj_tuple_t mp_sys_implementation_obj = {
Damien Georgec3184ae2015-04-21 14:45:04 +000082 {&mp_type_tuple},
83 2,
84 {
Damien Georgee1cda002017-06-08 00:41:27 +100085 MP_ROM_QSTR(MP_QSTR_micropython),
86 MP_ROM_PTR(&mp_sys_implementation_version_info_obj),
Damien Georgec3184ae2015-04-21 14:45:04 +000087 }
88};
89#endif
90
Paul Sokolovskybaaaf652014-04-13 09:46:58 +030091#undef I
Damien George30dd23a2014-08-10 17:50:28 +010092
Paul Sokolovskyb9b93542014-06-07 23:40:04 +030093#ifdef MICROPY_PY_SYS_PLATFORM
Alexander Steffen55f33242017-06-30 09:22:17 +020094/// \constant platform - the platform that MicroPython is running on
Paul Sokolovskyb9b93542014-06-07 23:40:04 +030095STATIC const MP_DEFINE_STR_OBJ(platform_obj, MICROPY_PY_SYS_PLATFORM);
96#endif
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030097
Damien Georgeb92cbe62014-09-15 15:53:09 +010098/// \function exit([retval])
99/// Raise a `SystemExit` exception. If an argument is given, it is the
100/// value given to `SystemExit`.
Damien George4b72b3a2016-01-03 14:21:40 +0000101STATIC mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
Damien Georgeb92cbe62014-09-15 15:53:09 +0100102 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}
110MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
111
Damien George4b72b3a2016-01-03 14:21:40 +0000112STATIC mp_obj_t mp_sys_print_exception(size_t n_args, const mp_obj_t *args) {
Tom Collinsf06d0832017-01-12 16:08:51 -0800113 #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
Damien George999cedb2015-11-27 17:01:44 +0000114 void *stream_obj = &mp_sys_stdout_obj;
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200115 if (n_args > 1) {
Damien George999cedb2015-11-27 17:01:44 +0000116 stream_obj = MP_OBJ_TO_PTR(args[1]); // XXX may fail
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200117 }
118
Damien George999cedb2015-11-27 17:01:44 +0000119 mp_print_t print = {stream_obj, mp_stream_write_adaptor};
Damien George7f9d1d62015-04-09 23:56:15 +0100120 mp_obj_print_exception(&print, args[0]);
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200121 #else
Damien George3a2171e2015-09-04 16:53:46 +0100122 (void)n_args;
Damien George7f9d1d62015-04-09 23:56:15 +0100123 mp_obj_print_exception(&mp_plat_print, args[0]);
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200124 #endif
125
126 return mp_const_none;
127}
128MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_print_exception);
129
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300130#if MICROPY_PY_SYS_EXC_INFO
131STATIC mp_obj_t mp_sys_exc_info(void) {
Damien George999cedb2015-11-27 17:01:44 +0000132 mp_obj_t cur_exc = MP_OBJ_FROM_PTR(MP_STATE_VM(cur_exception));
133 mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300134
135 if (cur_exc == MP_OBJ_NULL) {
136 t->items[0] = mp_const_none;
137 t->items[1] = mp_const_none;
138 t->items[2] = mp_const_none;
Damien George999cedb2015-11-27 17:01:44 +0000139 return MP_OBJ_FROM_PTR(t);
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300140 }
141
Damien George999cedb2015-11-27 17:01:44 +0000142 t->items[0] = MP_OBJ_FROM_PTR(mp_obj_get_type(cur_exc));
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300143 t->items[1] = cur_exc;
144 t->items[2] = mp_const_none;
Damien George999cedb2015-11-27 17:01:44 +0000145 return MP_OBJ_FROM_PTR(t);
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300146}
147MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info);
148#endif
149
Paul Sokolovskybfc20922017-08-11 09:42:39 +0300150STATIC mp_obj_t mp_sys_getsizeof(mp_obj_t obj) {
151 return mp_unary_op(MP_UNARY_OP_SIZEOF, obj);
152}
153MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_getsizeof_obj, mp_sys_getsizeof);
154
Damien Georgecbf76742015-11-27 13:38:15 +0000155STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
156 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys) },
Paul Sokolovskyd99e9082014-05-10 16:50:45 +0300157
Damien Georgecbf76742015-11-27 13:38:15 +0000158 { MP_ROM_QSTR(MP_QSTR_path), MP_ROM_PTR(&MP_STATE_VM(mp_sys_path_obj)) },
159 { MP_ROM_QSTR(MP_QSTR_argv), MP_ROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)) },
160 { MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&version_obj) },
161 { MP_ROM_QSTR(MP_QSTR_version_info), MP_ROM_PTR(&mp_sys_version_info_obj) },
162 { MP_ROM_QSTR(MP_QSTR_implementation), MP_ROM_PTR(&mp_sys_implementation_obj) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200163 #ifdef MICROPY_PY_SYS_PLATFORM
Damien Georgecbf76742015-11-27 13:38:15 +0000164 { MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&platform_obj) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200165 #endif
Damien George30dd23a2014-08-10 17:50:28 +0100166 /// \constant byteorder - the byte order of the system ("little" or "big")
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200167 #if MP_ENDIANNESS_LITTLE
Damien Georgecbf76742015-11-27 13:38:15 +0000168 { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_little) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200169 #else
Damien Georgecbf76742015-11-27 13:38:15 +0000170 { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_big) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200171 #endif
172
173 #if MICROPY_PY_SYS_MAXSIZE
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300174 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
Paul Sokolovsky96aa3a32017-03-06 12:15:25 +0100175 // Maximum mp_int_t value is not representable as small int, so we have
176 // little choice but to use MP_SMALL_INT_MAX. Apps also should be careful
177 // to not try to compare sys.maxsize to some literal number (as this
178 // number might not fit in available int size), but instead count number
179 // of "one" bits in sys.maxsize.
Damien Georgeb62bb532017-07-31 12:59:39 +1000180 { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_INT(MP_SMALL_INT_MAX) },
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300181 #else
Damien Georgecbf76742015-11-27 13:38:15 +0000182 { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_PTR(&mp_maxsize_obj) },
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300183 #endif
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200184 #endif
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300185
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200186 #if MICROPY_PY_SYS_EXIT
Damien George30dd23a2014-08-10 17:50:28 +0100187 // documented per-port
Damien Georgecbf76742015-11-27 13:38:15 +0000188 { MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&mp_sys_exit_obj) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200189 #endif
Damien George89755ae2014-05-11 17:35:43 +0100190
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200191 #if MICROPY_PY_SYS_STDFILES
Damien George30dd23a2014-08-10 17:50:28 +0100192 // documented per-port
Damien Georgecbf76742015-11-27 13:38:15 +0000193 { MP_ROM_QSTR(MP_QSTR_stdin), MP_ROM_PTR(&mp_sys_stdin_obj) },
194 { MP_ROM_QSTR(MP_QSTR_stdout), MP_ROM_PTR(&mp_sys_stdout_obj) },
195 { MP_ROM_QSTR(MP_QSTR_stderr), MP_ROM_PTR(&mp_sys_stderr_obj) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200196 #endif
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200197
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200198 #if MICROPY_PY_SYS_MODULES
Damien George3911d5a2015-12-16 19:40:14 -0500199 { MP_ROM_QSTR(MP_QSTR_modules), MP_ROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)) },
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200200 #endif
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300201 #if MICROPY_PY_SYS_EXC_INFO
Damien Georgecbf76742015-11-27 13:38:15 +0000202 { MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300203 #endif
Paul Sokolovskybfc20922017-08-11 09:42:39 +0300204 #if MICROPY_PY_SYS_GETSIZEOF
205 { MP_ROM_QSTR(MP_QSTR_getsizeof), MP_ROM_PTR(&mp_sys_getsizeof_obj) },
206 #endif
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300207
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200208 /*
209 * Extensions to CPython
210 */
211
Damien Georgecbf76742015-11-27 13:38:15 +0000212 { MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300213};
214
Damien George3b603f22014-11-29 14:39:27 +0000215STATIC MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table);
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300216
217const mp_obj_module_t mp_module_sys = {
218 .base = { &mp_type_module },
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300219 .globals = (mp_obj_dict_t*)&mp_module_sys_globals,
220};
221
222#endif