Skip to content

Commit 2f7fad6

Browse files
committed
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.
1 parent ebb9396 commit 2f7fad6

1 file changed

Lines changed: 6 additions & 21 deletions

File tree

py/builtinimport.c

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
261261
fromtuple = args[3];
262262
if (n_args >= 5) {
263263
level = MP_OBJ_SMALL_INT_VALUE(args[4]);
264+
if (level < 0) {
265+
mp_raise_ValueError(NULL);
266+
}
264267
}
265268
}
266269

@@ -305,28 +308,13 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
305308
chop_component(this_name, &p);
306309
}
307310

308-
309-
uint dots_seen = 0;
310311
while (level--) {
311312
chop_component(this_name, &p);
312-
dots_seen++;
313313
}
314314

315-
if (dots_seen == 0 && level >= 1) {
316-
// http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name
317-
// "If the module's name does not contain any package information
318-
// (e.g. it is set to '__main__') then relative imports are
319-
// resolved as if the module were a top level module, regardless
320-
// of where the module is actually located on the file system."
321-
// Supposedly this if catches this condition and resolve it properly
322-
// TODO: But nobody knows for sure. This condition happens when
323-
// package's __init__.py does something like "import .submod". So,
324-
// maybe we should check for package here? But quote above doesn't
325-
// talk about packages, it talks about dot-less module names.
326-
DEBUG_printf("Warning: no dots in current module name and level>0\n");
327-
p = this_name + this_name_l;
328-
} else if (level != -1) {
329-
mp_raise_msg(&mp_type_ImportError, "invalid relative import");
315+
// We must have some component left over to import from
316+
if (p == this_name) {
317+
mp_raise_ValueError("cannot perform relative import");
330318
}
331319

332320
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) {
339327

340328
qstr new_mod_q = qstr_from_strn(new_mod, new_mod_l);
341329
DEBUG_printf("Resolved base name for relative import: '%s'\n", qstr_str(new_mod_q));
342-
if (new_mod_q == MP_QSTR_) {
343-
mp_raise_ValueError("cannot perform relative import");
344-
}
345330
module_name = MP_OBJ_NEW_QSTR(new_mod_q);
346331
mod_str = new_mod;
347332
mod_len = new_mod_l;

0 commit comments

Comments
 (0)