diff options
author | Damien George <damien.p.george@gmail.com> | 2017-01-16 16:54:56 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-06-28 12:21:29 +1000 |
commit | 2f7fad66a2b3d51772854575b080518c92eefde8 (patch) | |
tree | 4b625bc7c7b08ea73aa65ff7786c5b21ba265b92 /py/builtinimport.c | |
parent | ebb93962741d68e4530cb89d207641c1d663f61f (diff) | |
download | micropython-2f7fad66a2b3d51772854575b080518c92eefde8.tar.gz micropython-2f7fad66a2b3d51772854575b080518c92eefde8.zip |
py/builtinimport: Remove unreachable code for relative imports.
The while-loop that calls chop_component will guarantee that level==-1 at
the end of the loop. Hence the code following it is unnecessary.
The check for p==this_name will catch imports that are beyond the
top-level, and also covers the case of new_mod_q==MP_QSTR_ (equivalent to
new_mod_l==0) so that check is removed.
There is also a new check at the start for level>=0 to guard against
__import__ being called with bad level values.
Diffstat (limited to 'py/builtinimport.c')
-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; |