2014-05-03 23:27:38 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
2017-08-11 07:42:39 +01:00
|
|
|
* Copyright (c) 2014-2017 Paul Sokolovsky
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/builtin.h"
|
|
|
|
#include "py/objlist.h"
|
2021-07-26 15:39:04 +01:00
|
|
|
#include "py/objmodule.h"
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/objtuple.h"
|
|
|
|
#include "py/objstr.h"
|
|
|
|
#include "py/objint.h"
|
2017-08-11 07:42:39 +01:00
|
|
|
#include "py/objtype.h"
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/stream.h"
|
2017-03-06 11:15:25 +00:00
|
|
|
#include "py/smallint.h"
|
2017-08-11 07:42:39 +01:00
|
|
|
#include "py/runtime.h"
|
2019-10-30 05:26:11 +00:00
|
|
|
#include "py/persistentcode.h"
|
2022-08-18 06:07:24 +01:00
|
|
|
#include "extmod/modplatform.h"
|
py/modsys: Append MicroPython git version and build date to sys.version.
This commit adds the git hash and build date to sys.version. This is
allowed according to CPython docs, and is what PyPy does. The docs state:
A string containing the version number of the Python interpreter plus
additional information on the build number and compiler used.
Eg on CPython:
Python 3.10.4 (main, Mar 23 2022, 23:05:40) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.version
'3.10.4 (main, Mar 23 2022, 23:05:40) [GCC 11.2.0]'
and PyPy:
Python 2.7.12 (5.6.0+dfsg-4, Nov 20 2016, 10:43:30)
[PyPy 5.6.0 with GCC 6.2.0 20161109] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> import sys
>>>> sys.version
'2.7.12 (5.6.0+dfsg-4, Nov 20 2016, 10:43:30)\n[PyPy 5.6.0 with GCC ...
With this commit on MicroPython we now have:
MicroPython v1.18-371-g9d08eb024 on 2022-04-28; linux [GCC 11.2.0] v...
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import sys
>>> sys.version
'3.4.0; MicroPython v1.18-371-g9d08eb024 on 2022-04-28'
Note that the start of the banner is the same as the end of sys.version.
This helps to keep code size under control because the string can be reused
by the compiler.
Signed-off-by: Damien George <damien@micropython.org>
2022-04-21 07:30:23 +01:00
|
|
|
#include "genhdr/mpversion.h"
|
2014-04-13 04:43:18 +01:00
|
|
|
|
2019-08-14 15:09:36 +01:00
|
|
|
#if MICROPY_PY_SYS_SETTRACE
|
|
|
|
#include "py/objmodule.h"
|
|
|
|
#include "py/profile.h"
|
|
|
|
#endif
|
|
|
|
|
2014-05-24 23:03:12 +01:00
|
|
|
#if MICROPY_PY_SYS
|
2014-04-13 04:43:18 +01:00
|
|
|
|
2015-01-12 22:30:49 +00:00
|
|
|
// defined per port; type of these is irrelevant, just need pointer
|
2015-04-09 11:27:15 +01:00
|
|
|
extern struct _mp_dummy_t mp_sys_stdin_obj;
|
|
|
|
extern struct _mp_dummy_t mp_sys_stdout_obj;
|
|
|
|
extern struct _mp_dummy_t mp_sys_stderr_obj;
|
2015-01-12 22:30:49 +00:00
|
|
|
|
2017-01-13 00:08:51 +00:00
|
|
|
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
|
2015-11-27 17:01:44 +00:00
|
|
|
const mp_print_t mp_sys_stdout_print = {&mp_sys_stdout_obj, mp_stream_write_adaptor};
|
2015-04-11 12:01:39 +01:00
|
|
|
#endif
|
|
|
|
|
2017-08-30 12:02:00 +01:00
|
|
|
// version - Python language version that this implementation conforms to, as a string
|
py/modsys: Append MicroPython git version and build date to sys.version.
This commit adds the git hash and build date to sys.version. This is
allowed according to CPython docs, and is what PyPy does. The docs state:
A string containing the version number of the Python interpreter plus
additional information on the build number and compiler used.
Eg on CPython:
Python 3.10.4 (main, Mar 23 2022, 23:05:40) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.version
'3.10.4 (main, Mar 23 2022, 23:05:40) [GCC 11.2.0]'
and PyPy:
Python 2.7.12 (5.6.0+dfsg-4, Nov 20 2016, 10:43:30)
[PyPy 5.6.0 with GCC 6.2.0 20161109] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> import sys
>>>> sys.version
'2.7.12 (5.6.0+dfsg-4, Nov 20 2016, 10:43:30)\n[PyPy 5.6.0 with GCC ...
With this commit on MicroPython we now have:
MicroPython v1.18-371-g9d08eb024 on 2022-04-28; linux [GCC 11.2.0] v...
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import sys
>>> sys.version
'3.4.0; MicroPython v1.18-371-g9d08eb024 on 2022-04-28'
Note that the start of the banner is the same as the end of sys.version.
This helps to keep code size under control because the string can be reused
by the compiler.
Signed-off-by: Damien George <damien@micropython.org>
2022-04-21 07:30:23 +01:00
|
|
|
STATIC const MP_DEFINE_STR_OBJ(mp_sys_version_obj, "3.4.0; " MICROPY_BANNER_NAME_AND_VERSION);
|
2014-08-10 17:50:28 +01:00
|
|
|
|
2017-08-30 12:02:00 +01:00
|
|
|
// version_info - Python language version that this implementation conforms to, as a tuple of ints
|
2023-10-04 01:20:47 +01:00
|
|
|
// TODO: CPython is now at 5-element array (major, minor, micro, releaselevel, serial), but save 2 els so far...
|
|
|
|
STATIC const mp_rom_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {MP_ROM_INT(3), MP_ROM_INT(4), MP_ROM_INT(0)}};
|
2015-04-21 15:45:04 +01:00
|
|
|
|
|
|
|
// sys.implementation object
|
|
|
|
// this holds the MicroPython version
|
2023-10-04 01:20:47 +01:00
|
|
|
STATIC const mp_rom_obj_tuple_t mp_sys_implementation_version_info_obj = {
|
2015-04-21 15:45:04 +01:00
|
|
|
{&mp_type_tuple},
|
2023-10-04 01:20:47 +01:00
|
|
|
4,
|
|
|
|
{
|
|
|
|
MP_ROM_INT(MICROPY_VERSION_MAJOR),
|
|
|
|
MP_ROM_INT(MICROPY_VERSION_MINOR),
|
|
|
|
MP_ROM_INT(MICROPY_VERSION_MICRO),
|
|
|
|
#if MICROPY_VERSION_PRERELEASE
|
|
|
|
MP_ROM_QSTR(MP_QSTR_preview),
|
|
|
|
#else
|
|
|
|
MP_ROM_QSTR(MP_QSTR_),
|
|
|
|
#endif
|
|
|
|
}
|
2015-04-21 15:45:04 +01:00
|
|
|
};
|
2022-04-26 08:28:39 +01:00
|
|
|
STATIC const MP_DEFINE_STR_OBJ(mp_sys_implementation_machine_obj, MICROPY_BANNER_MACHINE);
|
2023-10-10 06:50:28 +01:00
|
|
|
#define SYS_IMPLEMENTATION_ELEMS_BASE \
|
2019-10-30 05:26:11 +00:00
|
|
|
MP_ROM_QSTR(MP_QSTR_micropython), \
|
2022-04-26 08:28:39 +01:00
|
|
|
MP_ROM_PTR(&mp_sys_implementation_version_info_obj), \
|
|
|
|
MP_ROM_PTR(&mp_sys_implementation_machine_obj)
|
2023-10-10 06:50:28 +01:00
|
|
|
|
|
|
|
#if MICROPY_PERSISTENT_CODE_LOAD
|
|
|
|
#define SYS_IMPLEMENTATION_ELEMS__MPY \
|
|
|
|
, MP_ROM_INT(MPY_FILE_HEADER_INT)
|
|
|
|
#else
|
|
|
|
#define SYS_IMPLEMENTATION_ELEMS__MPY
|
2019-10-30 05:26:11 +00:00
|
|
|
#endif
|
2023-10-10 06:50:28 +01:00
|
|
|
|
2015-04-21 15:45:04 +01:00
|
|
|
#if MICROPY_PY_ATTRTUPLE
|
2023-10-10 06:50:28 +01:00
|
|
|
#if MICROPY_PREVIEW_VERSION_2
|
|
|
|
#define SYS_IMPLEMENTATION_ELEMS__V2 \
|
|
|
|
, MP_ROM_TRUE
|
|
|
|
#else
|
|
|
|
#define SYS_IMPLEMENTATION_ELEMS__V2
|
|
|
|
#endif
|
|
|
|
|
2019-10-30 05:26:11 +00:00
|
|
|
STATIC const qstr impl_fields[] = {
|
|
|
|
MP_QSTR_name,
|
|
|
|
MP_QSTR_version,
|
2022-04-26 08:28:39 +01:00
|
|
|
MP_QSTR__machine,
|
2019-10-30 05:26:11 +00:00
|
|
|
#if MICROPY_PERSISTENT_CODE_LOAD
|
2022-04-26 08:23:29 +01:00
|
|
|
MP_QSTR__mpy,
|
2019-10-30 05:26:11 +00:00
|
|
|
#endif
|
2023-10-10 06:50:28 +01:00
|
|
|
#if MICROPY_PREVIEW_VERSION_2
|
|
|
|
MP_QSTR__v2,
|
|
|
|
#endif
|
2019-10-30 05:26:11 +00:00
|
|
|
};
|
2015-04-21 15:45:04 +01:00
|
|
|
STATIC MP_DEFINE_ATTRTUPLE(
|
|
|
|
mp_sys_implementation_obj,
|
|
|
|
impl_fields,
|
2023-10-10 06:50:28 +01:00
|
|
|
3 + MICROPY_PERSISTENT_CODE_LOAD + MICROPY_PREVIEW_VERSION_2,
|
|
|
|
SYS_IMPLEMENTATION_ELEMS_BASE
|
|
|
|
SYS_IMPLEMENTATION_ELEMS__MPY
|
|
|
|
SYS_IMPLEMENTATION_ELEMS__V2
|
2015-04-21 15:45:04 +01:00
|
|
|
);
|
|
|
|
#else
|
2017-06-07 15:41:27 +01:00
|
|
|
STATIC const mp_rom_obj_tuple_t mp_sys_implementation_obj = {
|
2015-04-21 15:45:04 +01:00
|
|
|
{&mp_type_tuple},
|
2022-04-26 08:28:39 +01:00
|
|
|
3 + MICROPY_PERSISTENT_CODE_LOAD,
|
2023-10-10 06:50:28 +01:00
|
|
|
// Do not include SYS_IMPLEMENTATION_ELEMS__V2 because
|
|
|
|
// SYS_IMPLEMENTATION_ELEMS__MPY may be empty if
|
|
|
|
// MICROPY_PERSISTENT_CODE_LOAD is disabled, which means they'll share
|
|
|
|
// the same index. Cannot query _v2 if MICROPY_PY_ATTRTUPLE is
|
|
|
|
// disabled.
|
2015-04-21 15:45:04 +01:00
|
|
|
{
|
2023-10-10 06:50:28 +01:00
|
|
|
SYS_IMPLEMENTATION_ELEMS_BASE
|
|
|
|
SYS_IMPLEMENTATION_ELEMS__MPY
|
2015-04-21 15:45:04 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2014-04-13 07:46:58 +01:00
|
|
|
#undef I
|
2014-08-10 17:50:28 +01:00
|
|
|
|
2014-06-07 21:40:04 +01:00
|
|
|
#ifdef MICROPY_PY_SYS_PLATFORM
|
2017-08-30 12:02:00 +01:00
|
|
|
// platform - the platform that MicroPython is running on
|
2020-05-22 17:10:15 +01:00
|
|
|
STATIC const MP_DEFINE_STR_OBJ(mp_sys_platform_obj, MICROPY_PY_SYS_PLATFORM);
|
2014-06-07 21:40:04 +01:00
|
|
|
#endif
|
2014-04-13 04:43:18 +01:00
|
|
|
|
2022-10-06 16:13:58 +01:00
|
|
|
#ifdef MICROPY_PY_SYS_EXECUTABLE
|
|
|
|
// executable - the path to the micropython binary
|
|
|
|
// This object is non-const and is populated at startup in main()
|
|
|
|
MP_DEFINE_STR_OBJ(mp_sys_executable_obj, "");
|
|
|
|
#endif
|
|
|
|
|
2017-08-30 12:02:00 +01:00
|
|
|
// exit([retval]): raise SystemExit, with optional argument given to the exception
|
2016-01-03 14:21:40 +00:00
|
|
|
STATIC mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
|
2014-09-15 15:53:09 +01:00
|
|
|
if (n_args == 0) {
|
2021-07-14 14:25:18 +01:00
|
|
|
mp_raise_type(&mp_type_SystemExit);
|
2014-09-15 15:53:09 +01:00
|
|
|
} else {
|
2021-07-14 14:25:18 +01:00
|
|
|
mp_raise_type_arg(&mp_type_SystemExit, args[0]);
|
2014-09-15 15:53:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
|
|
|
|
|
2016-01-03 14:21:40 +00:00
|
|
|
STATIC mp_obj_t mp_sys_print_exception(size_t n_args, const mp_obj_t *args) {
|
2017-01-13 00:08:51 +00:00
|
|
|
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
|
2015-11-27 17:01:44 +00:00
|
|
|
void *stream_obj = &mp_sys_stdout_obj;
|
2014-12-06 12:29:09 +00:00
|
|
|
if (n_args > 1) {
|
2018-06-20 06:57:10 +01:00
|
|
|
mp_get_stream_raise(args[1], MP_STREAM_OP_WRITE);
|
|
|
|
stream_obj = MP_OBJ_TO_PTR(args[1]);
|
2014-12-06 12:29:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_print_t print = {stream_obj, mp_stream_write_adaptor};
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_obj_print_exception(&print, args[0]);
|
2014-12-06 12:29:09 +00:00
|
|
|
#else
|
2015-09-04 16:53:46 +01:00
|
|
|
(void)n_args;
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_obj_print_exception(&mp_plat_print, args[0]);
|
2014-12-06 12:29:09 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_print_exception_obj, 1, 2, mp_sys_print_exception);
|
|
|
|
|
2015-04-25 01:17:41 +01:00
|
|
|
#if MICROPY_PY_SYS_EXC_INFO
|
|
|
|
STATIC mp_obj_t mp_sys_exc_info(void) {
|
2015-11-27 17:01:44 +00:00
|
|
|
mp_obj_t cur_exc = MP_OBJ_FROM_PTR(MP_STATE_VM(cur_exception));
|
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
|
2015-04-25 01:17:41 +01:00
|
|
|
|
|
|
|
if (cur_exc == MP_OBJ_NULL) {
|
|
|
|
t->items[0] = mp_const_none;
|
|
|
|
t->items[1] = mp_const_none;
|
|
|
|
t->items[2] = mp_const_none;
|
2015-11-27 17:01:44 +00:00
|
|
|
return MP_OBJ_FROM_PTR(t);
|
2015-04-25 01:17:41 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 17:01:44 +00:00
|
|
|
t->items[0] = MP_OBJ_FROM_PTR(mp_obj_get_type(cur_exc));
|
2015-04-25 01:17:41 +01:00
|
|
|
t->items[1] = cur_exc;
|
|
|
|
t->items[2] = mp_const_none;
|
2015-11-27 17:01:44 +00:00
|
|
|
return MP_OBJ_FROM_PTR(t);
|
2015-04-25 01:17:41 +01:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info);
|
|
|
|
#endif
|
|
|
|
|
2018-04-04 05:23:25 +01:00
|
|
|
#if MICROPY_PY_SYS_GETSIZEOF
|
2017-08-11 07:42:39 +01:00
|
|
|
STATIC mp_obj_t mp_sys_getsizeof(mp_obj_t obj) {
|
|
|
|
return mp_unary_op(MP_UNARY_OP_SIZEOF, obj);
|
|
|
|
}
|
2018-04-04 05:23:25 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_getsizeof_obj, mp_sys_getsizeof);
|
|
|
|
#endif
|
2017-08-11 07:42:39 +01:00
|
|
|
|
2019-08-05 14:06:41 +01:00
|
|
|
#if MICROPY_PY_SYS_ATEXIT
|
|
|
|
// atexit(callback): Callback is called when sys.exit is called.
|
|
|
|
STATIC mp_obj_t mp_sys_atexit(mp_obj_t obj) {
|
|
|
|
mp_obj_t old = MP_STATE_VM(sys_exitfunc);
|
|
|
|
MP_STATE_VM(sys_exitfunc) = obj;
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_atexit_obj, mp_sys_atexit);
|
|
|
|
#endif
|
|
|
|
|
2019-08-14 15:09:36 +01:00
|
|
|
#if MICROPY_PY_SYS_SETTRACE
|
2021-11-19 06:26:04 +00:00
|
|
|
// settrace(tracefunc): Set the system's trace function.
|
2019-08-14 15:09:36 +01:00
|
|
|
STATIC mp_obj_t mp_sys_settrace(mp_obj_t obj) {
|
|
|
|
return mp_prof_settrace(obj);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_settrace_obj, mp_sys_settrace);
|
|
|
|
#endif // MICROPY_PY_SYS_SETTRACE
|
|
|
|
|
2023-06-05 07:52:29 +01:00
|
|
|
#if MICROPY_PY_SYS_PATH && !MICROPY_PY_SYS_ATTR_DELEGATION
|
|
|
|
#error "MICROPY_PY_SYS_PATH requires MICROPY_PY_SYS_ATTR_DELEGATION"
|
|
|
|
#endif
|
2023-06-05 13:38:36 +01:00
|
|
|
|
|
|
|
#if MICROPY_PY_SYS_PS1_PS2 && !MICROPY_PY_SYS_ATTR_DELEGATION
|
|
|
|
#error "MICROPY_PY_SYS_PS1_PS2 requires MICROPY_PY_SYS_ATTR_DELEGATION"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_PY_SYS_TRACEBACKLIMIT && !MICROPY_PY_SYS_ATTR_DELEGATION
|
|
|
|
#error "MICROPY_PY_SYS_TRACEBACKLIMIT requires MICROPY_PY_SYS_ATTR_DELEGATION"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_PY_SYS_ATTR_DELEGATION && !MICROPY_MODULE_ATTR_DELEGATION
|
|
|
|
#error "MICROPY_PY_SYS_ATTR_DELEGATION requires MICROPY_MODULE_ATTR_DELEGATION"
|
|
|
|
#endif
|
|
|
|
|
2021-07-26 15:39:04 +01:00
|
|
|
#if MICROPY_PY_SYS_ATTR_DELEGATION
|
2023-06-05 13:38:36 +01:00
|
|
|
// Must be kept in sync with the enum at the top of mpstate.h.
|
2021-07-26 15:39:04 +01:00
|
|
|
STATIC const uint16_t sys_mutable_keys[] = {
|
2023-06-05 07:52:29 +01:00
|
|
|
#if MICROPY_PY_SYS_PATH
|
|
|
|
// Code should access this (as an mp_obj_t) for use with e.g.
|
|
|
|
// mp_obj_list_append by using the `mp_sys_path` macro defined in runtime.h.
|
|
|
|
MP_QSTR_path,
|
|
|
|
#endif
|
2021-07-26 15:43:35 +01:00
|
|
|
#if MICROPY_PY_SYS_PS1_PS2
|
|
|
|
MP_QSTR_ps1,
|
|
|
|
MP_QSTR_ps2,
|
|
|
|
#endif
|
2021-07-26 15:41:27 +01:00
|
|
|
#if MICROPY_PY_SYS_TRACEBACKLIMIT
|
|
|
|
MP_QSTR_tracebacklimit,
|
|
|
|
#endif
|
2021-07-26 15:39:04 +01:00
|
|
|
MP_QSTRnull,
|
|
|
|
};
|
|
|
|
|
2023-06-05 06:52:57 +01:00
|
|
|
void mp_module_sys_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
2021-07-26 15:39:04 +01:00
|
|
|
MP_STATIC_ASSERT(MP_ARRAY_SIZE(sys_mutable_keys) == MP_SYS_MUTABLE_NUM + 1);
|
|
|
|
MP_STATIC_ASSERT(MP_ARRAY_SIZE(MP_STATE_VM(sys_mutable)) == MP_SYS_MUTABLE_NUM);
|
|
|
|
mp_module_generic_attr(attr, dest, sys_mutable_keys, MP_STATE_VM(sys_mutable));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-27 13:38:15 +00:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys) },
|
2014-05-10 14:50:45 +01:00
|
|
|
|
2023-06-05 07:52:29 +01:00
|
|
|
#if MICROPY_PY_SYS_ARGV
|
2015-11-27 13:38:15 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_argv), MP_ROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)) },
|
2023-06-05 07:52:29 +01:00
|
|
|
#endif
|
2020-05-22 17:10:15 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&mp_sys_version_obj) },
|
2015-11-27 13:38:15 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_version_info), MP_ROM_PTR(&mp_sys_version_info_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_implementation), MP_ROM_PTR(&mp_sys_implementation_obj) },
|
2015-11-21 13:32:00 +00:00
|
|
|
#ifdef MICROPY_PY_SYS_PLATFORM
|
2020-05-22 17:10:15 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&mp_sys_platform_obj) },
|
2015-11-21 13:32:00 +00:00
|
|
|
#endif
|
|
|
|
#if MP_ENDIANNESS_LITTLE
|
2015-11-27 13:38:15 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_little) },
|
2015-11-21 13:32:00 +00:00
|
|
|
#else
|
2015-11-27 13:38:15 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_big) },
|
2015-11-21 13:32:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_PY_SYS_MAXSIZE
|
2014-07-03 14:50:11 +01:00
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
|
2017-03-06 11:15:25 +00:00
|
|
|
// Maximum mp_int_t value is not representable as small int, so we have
|
|
|
|
// little choice but to use MP_SMALL_INT_MAX. Apps also should be careful
|
|
|
|
// to not try to compare sys.maxsize to some literal number (as this
|
|
|
|
// number might not fit in available int size), but instead count number
|
|
|
|
// of "one" bits in sys.maxsize.
|
2017-07-31 03:59:39 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_INT(MP_SMALL_INT_MAX) },
|
2014-07-03 14:50:11 +01:00
|
|
|
#else
|
2020-05-22 17:10:15 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_PTR(&mp_sys_maxsize_obj) },
|
2014-07-03 14:50:11 +01:00
|
|
|
#endif
|
2015-11-21 13:32:00 +00:00
|
|
|
#endif
|
2014-07-03 14:50:11 +01:00
|
|
|
|
2015-11-21 13:32:00 +00:00
|
|
|
#if MICROPY_PY_SYS_EXIT
|
2015-11-27 13:38:15 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&mp_sys_exit_obj) },
|
2015-11-21 13:32:00 +00:00
|
|
|
#endif
|
2014-05-11 17:35:43 +01:00
|
|
|
|
2019-08-14 15:09:36 +01:00
|
|
|
#if MICROPY_PY_SYS_SETTRACE
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_settrace), MP_ROM_PTR(&mp_sys_settrace_obj) },
|
|
|
|
#endif
|
|
|
|
|
2015-11-21 13:32:00 +00:00
|
|
|
#if MICROPY_PY_SYS_STDFILES
|
2015-11-27 13:38:15 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_stdin), MP_ROM_PTR(&mp_sys_stdin_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_stdout), MP_ROM_PTR(&mp_sys_stdout_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_stderr), MP_ROM_PTR(&mp_sys_stderr_obj) },
|
2015-11-21 13:32:00 +00:00
|
|
|
#endif
|
2014-12-06 12:29:09 +00:00
|
|
|
|
2015-12-04 22:09:10 +00:00
|
|
|
#if MICROPY_PY_SYS_MODULES
|
2015-12-17 00:40:14 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_modules), MP_ROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)) },
|
2015-12-04 22:09:10 +00:00
|
|
|
#endif
|
2015-04-25 01:17:41 +01:00
|
|
|
#if MICROPY_PY_SYS_EXC_INFO
|
2015-11-27 13:38:15 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
|
2015-04-25 01:17:41 +01:00
|
|
|
#endif
|
2017-08-11 07:42:39 +01:00
|
|
|
#if MICROPY_PY_SYS_GETSIZEOF
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_getsizeof), MP_ROM_PTR(&mp_sys_getsizeof_obj) },
|
|
|
|
#endif
|
2015-04-25 01:17:41 +01:00
|
|
|
|
2022-10-06 16:13:58 +01:00
|
|
|
#if MICROPY_PY_SYS_EXECUTABLE
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_executable), MP_ROM_PTR(&mp_sys_executable_obj) },
|
|
|
|
#endif
|
|
|
|
|
2014-12-06 12:29:09 +00:00
|
|
|
/*
|
|
|
|
* Extensions to CPython
|
|
|
|
*/
|
|
|
|
|
2015-11-27 13:38:15 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
|
2019-08-05 14:06:41 +01:00
|
|
|
#if MICROPY_PY_SYS_ATEXIT
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_atexit), MP_ROM_PTR(&mp_sys_atexit_obj) },
|
|
|
|
#endif
|
2014-04-13 04:43:18 +01:00
|
|
|
};
|
|
|
|
|
2014-11-29 14:39:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table);
|
2014-04-13 04:43:18 +01:00
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_sys = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_sys_globals,
|
|
|
|
};
|
|
|
|
|
2023-06-02 03:33:25 +01:00
|
|
|
// Unlike the other CPython-compatible modules, sys is not extensible from the
|
|
|
|
// filesystem. We rely on it to work so that things like sys.path are always
|
|
|
|
// available.
|
2022-08-17 07:18:31 +01:00
|
|
|
MP_REGISTER_MODULE(MP_QSTR_sys, mp_module_sys);
|
2022-04-20 07:14:22 +01:00
|
|
|
|
2023-06-05 07:52:29 +01:00
|
|
|
#if MICROPY_PY_SYS_ARGV
|
|
|
|
// Code should access this (as an mp_obj_t) for use with e.g.
|
|
|
|
// mp_obj_list_append by using the `mp_sys_argv` macro defined in runtime.h.
|
2022-07-02 21:05:41 +01:00
|
|
|
MP_REGISTER_ROOT_POINTER(mp_obj_list_t mp_sys_argv_obj);
|
2023-06-05 07:52:29 +01:00
|
|
|
#endif
|
2022-07-02 21:05:41 +01:00
|
|
|
|
|
|
|
#if MICROPY_PY_SYS_EXC_INFO
|
|
|
|
// current exception being handled, for sys.exc_info()
|
|
|
|
MP_REGISTER_ROOT_POINTER(mp_obj_base_t * cur_exception);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_PY_SYS_ATEXIT
|
|
|
|
// exposed through sys.atexit function
|
|
|
|
MP_REGISTER_ROOT_POINTER(mp_obj_t sys_exitfunc);
|
2014-04-13 04:43:18 +01:00
|
|
|
#endif
|
2022-07-02 21:05:41 +01:00
|
|
|
|
|
|
|
#if MICROPY_PY_SYS_ATTR_DELEGATION
|
|
|
|
// Contains mutable sys attributes.
|
|
|
|
MP_REGISTER_ROOT_POINTER(mp_obj_t sys_mutable[MP_SYS_MUTABLE_NUM]);
|
2023-06-12 04:09:48 +01:00
|
|
|
MP_REGISTER_MODULE_DELEGATION(mp_module_sys, mp_module_sys_attr);
|
2022-07-02 21:05:41 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_SYS
|