diff options
-rw-r--r-- | py/builtinimport.c | 27 |
1 files changed, 6 insertions, 21 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c index 173e040afb..5142c7d8f5 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -261,6 +261,9 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { fromtuple = args[3]; if (n_args >= 5) { level = MP_OBJ_SMALL_INT_VALUE(args[4]); + if (level < 0) { + mp_raise_ValueError(NULL); + } } } @@ -305,28 +308,13 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { chop_component(this_name, &p); } - - uint dots_seen = 0; while (level--) { chop_component(this_name, &p); - dots_seen++; } - if (dots_seen == 0 && level >= 1) { - // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name - // "If the module's name does not contain any package information - // (e.g. it is set to '__main__') then relative imports are - // resolved as if the module were a top level module, regardless - // of where the module is actually located on the file system." - // Supposedly this if catches this condition and resolve it properly - // TODO: But nobody knows for sure. This condition happens when - // package's __init__.py does something like "import .submod". So, - // maybe we should check for package here? But quote above doesn't - // talk about packages, it talks about dot-less module names. - DEBUG_printf("Warning: no dots in current module name and level>0\n"); - p = this_name + this_name_l; - } else if (level != -1) { - mp_raise_msg(&mp_type_ImportError, "invalid relative import"); + // We must have some component left over to import from + if (p == this_name) { + mp_raise_ValueError("cannot perform relative import"); } uint new_mod_l = (mod_len == 0 ? (size_t)(p - this_name) : (size_t)(p - this_name) + 1 + mod_len); @@ -339,9 +327,6 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { qstr new_mod_q = qstr_from_strn(new_mod, new_mod_l); DEBUG_printf("Resolved base name for relative import: '%s'\n", qstr_str(new_mod_q)); - if (new_mod_q == MP_QSTR_) { - mp_raise_ValueError("cannot perform relative import"); - } module_name = MP_OBJ_NEW_QSTR(new_mod_q); mod_str = new_mod; mod_len = new_mod_l; |