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

Skip to content

Commit 68e7455

Browse files
committed
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 <laurens@pybricks.com>
1 parent c616721 commit 68e7455

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

py/builtin.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ 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);
68-
mp_obj_t mp_micropython_mem_info(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);
6973

7074
MP_DECLARE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj);
7175
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___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"));

0 commit comments

Comments
 (0)
0