8000 py/builtinimport: Allow overriding of mp_builtin___import__. · micropython/micropython@ac16032 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac16032

Browse files
committed
py/builtinimport: Allow overriding of mp_builtin___import__.
This allows ports to override mp_builtin___import__. This is particularly useful in MicroPython applications where MICROPY_ENABLE_EXTERNAL_IMPORT has to be disabled due to its impact on build size (2% to 2.5% of the minimal port). By overriding the otherwise very minimal mp_builtin___import__, ports can still allow limited forms of application-specific imports. Signed-off-by: Laurens Valk <laurens@pybricks.com>
1 parent 6971992 commit ac16032

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

py/builtin.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@ MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj);
6464

6565
#endif
6666

67+
// A port can provide its own import handler to expand or fully replace
68+
// the mp_builtin___import___default function.
69+
mp_obj_t mp_builtin___import___default(size_t n_args, const mp_obj_t *args);
70+
#if MICROPY_MODULE_OVERRIDE_BUILTIN_IMPORT
6771
mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args);
72+
#else
73+
#define mp_builtin___import__ mp_builtin___import___default
74+
#endif
75+
6876
mp_obj_t mp_micropython_mem_info(size_t n_args, const mp_obj_t *args);
6977

7078
MP_DECLARE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj);

py/builtinimport.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ STATIC mp_obj_t process_import_at_level(qstr full_mod_name, qstr level_mod_name,
467467
return module_obj;
468468
}
469469

470-
mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
470+
mp_obj_t mp_builtin___import___default(size_t n_args, const mp_obj_t *args) {
471471
#if DEBUG_PRINT
472472
DEBUG_printf("__import__:\n");
473473
for (size_t i = 0; i < n_args; i++) {
@@ -566,7 +566,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
566566

567567
#else // MICROPY_ENABLE_EXTERNAL_IMPORT
568568

569-
mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
569+
mp_obj_t mp_builtin___import___default(size_t n_args, const mp_obj_t *args) {
570570
// Check that it's not a relative import
571571
if (n_args >= 5 && MP_OBJ_SMALL_INT_VALUE(args[4]) != 0) {
572572
mp_raise_NotImplementedError(MP_ERROR_TEXT("relative import"));

py/mpconfig.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,13 @@ typedef double mp_float_t;
860860
#define MICROPY_MODULE_OVERRIDE_MAIN_IMPORT (0)
861861
#endif
862862

863+
// Whether to allow the port to provide its own mp_builtin___import__
864+
// implementation. If enabled, the provided implementation can still call
865+
// mp_builtin___import___default to replicate and extend the default behavior.
866+
#ifndef MICROPY_MODULE_OVERRIDE_BUILTIN_IMPORT
867+
#define MICROPY_MODULE_OVERRIDE_BUILTIN_IMPORT (0)
868+
#endif
869+
863870
// Whether frozen modules are supported in the form of strings
864871
#ifndef MICROPY_MODULE_FROZEN_STR
865872
#define MICROPY_MODULE_FROZEN_STR (0)

0 commit comments

Comments
 (0)
0