blob: a1a9265dbd152014ef809b4b1c5c50187a58c987 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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 Sokolovsky4e0eeeb2014-07-03 16:50:11 +030027#include <stdint.h>
28#include <limits.h>
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030029#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030030#include "misc.h"
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030031#include "qstr.h"
32#include "obj.h"
33#include "builtin.h"
34#include "runtime.h"
35#include "objlist.h"
Paul Sokolovskybaaaf652014-04-13 09:46:58 +030036#include "objtuple.h"
Paul Sokolovskybbae42d2014-04-14 01:46:45 +030037#include "objstr.h"
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030038#include "mpz.h"
39#include "objint.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 George30dd23a2014-08-10 17:50:28 +010043/// \module sys - system specific functions
44
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030045// These should be implemented by ports, specific types don't matter,
46// only addresses.
47struct _dummy_t;
Damien George89755ae2014-05-11 17:35:43 +010048extern struct _dummy_t mp_sys_exit_obj;
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030049
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030050extern mp_obj_int_t mp_maxsize_obj;
51
Damien George30dd23a2014-08-10 17:50:28 +010052// TODO document these, they aren't constants or functions...
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030053mp_obj_list_t mp_sys_path_obj;
54mp_obj_list_t mp_sys_argv_obj;
Damien George30dd23a2014-08-10 17:50:28 +010055
56/// \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
59/// \constant version - 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)}};
Paul Sokolovskybaaaf652014-04-13 09:46:58 +030063#undef I
Damien George30dd23a2014-08-10 17:50:28 +010064
Paul Sokolovskyb9b93542014-06-07 23:40:04 +030065#ifdef MICROPY_PY_SYS_PLATFORM
Damien George30dd23a2014-08-10 17:50:28 +010066/// \constant platform - the platform that Micro Python is running on
Paul Sokolovskyb9b93542014-06-07 23:40:04 +030067STATIC const MP_DEFINE_STR_OBJ(platform_obj, MICROPY_PY_SYS_PLATFORM);
68#endif
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030069
70STATIC const mp_map_elem_t mp_module_sys_globals_table[] = {
71 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_sys) },
Paul Sokolovskyd99e9082014-05-10 16:50:45 +030072
Paul Sokolovsky5500cde2014-04-13 06:43:18 +030073 { MP_OBJ_NEW_QSTR(MP_QSTR_path), (mp_obj_t)&mp_sys_path_obj },
74 { MP_OBJ_NEW_QSTR(MP_QSTR_argv), (mp_obj_t)&mp_sys_argv_obj },
Paul Sokolovskybbae42d2014-04-14 01:46:45 +030075 { MP_OBJ_NEW_QSTR(MP_QSTR_version), (mp_obj_t)&version_obj },
Paul Sokolovskybaaaf652014-04-13 09:46:58 +030076 { MP_OBJ_NEW_QSTR(MP_QSTR_version_info), (mp_obj_t)&mp_sys_version_info_obj },
Paul Sokolovskyb9b93542014-06-07 23:40:04 +030077#ifdef MICROPY_PY_SYS_PLATFORM
78 { MP_OBJ_NEW_QSTR(MP_QSTR_platform), (mp_obj_t)&platform_obj },
79#endif
Damien George30dd23a2014-08-10 17:50:28 +010080 /// \constant byteorder - the byte order of the system ("little" or "big")
Paul Sokolovsky978d2c02014-04-13 09:53:52 +030081#if MP_ENDIANNESS_LITTLE
82 { MP_OBJ_NEW_QSTR(MP_QSTR_byteorder), MP_OBJ_NEW_QSTR(MP_QSTR_little) },
83#else
84 { MP_OBJ_NEW_QSTR(MP_QSTR_byteorder), MP_OBJ_NEW_QSTR(MP_QSTR_big) },
85#endif
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030086#if MICROPY_PY_SYS_MAXSIZE
87 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
88 // INT_MAX is not representable as small int, as we know that small int
89 // takes one bit for tag. So, we have little choice but to provide this
90 // value. Apps also should be careful to not try to compare sys.maxsize
91 // with some number (which may not fit in available int size), but instead
92 // count number of significant bits in sys.maxsize.
93 { MP_OBJ_NEW_QSTR(MP_QSTR_maxsize), MP_OBJ_NEW_SMALL_INT(INT_MAX >> 1) },
94 #else
95 { MP_OBJ_NEW_QSTR(MP_QSTR_maxsize), (mp_obj_t)&mp_maxsize_obj },
96 #endif
97#endif
98
Damien Georgeee3fd462014-05-24 23:03:12 +010099#if MICROPY_PY_SYS_EXIT
Damien George30dd23a2014-08-10 17:50:28 +0100100 // documented per-port
Damien George89755ae2014-05-11 17:35:43 +0100101 { MP_OBJ_NEW_QSTR(MP_QSTR_exit), (mp_obj_t)&mp_sys_exit_obj },
102#endif
103
Damien Georgeee3fd462014-05-24 23:03:12 +0100104#if MICROPY_PY_SYS_STDFILES
Damien George30dd23a2014-08-10 17:50:28 +0100105 // documented per-port
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300106 { MP_OBJ_NEW_QSTR(MP_QSTR_stdin), (mp_obj_t)&mp_sys_stdin_obj },
107 { MP_OBJ_NEW_QSTR(MP_QSTR_stdout), (mp_obj_t)&mp_sys_stdout_obj },
108 { MP_OBJ_NEW_QSTR(MP_QSTR_stderr), (mp_obj_t)&mp_sys_stderr_obj },
Paul Sokolovsky4165cd12014-04-13 07:00:37 +0300109#endif
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300110};
111
112STATIC const mp_obj_dict_t mp_module_sys_globals = {
113 .base = {&mp_type_dict},
114 .map = {
115 .all_keys_are_qstrs = 1,
116 .table_is_fixed_array = 1,
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200117 .used = MP_ARRAY_SIZE(mp_module_sys_globals_table),
118 .alloc = MP_ARRAY_SIZE(mp_module_sys_globals_table),
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300119 .table = (mp_map_elem_t*)mp_module_sys_globals_table,
120 },
121};
122
123const mp_obj_module_t mp_module_sys = {
124 .base = { &mp_type_module },
125 .name = MP_QSTR_sys,
126 .globals = (mp_obj_dict_t*)&mp_module_sys_globals,
127};
128
129#endif