2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* 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-02-07 12:24:10 -05:00
|
|
|
#include "py/mpstate.h"
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/builtin.h"
|
|
|
|
#include "py/objlist.h"
|
|
|
|
#include "py/objtuple.h"
|
|
|
|
#include "py/objstr.h"
|
|
|
|
#include "py/objint.h"
|
|
|
|
#include "py/stream.h"
|
2017-03-06 06:15:25 -05:00
|
|
|
#include "py/smallint.h"
|
2014-04-12 23:43:18 -04:00
|
|
|
|
2014-05-24 18:03:12 -04:00
|
|
|
#if MICROPY_PY_SYS
|
2014-04-12 23:43:18 -04:00
|
|
|
|
2015-04-22 12:38:05 -04:00
|
|
|
#include "genhdr/mpversion.h"
|
2015-04-21 10:45:04 -04:00
|
|
|
|
2014-08-10 12:50:28 -04:00
|
|
|
/// \module sys - system specific functions
|
|
|
|
|
2015-01-12 17:30:49 -05:00
|
|
|
// defined per port; type of these is irrelevant, just need pointer
|
2015-04-09 06:27:15 -04: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 17:30:49 -05:00
|
|
|
|
2017-01-12 19:08:51 -05:00
|
|
|
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
|
2015-11-27 12:01:44 -05:00
|
|
|
const mp_print_t mp_sys_stdout_print = {&mp_sys_stdout_obj, mp_stream_write_adaptor};
|
2015-04-11 07:01:39 -04:00
|
|
|
#endif
|
|
|
|
|
2014-08-10 12:50:28 -04:00
|
|
|
/// \constant version - Python language version that this implementation conforms to, as a string
|
|
|
|
STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0");
|
|
|
|
|
2014-08-10 12:53:43 -04:00
|
|
|
/// \constant version_info - Python language version that this implementation conforms to, as a tuple of ints
|
2014-04-13 02:46:58 -04:00
|
|
|
#define I(n) MP_OBJ_NEW_SMALL_INT(n)
|
|
|
|
// TODO: CPython is now at 5-element array, but save 2 els so far...
|
2014-04-13 18:46:45 -04:00
|
|
|
STATIC const mp_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {I(3), I(4), I(0)}};
|
2015-04-21 10:45:04 -04:00
|
|
|
|
|
|
|
// sys.implementation object
|
|
|
|
// this holds the MicroPython version
|
|
|
|
STATIC const mp_obj_tuple_t mp_sys_implementation_version_info_obj = {
|
|
|
|
{&mp_type_tuple},
|
|
|
|
3,
|
|
|
|
{ I(MICROPY_VERSION_MAJOR), I(MICROPY_VERSION_MINOR), I(MICROPY_VERSION_MICRO) }
|
|
|
|
};
|
|
|
|
#if MICROPY_PY_ATTRTUPLE
|
|
|
|
STATIC const qstr impl_fields[] = { MP_QSTR_name, MP_QSTR_version };
|
|
|
|
STATIC MP_DEFINE_ATTRTUPLE(
|
|
|
|
mp_sys_implementation_obj,
|
|
|
|
impl_fields,
|
|
|
|
2,
|
2015-11-27 08:38:15 -05:00
|
|
|
MP_ROM_QSTR(MP_QSTR_micropython),
|
|
|
|
MP_ROM_PTR(&mp_sys_implementation_version_info_obj)
|
2015-04-21 10:45:04 -04:00
|
|
|
);
|
|
|
|
#else
|
2017-06-07 10:41:27 -04:00
|
|
|
STATIC const mp_rom_obj_tuple_t mp_sys_implementation_obj = {
|
2015-04-21 10:45:04 -04:00
|
|
|
{&mp_type_tuple},
|
|
|
|
2,
|
|
|
|
{
|
2017-06-07 10:41:27 -04:00
|
|
|
MP_ROM_QSTR(MP_QSTR_micropython),
|
|
|
|
MP_ROM_PTR(&mp_sys_implementation_version_info_obj),
|
2015-04-21 10:45:04 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2014-04-13 02:46:58 -04:00
|
|
|
#undef I
|
2014-08-10 12:50:28 -04:00
|
|
|
|
2014-06-07 16:40:04 -04:00
|
|
|
#ifdef MICROPY_PY_SYS_PLATFORM
|
2017-06-30 03:22:17 -04:00
|
|
|
/// \constant platform - the platform that MicroPython is running on
|
2014-06-07 16:40:04 -04:00
|
|
|
STATIC const MP_DEFINE_STR_OBJ(platform_obj, MICROPY_PY_SYS_PLATFORM);
|
|
|
|
#endif
|
2014-04-12 23:43:18 -04:00
|
|
|
|
2014-09-15 10:53:09 -04:00
|
|
|
/// \function exit([retval])
|
|
|
|
/// Raise a `SystemExit` exception. If an argument is given, it is the
|
|
|
|
/// value given to `SystemExit`.
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
|
2014-09-15 10:53:09 -04:00
|
|
|
mp_obj_t exc;
|
|
|
|
if (n_args == 0) {
|
|
|
|
exc = mp_obj_new_exception(&mp_type_SystemExit);
|
|
|
|
} else {
|
|
|
|
exc = mp_obj_new_exception_arg1(&mp_type_SystemExit, args[0]);
|
|
|
|
}
|
|
|
|
nlr_raise(exc);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
|
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t mp_sys_print_exception(size_t n_args, const mp_obj_t *args) {
|
2017-01-12 19:08:51 -05:00
|
|
|
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
|
2015-11-27 12:01:44 -05:00
|
|
|
void *stream_obj = &mp_sys_stdout_obj;
|
2014-12-06 07:29:09 -05:00
|
|
|
if (n_args > 1) {
|
2015-11-27 12:01:44 -05:00
|
|
|
stream_obj = MP_OBJ_TO_PTR(args[1]); // XXX may fail
|
2014-12-06 07:29:09 -05:00
|
|
|
}
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_print_t print = {stream_obj, mp_stream_write_adaptor};
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_obj_print_exception(&print, args[0]);
|
2014-12-06 07:29:09 -05:00
|
|
|
#else
|
2015-09-04 11:53:46 -04:00
|
|
|
(void)n_args;
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_obj_print_exception(&mp_plat_print, args[0]);
|
2014-12-06 07:29:09 -05: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-24 20:17:41 -04:00
|
|
|
#if MICROPY_PY_SYS_EXC_INFO
|
|
|
|
STATIC mp_obj_t mp_sys_exc_info(void) {
|
2015-11-27 12:01:44 -05: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-24 20:17:41 -04: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 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(t);
|
2015-04-24 20:17:41 -04:00
|
|
|
}
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
t->items[0] = MP_OBJ_FROM_PTR(mp_obj_get_type(cur_exc));
|
2015-04-24 20:17:41 -04:00
|
|
|
t->items[1] = cur_exc;
|
|
|
|
t->items[2] = mp_const_none;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(t);
|
2015-04-24 20:17:41 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(mp_sys_exc_info_obj, mp_sys_exc_info);
|
|
|
|
#endif
|
|
|
|
|
2015-11-27 08:38:15 -05: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 09:50:45 -04:00
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_path), MP_ROM_PTR(&MP_STATE_VM(mp_sys_path_obj)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_argv), MP_ROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&version_obj) },
|
|
|
|
{ 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 08:32:00 -05:00
|
|
|
#ifdef MICROPY_PY_SYS_PLATFORM
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_platform), MP_ROM_PTR(&platform_obj) },
|
2015-11-21 08:32:00 -05:00
|
|
|
#endif
|
2014-08-10 12:50:28 -04:00
|
|
|
/// \constant byteorder - the byte order of the system ("little" or "big")
|
2015-11-21 08:32:00 -05:00
|
|
|
#if MP_ENDIANNESS_LITTLE
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_little) },
|
2015-11-21 08:32:00 -05:00
|
|
|
#else
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_QSTR(MP_QSTR_big) },
|
2015-11-21 08:32:00 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MICROPY_PY_SYS_MAXSIZE
|
2014-07-03 09:50:11 -04:00
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
|
2017-03-06 06:15:25 -05: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-30 22:59:39 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_INT(MP_SMALL_INT_MAX) },
|
2014-07-03 09:50:11 -04:00
|
|
|
#else
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_maxsize), MP_ROM_PTR(&mp_maxsize_obj) },
|
2014-07-03 09:50:11 -04:00
|
|
|
#endif
|
2015-11-21 08:32:00 -05:00
|
|
|
#endif
|
2014-07-03 09:50:11 -04:00
|
|
|
|
2015-11-21 08:32:00 -05:00
|
|
|
#if MICROPY_PY_SYS_EXIT
|
2014-08-10 12:50:28 -04:00
|
|
|
// documented per-port
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&mp_sys_exit_obj) },
|
2015-11-21 08:32:00 -05:00
|
|
|
#endif
|
2014-05-11 12:35:43 -04:00
|
|
|
|
2015-11-21 08:32:00 -05:00
|
|
|
#if MICROPY_PY_SYS_STDFILES
|
2014-08-10 12:50:28 -04:00
|
|
|
// documented per-port
|
2015-11-27 08:38:15 -05: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 08:32:00 -05:00
|
|
|
#endif
|
2014-12-06 07:29:09 -05:00
|
|
|
|
2015-12-04 17:09:10 -05:00
|
|
|
#if MICROPY_PY_SYS_MODULES
|
2015-12-16 19:40:14 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_modules), MP_ROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)) },
|
2015-12-04 17:09:10 -05:00
|
|
|
#endif
|
2015-04-24 20:17:41 -04:00
|
|
|
#if MICROPY_PY_SYS_EXC_INFO
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_exc_info), MP_ROM_PTR(&mp_sys_exc_info_obj) },
|
2015-04-24 20:17:41 -04:00
|
|
|
#endif
|
|
|
|
|
2014-12-06 07:29:09 -05:00
|
|
|
/*
|
|
|
|
* Extensions to CPython
|
|
|
|
*/
|
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_print_exception), MP_ROM_PTR(&mp_sys_print_exception_obj) },
|
2014-04-12 23:43:18 -04:00
|
|
|
};
|
|
|
|
|
2014-11-29 09:39:27 -05:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table);
|
2014-04-12 23:43:18 -04:00
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_sys = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t*)&mp_module_sys_globals,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|