From 14e9c00cb99465236c579da722a72a9cfc4ef12f Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 15 Aug 2025 14:17:09 +1000 Subject: [PATCH 1/2] py/builtinimport: Guard code needing sys.path with MICROPY_PY_SYS_PATH. Signed-off-by: Damien George --- py/builtinimport.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/py/builtinimport.c b/py/builtinimport.c index a2737a03e8dc2..57b5c14e8836e 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -116,7 +116,7 @@ static mp_import_stat_t stat_module(vstr_t *path) { // path (i.e. "/mod_name(.py)"). static mp_import_stat_t stat_top_level(qstr mod_name, vstr_t *dest) { DEBUG_printf("stat_top_level: '%s'\n", qstr_str(mod_name)); - #if MICROPY_PY_SYS + #if MICROPY_PY_SYS && MICROPY_PY_SYS_PATH size_t path_num; mp_obj_t *path_items; mp_obj_get_array(mp_sys_path, &path_num, &path_items); @@ -367,7 +367,7 @@ static mp_obj_t process_import_at_level(qstr full_mod_name, qstr level_mod_name, // Immediately return if the module at this level is already loaded. mp_map_elem_t *elem; - #if MICROPY_PY_SYS + #if MICROPY_PY_SYS && MICROPY_PY_SYS_PATH // If sys.path is empty, the intention is to force using a built-in. This // means we should also ignore any loaded modules with the same name // which may have come from the filesystem. From a3f9dec78852caeb56b70f1cf5cd2bd71f36c1ee Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 15 Aug 2025 13:11:51 +1000 Subject: [PATCH 2/2] py/mpconfig: Enable the sys module at all feature levels by default. This is a pretty fundamental module, and even minimal ports like unix and zephyr minimal have it enabled. So, enabled it by default at the lowest feature level. Most things in the `sys` module are configurable, and off by default, so it shouldn't add too much to ports that don't already have it enabled (which is just the minimal port). Also note that `sys` is still disabled on the bare-arm port, to keep that ultra minimal. It means we now have bare-arm without `sys` and the minimal port with `sys`. That will allow different code size comparisons if/when new `sys` features are added. Signed-off-by: Damien George --- examples/embedding/mpconfigport.h | 1 + ports/bare-arm/mpconfigport.h | 3 +++ ports/minimal/mpconfigport.h | 6 ++++++ ports/nrf/mpconfigport.h | 1 - ports/powerpc/mpconfigport.h | 1 - ports/unix/variants/minimal/mpconfigvariant.h | 3 +-- ports/zephyr/mpconfigport_minimal.h | 1 - py/mpconfig.h | 2 +- 8 files changed, 12 insertions(+), 6 deletions(-) diff --git a/examples/embedding/mpconfigport.h b/examples/embedding/mpconfigport.h index ed34a5d766aeb..b7a38aa81a752 100644 --- a/examples/embedding/mpconfigport.h +++ b/examples/embedding/mpconfigport.h @@ -13,3 +13,4 @@ #define MICROPY_ENABLE_COMPILER (1) #define MICROPY_ENABLE_GC (1) #define MICROPY_PY_GC (1) +#define MICROPY_PY_SYS (0) diff --git a/ports/bare-arm/mpconfigport.h b/ports/bare-arm/mpconfigport.h index 65bb67f7b9a70..f4b87f23eff41 100644 --- a/ports/bare-arm/mpconfigport.h +++ b/ports/bare-arm/mpconfigport.h @@ -37,6 +37,9 @@ // Python internal features #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NONE) +// Fine control over Python builtins, classes, modules, etc. +#define MICROPY_PY_SYS (0) + // Type definitions for the specific machine typedef int32_t mp_int_t; // must be pointer size diff --git a/ports/minimal/mpconfigport.h b/ports/minimal/mpconfigport.h index 87287cbc6f359..b542c124d43c7 100644 --- a/ports/minimal/mpconfigport.h +++ b/ports/minimal/mpconfigport.h @@ -21,6 +21,12 @@ // Use the minimum headroom in the chunk allocator for parse nodes. #define MICROPY_ALLOC_PARSE_CHUNK_INIT (16) +// Disable all optional sys module features. +#define MICROPY_PY_SYS_MODULES (0) +#define MICROPY_PY_SYS_EXIT (0) +#define MICROPY_PY_SYS_PATH (0) +#define MICROPY_PY_SYS_ARGV (0) + // type definitions for the specific machine typedef intptr_t mp_int_t; // must be pointer size diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index d944fc8a11e71..d361d01ccefa9 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -289,7 +289,6 @@ #define MICROPY_PY_GENERATOR_PEND_THROW (1) #define MICROPY_PY_MATH (1) #define MICROPY_PY_STRUCT (1) -#define MICROPY_PY_SYS (1) #define MICROPY_PY_SYS_PATH_ARGV_DEFAULTS (1) #endif diff --git a/ports/powerpc/mpconfigport.h b/ports/powerpc/mpconfigport.h index 091e94bdafdf2..fcb66bab1e995 100644 --- a/ports/powerpc/mpconfigport.h +++ b/ports/powerpc/mpconfigport.h @@ -81,7 +81,6 @@ #define MICROPY_PY_CMATH (0) #define MICROPY_PY_IO (0) #define MICROPY_PY_STRUCT (1) -#define MICROPY_PY_SYS (1) #define MICROPY_MODULE_FROZEN_MPY (1) #define MICROPY_CPYTHON_COMPAT (0) #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) diff --git a/ports/unix/variants/minimal/mpconfigvariant.h b/ports/unix/variants/minimal/mpconfigvariant.h index 97ed786b8f409..2edac41c7fd12 100644 --- a/ports/unix/variants/minimal/mpconfigvariant.h +++ b/ports/unix/variants/minimal/mpconfigvariant.h @@ -61,6 +61,5 @@ #define MICROPY_PY_BUILTINS_RANGE_ATTRS (1) #define MICROPY_PY_GENERATOR_PEND_THROW (1) -// Enable just the sys and os built-in modules. -#define MICROPY_PY_SYS (1) +// Add just the os built-in module. #define MICROPY_PY_OS (1) diff --git a/ports/zephyr/mpconfigport_minimal.h b/ports/zephyr/mpconfigport_minimal.h index 24e0c9f1adc05..c802ba5254042 100644 --- a/ports/zephyr/mpconfigport_minimal.h +++ b/ports/zephyr/mpconfigport_minimal.h @@ -59,7 +59,6 @@ #define MICROPY_PY_BUILTINS_STR_OP_MODULO (1) #define MICROPY_PY_BUILTINS_BYTEARRAY (1) #define MICROPY_PY_BUILTINS_DICT_FROMKEYS (1) -#define MICROPY_PY_SYS (1) #ifdef CONFIG_BOARD #define MICROPY_HW_BOARD_NAME "zephyr-" CONFIG_BOARD diff --git a/py/mpconfig.h b/py/mpconfig.h index 877b262c8b7ae..303eb08f90bea 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1610,7 +1610,7 @@ typedef time_t mp_timestamp_t; // Whether to provide "sys" module #ifndef MICROPY_PY_SYS -#define MICROPY_PY_SYS (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_CORE_FEATURES) +#define MICROPY_PY_SYS (1) #endif // Whether to initialise "sys.path" and "sys.argv" to their defaults in mp_init()