py/runtime: Allow initialising sys.path/argv with defaults.

If MICROPY_PY_SYS_PATH_ARGV_DEFAULTS is enabled (which it is by default)
then sys.path and sys.argv will be initialised and populated with default
values.  This keeps all bare-metal ports aligned.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2021-12-17 23:35:32 +11:00
parent 86394f70fc
commit de43b500bd
18 changed files with 18 additions and 59 deletions

View File

@ -53,8 +53,6 @@ The basic MicroPython firmware is implemented in the main port file, e.g ``main.
mp_stack_ctrl_init();
gc_init(heap, heap + sizeof(heap));
mp_init();
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0);
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0);
// Start a normal REPL; will exit when ctrl-D is entered on a blank line.
pyexec_friendly_repl();

View File

@ -192,8 +192,6 @@ MP_NOINLINE int main_(int argc, char **argv) {
#ifdef _WIN32
set_fmode_binary();
#endif
mp_obj_list_init(mp_sys_path, 0);
mp_obj_list_init(mp_sys_argv, 0);
#if MICROPY_EMIT_NATIVE
// Set default emitter options

View File

@ -139,9 +139,6 @@ soft_reset:
// MicroPython init
mp_init();
mp_obj_list_init(mp_sys_path, 0);
mp_obj_list_init(mp_sys_argv, 0);
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
// execute all basic initializations
mp_irq_init0();

View File

@ -140,13 +140,7 @@ soft_reset:
mp_stack_set_limit(MP_TASK_STACK_SIZE - MP_TASK_STACK_LIMIT_MARGIN);
gc_init(mp_task_heap, mp_task_heap + mp_task_heap_size);
mp_init();
mp_obj_list_init(mp_sys_path, 0);
#if MICROPY_MODULE_FROZEN
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
#endif
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_));
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
mp_obj_list_init(mp_sys_argv, 0);
readline_init0();
// initialise peripherals

View File

@ -52,14 +52,8 @@ STATIC void mp_reset(void) {
mp_hal_init();
gc_init(heap, heap + sizeof(heap));
mp_init();
mp_obj_list_init(mp_sys_path, 0);
#if MICROPY_MODULE_FROZEN
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
#endif
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_));
mp_obj_list_init(mp_sys_argv, 0);
#if MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA
extern void esp_native_code_init(void);
esp_native_code_init();

View File

@ -91,10 +91,6 @@ void mp_js_init(int heap_size) {
#endif
mp_init();
mp_obj_list_init(mp_sys_path, 0);
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_));
mp_obj_list_init(mp_sys_argv, 0);
}
void mp_js_init_repl() {

View File

@ -75,12 +75,6 @@ int main(void) {
gc_init(&_gc_heap_start, &_gc_heap_end);
mp_init();
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0);
#if MICROPY_MODULE_FROZEN
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
#endif
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_));
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0);
#if MICROPY_PY_NETWORK
mod_network_init();
#endif

View File

@ -129,16 +129,8 @@ soft_reset:
gc_init(&_heap_start, &_heap_end);
mp_init();
mp_obj_list_init(mp_sys_path, 0);
#if MICROPY_MODULE_FROZEN
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
#endif
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
mp_obj_list_init(mp_sys_argv, 0);
readline_init0();
#if MICROPY_PY_MACHINE_HW_SPI
spi_init0();
#endif

View File

@ -100,13 +100,7 @@ int main(int argc, char **argv) {
// Initialise MicroPython runtime.
mp_init();
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0);
#if MICROPY_MODULE_FROZEN
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
#endif
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_));
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0);
// Initialise sub-systems.
readline_init0();

View File

@ -41,9 +41,6 @@ void samd_main(void) {
for (;;) {
gc_init(&_sheap, &_eheap);
mp_init();
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0);
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_));
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0);
// Execute _boot.py to set up the filesystem.
pyexec_frozen_module("_boot.py");

View File

@ -494,12 +494,6 @@ soft_reset:
// MicroPython init
mp_init();
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0);
#if MICROPY_MODULE_FROZEN
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
#endif
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0);
// Initialise low-level sub-systems. Here we need to very basic things like
// zeroing out memory and resetting any of the sub-systems. Following this

View File

@ -269,9 +269,6 @@ soft_reset:
// MicroPython init
mp_init();
mp_obj_list_init(mp_sys_path, 0);
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
mp_obj_list_init(mp_sys_argv, 0);
readline_init0();

View File

@ -110,6 +110,7 @@
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
#define MICROPY_PY_BUILTINS_SLICE_ATTRS (1)
#define MICROPY_PY_BUILTINS_SLICE_INDICES (1)
#define MICROPY_PY_SYS_PATH_ARGV_DEFAULTS (0)
#define MICROPY_PY_SYS_EXIT (1)
#define MICROPY_PY_SYS_ATEXIT (1)
#if MICROPY_PY_SYS_SETTRACE

View File

@ -85,6 +85,7 @@
#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1)
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
#define MICROPY_PY_BUILTINS_SLICE_ATTRS (1)
#define MICROPY_PY_SYS_PATH_ARGV_DEFAULTS (0)
#define MICROPY_PY_SYS_EXIT (1)
#define MICROPY_PY_SYS_ATEXIT (1)
#define MICROPY_PY_SYS_PLATFORM "win32"

View File

@ -139,9 +139,6 @@ soft_reset:
gc_init(heap, heap + sizeof(heap));
#endif
mp_init();
mp_obj_list_init(mp_sys_path, 0);
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
mp_obj_list_init(mp_sys_argv, 0);
#ifdef CONFIG_USB
usb_enable(NULL);

View File

@ -1306,6 +1306,11 @@ typedef double mp_float_t;
#define MICROPY_PY_SYS (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_CORE_FEATURES)
#endif
// Whether to initialise "sys.path" and "sys.argv" to their defaults in mp_init()
#ifndef MICROPY_PY_SYS_PATH_ARGV_DEFAULTS
#define MICROPY_PY_SYS_PATH_ARGV_DEFAULTS (1)
#endif
// Whether to provide "sys.maxsize" constant
#ifndef MICROPY_PY_SYS_MAXSIZE
#define MICROPY_PY_SYS_MAXSIZE (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)

View File

@ -153,7 +153,8 @@ typedef struct _mp_state_vm_t {
// dictionary for the __main__ module
mp_obj_dict_t dict_main;
// these two lists must be initialised per port, after the call to mp_init
// If MICROPY_PY_SYS_PATH_ARGV_DEFAULTS is not enabled then these two lists
// must be initialised after the call to mp_init.
mp_obj_list_t mp_sys_path_obj;
mp_obj_list_t mp_sys_argv_obj;

View File

@ -122,6 +122,15 @@ void mp_init(void) {
MP_STATE_VM(vfs_mount_table) = NULL;
#endif
#if MICROPY_PY_SYS_PATH_ARGV_DEFAULTS
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), 0);
#if MICROPY_MODULE_FROZEN
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
#endif
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0);
#endif
#if MICROPY_PY_SYS_ATEXIT
MP_STATE_VM(sys_exitfunc) = mp_const_none;
#endif