summaryrefslogtreecommitdiff
path: root/py/builtinimport.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-02-20 18:00:44 +1100
committerDamien George <damien.p.george@gmail.com>2018-02-20 18:00:44 +1100
commit209936880df4d63730ea77cf4b6f84e5f2f56d7c (patch)
tree3799e168efc951c01d34c088253da895587a4675 /py/builtinimport.c
parent6e7819ee2ee20ec9f09feb40b68be5973797f874 (diff)
py/builtinimport: Add compile-time option to disable external imports.
The new option is MICROPY_ENABLE_EXTERNAL_IMPORT and is enabled by default so that the default behaviour is the same as before. With it disabled import is only supported for built-in modules, not for external files nor frozen modules. This allows to support targets that have no filesystem of any kind and that only have access to pre-supplied built-in modules implemented natively.
Diffstat (limited to 'py/builtinimport.c')
-rw-r--r--py/builtinimport.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c
index 19bc9fc95..b8ed096ca 100644
--- a/py/builtinimport.c
+++ b/py/builtinimport.c
@@ -44,6 +44,8 @@
#define DEBUG_printf(...) (void)0
#endif
+#if MICROPY_ENABLE_EXTERNAL_IMPORT
+
#define PATH_SEP_CHAR '/'
bool mp_obj_is_package(mp_obj_t module) {
@@ -473,4 +475,41 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
// Otherwise, we need to return top-level package
return top_module_obj;
}
+
+#else // MICROPY_ENABLE_EXTERNAL_IMPORT
+
+mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
+ // Check that it's not a relative import
+ if (n_args >= 5 && MP_OBJ_SMALL_INT_VALUE(args[4]) != 0) {
+ mp_raise_NotImplementedError("relative import");
+ }
+
+ // Check if module already exists, and return it if it does
+ qstr module_name_qstr = mp_obj_str_get_qstr(args[0]);
+ mp_obj_t module_obj = mp_module_get(module_name_qstr);
+ if (module_obj != MP_OBJ_NULL) {
+ return module_obj;
+ }
+
+ #if MICROPY_MODULE_WEAK_LINKS
+ // Check if there is a weak link to this module
+ mp_map_elem_t *el = mp_map_lookup((mp_map_t*)&mp_builtin_module_weak_links_map, MP_OBJ_NEW_QSTR(module_name_qstr), MP_MAP_LOOKUP);
+ if (el != NULL) {
+ // Found weak-linked module
+ mp_module_call_init(module_name_qstr, el->value);
+ return el->value;
+ }
+ #endif
+
+ // Couldn't find the module, so fail
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ mp_raise_msg(&mp_type_ImportError, "module not found");
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
+ "no module named '%q'", module_name_qstr));
+ }
+}
+
+#endif // MICROPY_ENABLE_EXTERNAL_IMPORT
+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin___import___obj, 1, 5, mp_builtin___import__);