Skip to content

Commit d8ad878

Browse files
laurensvalkdpgeorge
authored andcommitted
py/builtinimport: Allow overriding of mp_builtin___import__.
This allows ports to override mp_builtin___import__. This can be 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 <[email protected]>
1 parent 3d65101 commit d8ad878

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

py/builtin.h

+6
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj);
6464

6565
#endif
6666

67+
// A port can provide its own import handler by defining mp_builtin___import__.
68+
#ifndef mp_builtin___import__
69+
#define mp_builtin___import__ mp_builtin___import___default
70+
#endif
6771
mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args);
72+
mp_obj_t mp_builtin___import___default(size_t n_args, const mp_obj_t *args);
73+
6874
mp_obj_t mp_micropython_mem_info(size_t n_args, const mp_obj_t *args);
6975

7076
MP_DECLARE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj);

py/builtinimport.c

+2-2
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"));

0 commit comments

Comments
 (0)