summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul m. p. P <pmpp.pub@gmail.com>2019-07-19 00:54:12 +0200
committerDamien George <damien.p.george@gmail.com>2019-07-31 22:36:00 +1000
commit60f1063797716cfec14c84afafec69bf06127777 (patch)
tree519d822cb042395d881d60d90a267e03fe4055a1
parenta8e3201b376d931a77e66dc80293c570983f9c7b (diff)
py/runtime: Allow to override builtins.__import__ with Python func.
This patch adds a simple but powerful hook into the import system, in a CPython compatible way, by allowing to override builtins.__import__. This does introduce some overhead to all imports but it's minor: - the dict lookup of __import__ is bypassed if there are no modifications to the builtins module (which is the case at start up); - imports are not performance critical, usually done just at the start of a script; - compared to how much work is done in an import, looking up a value in a dict is a relatively small additional piece of work.
-rw-r--r--py/runtime.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/py/runtime.c b/py/runtime.c
index e50256605..70d795719 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -1335,7 +1335,17 @@ mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
args[3] = fromlist;
args[4] = level;
- // TODO lookup __import__ and call that instead of going straight to builtin implementation
+ #if MICROPY_CAN_OVERRIDE_BUILTINS
+ // Lookup __import__ and call that if it exists
+ mp_obj_dict_t *bo_dict = MP_STATE_VM(mp_module_builtins_override_dict);
+ if (bo_dict != NULL) {
+ mp_map_elem_t *import = mp_map_lookup(&bo_dict->map, MP_OBJ_NEW_QSTR(MP_QSTR___import__), MP_MAP_LOOKUP);
+ if (import != NULL) {
+ return mp_call_function_n_kw(import->value, 5, 0, args);
+ }
+ }
+ #endif
+
return mp_builtin___import__(5, args);
}