blob: ee6f8686e014cd5e3fef60660226c09551df11f7 [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
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien Georgee1e359f2015-02-07 17:24:10 +000027#include "py/mpstate.h"
Damien George51dfcb42015-01-01 20:27:54 +000028#include "py/nlr.h"
29#include "py/builtin.h"
30#include "py/objlist.h"
31#include "py/objtuple.h"
32#include "py/objstr.h"
33#include "py/objint.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 Sokolovsky5500cde2014-04-13 06:43:18 +030036
Damien Georgeee3fd462014-05-24 23:03:12 +010037#if MICROPY_PY_SYS
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030038
Damien George95f53462015-04-22 17:38:05 +010039#include "genhdr/mpversion.h"
Damien Georgec3184ae2015-04-21 14:45:04 +000040
Damien George30dd23a2014-08-10 17:50:28 +010041/// \module sys - system specific functions
42
Damien Georgecd342072015-01-12 22:30:49 +000043// defined per port; type of these is irrelevant, just need pointer
stijne50cff62015-04-09 12:27:15 +020044extern struct _mp_dummy_t mp_sys_stdin_obj;
45extern struct _mp_dummy_t mp_sys_stdout_obj;
46extern struct _mp_dummy_t mp_sys_stderr_obj;
Damien Georgecd342072015-01-12 22:30:49 +000047
Tom Collinsf06d0832017-01-12 16:08:51 -080048#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
Damien George999cedb2015-11-27 17:01:44 +000049const mp_print_t mp_sys_stdout_print = {&mp_sys_stdout_obj, mp_stream_write_adaptor};
Damien George5ae5ec92015-04-11 12:01:39 +010050#endif
51
Damien George30dd23a2014-08-10 17:50:28 +010052/// \constant version - Python language version that this implementation conforms to, as a string
53STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0");
54
Damien George4ef26c12014-08-10 17:53:43 +010055/// \constant version_info - Python language version that this implementation conforms to, as a tuple of ints
Paul Sokolovskybaaaf652014-04-13 09:46:58 +030056#define I(n) MP_OBJ_NEW_SMALL_INT(n)
57// TODO: CPython is now at 5-element array, but save 2 els so far...
Paul Sokolovskybbae42d2014-04-14 01:46:45 +030058STATIC 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 +000059
60// sys.implementation object
61// this holds the MicroPython version
62STATIC const mp_obj_tuple_t mp_sys_implementation_version_info_obj = {
63 {&mp_type_tuple},
64 3,
65 { I(MICROPY_VERSION_MAJOR), I(MICROPY_VERSION_MINOR), I(MICROPY_VERSION_MICRO) }
66};
67#if MICROPY_PY_ATTRTUPLE
68STATIC const qstr impl_fields[] = { MP_QSTR_name, MP_QSTR_version };
69STATIC MP_DEFINE_ATTRTUPLE(
70 mp_sys_implementation_obj,
71 impl_fields,
72 2,
Damien Georgecbf76742015-11-27 13:38:15 +000073 MP_ROM_QSTR(MP_QSTR_micropython),
74 MP_ROM_PTR(&mp_sys_implementation_version_info_obj)
Damien Georgec3184ae2015-04-21 14:45:04 +000075);
76#else
Damien Georgee1cda002017-06-08 00:41:27 +100077STATIC const mp_rom_obj_tuple_t mp_sys_implementation_obj = {
Damien Georgec3184ae2015-04-21 14:45:04 +000078 {&mp_type_tuple},
79 2,
80 {
Damien Georgee1cda002017-06-08 00:41:27 +100081 MP_ROM_QSTR(MP_QSTR_micropython),
82 MP_ROM_PTR(&mp_sys_implementation_version_info_obj),
Damien Georgec3184ae2015-04-21 14:45:04 +000083 }
84};
85#endif
86
Paul Sokolovskybaaaf652014-04-13 09:46:58 +030087#undef I
Damien George30dd23a2014-08-10 17:50:28 +010088
Paul Sokolovskyb9b93542014-06-07 23:40:04 +030089#ifdef MICROPY_PY_SYS_PLATFORM
Alexander Steffen55f33242017-06-30 09:22:17 +020090/// \constant platform - the platform that MicroPython is running on
Paul Sokolovskyb9b93542014-06-07 23:40:04 +030091STATIC const MP_DEFINE_STR_OBJ(platform_obj, MICROPY_PY_SYS_PLATFORM);
92#endif
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030093
Damien Georgeb92cbe62014-09-15 15:53:09 +010094/// \function exit([retval])
95/// Raise a `SystemExit` exception. If an argument is given, it is the
96/// value given to `SystemExit`.
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
Damien Georgecbf76742015-11-27 13:38:15 +0000146STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
147 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys) },
Paul Sokolovskyd99e9082014-05-10 16:50:45 +0300148
Damien Georgecbf76742015-11-27 13:38:15 +0000149 { MP_ROM_QSTR(MP_QSTR_path), MP_ROM_PTR(&MP_STATE_VM(mp_sys_path_obj)) },
150 { MP_ROM_QSTR(MP_QSTR_argv), MP_ROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)) },
151 { MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&version_obj) },
152 { MP_ROM_QSTR(MP_QSTR_version_info), MP_ROM_PTR(&mp_sys_version_info_obj) },
153 { MP_ROM_QSTR(MP_QSTR_implementation), MP_ROM_PTR(&mp_sys_implementation_obj) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200154 #ifdef MICROPY_PY_SYS_PLATFORM
Damien Georgecbf76742015-11-27 13:38:15 +0000155 { MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&platform_obj) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200156 #endif
Damien George30dd23a2014-08-10 17:50:28 +0100157 /// \constant byteorder - the byte order of the system ("little" or "big")
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200158 #if MP_ENDIANNESS_LITTLE
Damien Georgecbf76742015-11-27 13:38:15 +0000159 { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_little) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200160 #else
Damien Georgecbf76742015-11-27 13:38:15 +0000161 { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_big) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200162 #endif
163
164 #if MICROPY_PY_SYS_MAXSIZE
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300165 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
Paul Sokolovsky96aa3a32017-03-06 12:15:25 +0100166 // Maximum mp_int_t value is not representable as small int, so we have
167 // little choice but to use MP_SMALL_INT_MAX. Apps also should be careful
168 // to not try to compare sys.maxsize to some literal number (as this
169 // number might not fit in available int size), but instead count number
170 // of "one" bits in sys.maxsize.
Damien Georgeb62bb532017-07-31 12:59:39 +1000171 { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_INT(MP_SMALL_INT_MAX) },
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300172 #else
Damien Georgecbf76742015-11-27 13:38:15 +0000173 { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_PTR(&mp_maxsize_obj) },
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300174 #endif
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200175 #endif
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +0300176
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200177 #if MICROPY_PY_SYS_EXIT
Damien George30dd23a2014-08-10 17:50:28 +0100178 // documented per-port
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 George30dd23a2014-08-10 17:50:28 +0100183 // documented per-port
Damien Georgecbf76742015-11-27 13:38:15 +0000184 { MP_ROM_QSTR(MP_QSTR_stdin), MP_ROM_PTR(&mp_sys_stdin_obj) },
185 { MP_ROM_QSTR(MP_QSTR_stdout), MP_ROM_PTR(&mp_sys_stdout_obj) },
186 { MP_ROM_QSTR(MP_QSTR_stderr), MP_ROM_PTR(&mp_sys_stderr_obj) },
Paul Sokolovsky72bd1722015-11-21 15:32:00 +0200187 #endif
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200188
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200189 #if MICROPY_PY_SYS_MODULES
Damien George3911d5a2015-12-16 19:40:14 -0500190 { MP_ROM_QSTR(MP_QSTR_modules), MP_ROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)) },
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200191 #endif
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300192 #if MICROPY_PY_SYS_EXC_INFO
Damien Georgecbf76742015-11-27 13:38:15 +0000193 { MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300194 #endif
195
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200196 /*
197 * Extensions to CPython
198 */
199
Damien Georgecbf76742015-11-27 13:38:15 +0000200 { MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300201};
202
Damien George3b603f22014-11-29 14:39:27 +0000203STATIC MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table);
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300204
205const mp_obj_module_t mp_module_sys = {
206 .base = { &mp_type_module },
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300207 .globals = (mp_obj_dict_t*)&mp_module_sys_globals,
208};
209
210#endif