Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 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 | |
Paul Sokolovsky | 4e0eeeb | 2014-07-03 16:50:11 +0300 | [diff] [blame] | 27 | #include <stdint.h> |
Damien George | b92cbe6 | 2014-09-15 15:53:09 +0100 | [diff] [blame] | 28 | |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 29 | #include "mpconfig.h" |
Damien George | b92cbe6 | 2014-09-15 15:53:09 +0100 | [diff] [blame] | 30 | #include "nlr.h" |
Paul Sokolovsky | 59c675a | 2014-06-21 22:43:22 +0300 | [diff] [blame] | 31 | #include "misc.h" |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 32 | #include "qstr.h" |
| 33 | #include "obj.h" |
| 34 | #include "builtin.h" |
| 35 | #include "runtime.h" |
| 36 | #include "objlist.h" |
Paul Sokolovsky | baaaf65 | 2014-04-13 09:46:58 +0300 | [diff] [blame] | 37 | #include "objtuple.h" |
Paul Sokolovsky | bbae42d | 2014-04-14 01:46:45 +0300 | [diff] [blame] | 38 | #include "objstr.h" |
Paul Sokolovsky | 4e0eeeb | 2014-07-03 16:50:11 +0300 | [diff] [blame] | 39 | #include "mpz.h" |
| 40 | #include "objint.h" |
Paul Sokolovsky | 46c3ab2 | 2014-12-06 14:29:09 +0200 | [diff] [blame^] | 41 | #include "pfenv.h" |
| 42 | #include "stream.h" |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 43 | |
Damien George | ee3fd46 | 2014-05-24 23:03:12 +0100 | [diff] [blame] | 44 | #if MICROPY_PY_SYS |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 45 | |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 46 | /// \module sys - system specific functions |
| 47 | |
Damien George | b92cbe6 | 2014-09-15 15:53:09 +0100 | [diff] [blame] | 48 | // These two lists must be initialised per port (after the call to mp_init). |
Damien George | 4ef26c1 | 2014-08-10 17:53:43 +0100 | [diff] [blame] | 49 | // TODO document these properly, they aren't constants or functions... |
| 50 | /// \constant path - a mutable list of directories to search for imported modules |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 51 | mp_obj_list_t mp_sys_path_obj; |
Damien George | 4ef26c1 | 2014-08-10 17:53:43 +0100 | [diff] [blame] | 52 | /// \constant argv - a mutable list of arguments this program started with |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 53 | mp_obj_list_t mp_sys_argv_obj; |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 54 | |
| 55 | /// \constant version - Python language version that this implementation conforms to, as a string |
| 56 | STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0"); |
| 57 | |
Damien George | 4ef26c1 | 2014-08-10 17:53:43 +0100 | [diff] [blame] | 58 | /// \constant version_info - Python language version that this implementation conforms to, as a tuple of ints |
Paul Sokolovsky | baaaf65 | 2014-04-13 09:46:58 +0300 | [diff] [blame] | 59 | #define I(n) MP_OBJ_NEW_SMALL_INT(n) |
| 60 | // TODO: CPython is now at 5-element array, but save 2 els so far... |
Paul Sokolovsky | bbae42d | 2014-04-14 01:46:45 +0300 | [diff] [blame] | 61 | STATIC const mp_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {I(3), I(4), I(0)}}; |
Paul Sokolovsky | baaaf65 | 2014-04-13 09:46:58 +0300 | [diff] [blame] | 62 | #undef I |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 63 | |
Paul Sokolovsky | b9b9354 | 2014-06-07 23:40:04 +0300 | [diff] [blame] | 64 | #ifdef MICROPY_PY_SYS_PLATFORM |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 65 | /// \constant platform - the platform that Micro Python is running on |
Paul Sokolovsky | b9b9354 | 2014-06-07 23:40:04 +0300 | [diff] [blame] | 66 | STATIC const MP_DEFINE_STR_OBJ(platform_obj, MICROPY_PY_SYS_PLATFORM); |
| 67 | #endif |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 68 | |
Damien George | b92cbe6 | 2014-09-15 15:53:09 +0100 | [diff] [blame] | 69 | /// \function exit([retval]) |
| 70 | /// Raise a `SystemExit` exception. If an argument is given, it is the |
| 71 | /// value given to `SystemExit`. |
| 72 | STATIC mp_obj_t mp_sys_exit(mp_uint_t n_args, const mp_obj_t *args) { |
| 73 | mp_obj_t exc; |
| 74 | if (n_args == 0) { |
| 75 | exc = mp_obj_new_exception(&mp_type_SystemExit); |
| 76 | } else { |
| 77 | exc = mp_obj_new_exception_arg1(&mp_type_SystemExit, args[0]); |
| 78 | } |
| 79 | nlr_raise(exc); |
| 80 | } |
| 81 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit); |
| 82 | |
Paul Sokolovsky | 46c3ab2 | 2014-12-06 14:29:09 +0200 | [diff] [blame^] | 83 | STATIC mp_obj_t mp_sys_print_exception(mp_uint_t n_args, const mp_obj_t *args) { |
| 84 | #if MICROPY_PY_IO |
| 85 | mp_obj_t stream_obj = &mp_sys_stdout_obj; |
| 86 | if (n_args > 1) { |
| 87 | stream_obj = args[1]; |
| 88 | } |
| 89 | |
| 90 | pfenv_t pfenv; |
| 91 | pfenv.data = stream_obj; |
| 92 | pfenv.print_strn = (void (*)(void *, const char *, mp_uint_t))mp_stream_write; |
| 93 | mp_obj_print_exception((void (*)(void *env, const char *fmt, ...))pfenv_printf, &pfenv, args[0]); |
| 94 | #else |
| 95 | mp_obj_print_exception(printf_wrapper, NULL, args[0]); |
| 96 | #endif |
| 97 | |
| 98 | return mp_const_none; |
| 99 | } |
| 100 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_print_exception); |
| 101 | |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 102 | STATIC const mp_map_elem_t mp_module_sys_globals_table[] = { |
| 103 | { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_sys) }, |
Paul Sokolovsky | d99e908 | 2014-05-10 16:50:45 +0300 | [diff] [blame] | 104 | |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 105 | { MP_OBJ_NEW_QSTR(MP_QSTR_path), (mp_obj_t)&mp_sys_path_obj }, |
| 106 | { MP_OBJ_NEW_QSTR(MP_QSTR_argv), (mp_obj_t)&mp_sys_argv_obj }, |
Paul Sokolovsky | bbae42d | 2014-04-14 01:46:45 +0300 | [diff] [blame] | 107 | { MP_OBJ_NEW_QSTR(MP_QSTR_version), (mp_obj_t)&version_obj }, |
Paul Sokolovsky | baaaf65 | 2014-04-13 09:46:58 +0300 | [diff] [blame] | 108 | { MP_OBJ_NEW_QSTR(MP_QSTR_version_info), (mp_obj_t)&mp_sys_version_info_obj }, |
Paul Sokolovsky | b9b9354 | 2014-06-07 23:40:04 +0300 | [diff] [blame] | 109 | #ifdef MICROPY_PY_SYS_PLATFORM |
| 110 | { MP_OBJ_NEW_QSTR(MP_QSTR_platform), (mp_obj_t)&platform_obj }, |
| 111 | #endif |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 112 | /// \constant byteorder - the byte order of the system ("little" or "big") |
Paul Sokolovsky | 978d2c0 | 2014-04-13 09:53:52 +0300 | [diff] [blame] | 113 | #if MP_ENDIANNESS_LITTLE |
| 114 | { MP_OBJ_NEW_QSTR(MP_QSTR_byteorder), MP_OBJ_NEW_QSTR(MP_QSTR_little) }, |
| 115 | #else |
| 116 | { MP_OBJ_NEW_QSTR(MP_QSTR_byteorder), MP_OBJ_NEW_QSTR(MP_QSTR_big) }, |
| 117 | #endif |
Paul Sokolovsky | 4e0eeeb | 2014-07-03 16:50:11 +0300 | [diff] [blame] | 118 | #if MICROPY_PY_SYS_MAXSIZE |
| 119 | #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE |
| 120 | // INT_MAX is not representable as small int, as we know that small int |
| 121 | // takes one bit for tag. So, we have little choice but to provide this |
| 122 | // value. Apps also should be careful to not try to compare sys.maxsize |
| 123 | // with some number (which may not fit in available int size), but instead |
| 124 | // count number of significant bits in sys.maxsize. |
| 125 | { MP_OBJ_NEW_QSTR(MP_QSTR_maxsize), MP_OBJ_NEW_SMALL_INT(INT_MAX >> 1) }, |
| 126 | #else |
| 127 | { MP_OBJ_NEW_QSTR(MP_QSTR_maxsize), (mp_obj_t)&mp_maxsize_obj }, |
| 128 | #endif |
| 129 | #endif |
| 130 | |
Damien George | ee3fd46 | 2014-05-24 23:03:12 +0100 | [diff] [blame] | 131 | #if MICROPY_PY_SYS_EXIT |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 132 | // documented per-port |
Damien George | 89755ae | 2014-05-11 17:35:43 +0100 | [diff] [blame] | 133 | { MP_OBJ_NEW_QSTR(MP_QSTR_exit), (mp_obj_t)&mp_sys_exit_obj }, |
| 134 | #endif |
| 135 | |
Damien George | ee3fd46 | 2014-05-24 23:03:12 +0100 | [diff] [blame] | 136 | #if MICROPY_PY_SYS_STDFILES |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 137 | // documented per-port |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 138 | { MP_OBJ_NEW_QSTR(MP_QSTR_stdin), (mp_obj_t)&mp_sys_stdin_obj }, |
| 139 | { MP_OBJ_NEW_QSTR(MP_QSTR_stdout), (mp_obj_t)&mp_sys_stdout_obj }, |
| 140 | { MP_OBJ_NEW_QSTR(MP_QSTR_stderr), (mp_obj_t)&mp_sys_stderr_obj }, |
Paul Sokolovsky | 4165cd1 | 2014-04-13 07:00:37 +0300 | [diff] [blame] | 141 | #endif |
Paul Sokolovsky | 46c3ab2 | 2014-12-06 14:29:09 +0200 | [diff] [blame^] | 142 | |
| 143 | /* |
| 144 | * Extensions to CPython |
| 145 | */ |
| 146 | |
| 147 | { MP_OBJ_NEW_QSTR(MP_QSTR_print_exception), (mp_obj_t)&mp_sys_print_exception_obj }, |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 148 | }; |
| 149 | |
Damien George | 3b603f2 | 2014-11-29 14:39:27 +0000 | [diff] [blame] | 150 | STATIC MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table); |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 151 | |
| 152 | const mp_obj_module_t mp_module_sys = { |
| 153 | .base = { &mp_type_module }, |
| 154 | .name = MP_QSTR_sys, |
| 155 | .globals = (mp_obj_dict_t*)&mp_module_sys_globals, |
| 156 | }; |
| 157 | |
| 158 | #endif |