Numworks Epsilon  1.4.1
Graphing Calculator Operating System
modsys.c
Go to the documentation of this file.
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 
38 #if MICROPY_PY_SYS
39 
40 #include "genhdr/mpversion.h"
41 
42 // defined per port; type of these is irrelevant, just need pointer
43 extern struct _mp_dummy_t mp_sys_stdin_obj;
44 extern struct _mp_dummy_t mp_sys_stdout_obj;
45 extern struct _mp_dummy_t mp_sys_stderr_obj;
46 
47 #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
48 const mp_print_t mp_sys_stdout_print = {&mp_sys_stdout_obj, mp_stream_write_adaptor};
49 #endif
50 
51 // version - Python language version that this implementation conforms to, as a string
52 STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0");
53 
54 // version_info - Python language version that this implementation conforms to, as a tuple of ints
55 #define I(n) MP_OBJ_NEW_SMALL_INT(n)
56 // TODO: CPython is now at 5-element array, but save 2 els so far...
57 STATIC const mp_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {I(3), I(4), I(0)}};
58 
59 // sys.implementation object
60 // this holds the MicroPython version
61 STATIC const mp_obj_tuple_t mp_sys_implementation_version_info_obj = {
62  {&mp_type_tuple},
63  3,
65 };
66 #if MICROPY_PY_ATTRTUPLE
67 STATIC const qstr impl_fields[] = { MP_QSTR_name, MP_QSTR_version };
69  mp_sys_implementation_obj,
70  impl_fields,
71  2,
72  MP_ROM_QSTR(MP_QSTR_micropython),
73  MP_ROM_PTR(&mp_sys_implementation_version_info_obj)
74 );
75 #else
76 STATIC const mp_rom_obj_tuple_t mp_sys_implementation_obj = {
77  {&mp_type_tuple},
78  2,
79  {
80  MP_ROM_QSTR(MP_QSTR_micropython),
81  MP_ROM_PTR(&mp_sys_implementation_version_info_obj),
82  }
83 };
84 #endif
85 
86 #undef I
87 
88 #ifdef MICROPY_PY_SYS_PLATFORM
89 // platform - the platform that MicroPython is running on
90 STATIC const MP_DEFINE_STR_OBJ(platform_obj, MICROPY_PY_SYS_PLATFORM);
91 #endif
92 
93 // exit([retval]): raise SystemExit, with optional argument given to the exception
94 STATIC mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
95  mp_obj_t exc;
96  if (n_args == 0) {
98  } else {
100  }
101  nlr_raise(exc);
102 }
103 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
104 
105 STATIC mp_obj_t mp_sys_print_exception(size_t n_args, const mp_obj_t *args) {
106  #if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
107  void *stream_obj = &mp_sys_stdout_obj;
108  if (n_args > 1) {
109  stream_obj = MP_OBJ_TO_PTR(args[1]); // XXX may fail
110  }
111 
112  mp_print_t print = {stream_obj, mp_stream_write_adaptor};
113  mp_obj_print_exception(&print, args[0]);
114  #else
115  (void)n_args;
117  #endif
118 
119  return mp_const_none;
120 }
121 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_print_exception);
122 
123 #if MICROPY_PY_SYS_EXC_INFO
124 STATIC mp_obj_t mp_sys_exc_info(void) {
125  mp_obj_t cur_exc = MP_OBJ_FROM_PTR(MP_STATE_VM(cur_exception));
127 
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;
132  return MP_OBJ_FROM_PTR(t);
133  }
134 
135  t->items[0] = MP_OBJ_FROM_PTR(mp_obj_get_type(cur_exc));
136  t->items[1] = cur_exc;
137  t->items[2] = mp_const_none;
138  return MP_OBJ_FROM_PTR(t);
139 }
140 MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info);
141 #endif
142 
143 STATIC mp_obj_t mp_sys_getsizeof(mp_obj_t obj) {
144  return mp_unary_op(MP_UNARY_OP_SIZEOF, obj);
145 }
146 MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_getsizeof_obj, mp_sys_getsizeof);
147 
148 STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
149  { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys) },
150 
151  { 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) },
156  #ifdef MICROPY_PY_SYS_PLATFORM
157  { MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&platform_obj) },
158  #endif
159  #if MP_ENDIANNESS_LITTLE
160  { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_little) },
161  #else
162  { MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_big) },
163  #endif
164 
165  #if MICROPY_PY_SYS_MAXSIZE
166  #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
167  // 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.
172  { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_INT(MP_SMALL_INT_MAX) },
173  #else
174  { MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_PTR(&mp_maxsize_obj) },
175  #endif
176  #endif
177 
178  #if MICROPY_PY_SYS_EXIT
179  { MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&mp_sys_exit_obj) },
180  #endif
181 
182  #if MICROPY_PY_SYS_STDFILES
183  { 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) },
186  #endif
187 
188  #if MICROPY_PY_SYS_MODULES
189  { MP_ROM_QSTR(MP_QSTR_modules), MP_ROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)) },
190  #endif
191  #if MICROPY_PY_SYS_EXC_INFO
192  { MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
193  #endif
194  #if MICROPY_PY_SYS_GETSIZEOF
195  { MP_ROM_QSTR(MP_QSTR_getsizeof), MP_ROM_PTR(&mp_sys_getsizeof_obj) },
196  #endif
197 
198  /*
199  * Extensions to CPython
200  */
201 
202  { MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
203 };
204 
205 STATIC MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table);
206 
208  .base = { &mp_type_module },
209  .globals = (mp_obj_dict_t*)&mp_module_sys_globals,
210 };
211 
212 #endif
const mp_print_t mp_plat_print
Definition: mpprint.c:51
mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg)
Definition: runtime.c:216
#define MP_SMALL_INT_MAX
Definition: smallint.h:62
#define MP_ROM_INT(i)
Definition: obj.h:240
mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items)
Definition: objtuple.c:235
const mp_obj_type_t mp_type_SystemExit
#define mp_const_none
Definition: obj.h:614
#define MP_DEFINE_CONST_DICT(dict_name, table_name)
Definition: obj.h:317
#define MICROPY_VERSION_MAJOR
Definition: mpversion.h:1
mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type)
Definition: objexcept.c:329
mp_obj_type_t * mp_obj_get_type(mp_const_obj_t o_in)
Definition: obj.c:40
#define MP_ROM_QSTR(q)
Definition: obj.h:241
#define MP_OBJ_FROM_PTR(p)
Definition: obj.h:233
#define MP_ROM_PTR(p)
Definition: obj.h:242
mp_obj_base_t base
Definition: obj.h:814
#define MP_STATE_VM(x)
Definition: mpstate.h:241
#define STATIC
Definition: mpconfig.h:1178
mp_obj_t items[]
Definition: objtuple.h:34
#define MICROPY_VERSION_MINOR
Definition: mpversion.h:2
#define MP_DEFINE_STR_OBJ(obj_name, str)
Definition: objstr.h:39
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name)
Definition: obj.h:285
#define MP_DEFINE_ATTRTUPLE(tuple_obj_name, fields, nitems,...)
Definition: objtuple.h:51
#define NULL
Definition: stddef.h:4
#define MP_OBJ_NULL
Definition: obj.h:73
mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg)
Definition: objexcept.c:334
const mp_obj_module_t mp_module_sys
size_t qstr
Definition: qstr.h:48
args
Definition: i18n.py:175
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name)
Definition: obj.h:297
void mp_stream_write_adaptor(void *self, const char *buf, size_t len)
Definition: stream.c:262
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name)
Definition: obj.h:282
#define MICROPY_VERSION_MICRO
Definition: mpversion.h:3
#define MP_OBJ_TO_PTR(o)
Definition: obj.h:228
const mp_obj_type_t mp_type_tuple
Definition: objtuple.c:220
const mp_obj_int_t mp_maxsize_obj
const mp_obj_type_t mp_type_module
Definition: objmodule.c:94
#define nlr_raise(val)
Definition: nlr.h:89
uint64_t mp_obj_t
Definition: obj.h:39
void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc)
Definition: obj.c:81