Browse Source

py/builtinimport: Raise ValueError for bad relative import, per CPython.

pull/2797/head
Damien George 8 years ago
parent
commit
63e291de70
  1. 3
      py/builtinimport.c
  2. 4
      tests/import/pkg7/subpkg1/subpkg2/mod3.py

3
py/builtinimport.c

@ -360,8 +360,7 @@ 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_) {
// CPython raises SystemError
mp_raise_msg(&mp_type_ImportError, "cannot perform relative import");
mp_raise_msg(&mp_type_ValueError, "cannot perform relative import");
}
module_name = MP_OBJ_NEW_QSTR(new_mod_q);
mod_str = new_mod;

4
tests/import/pkg7/subpkg1/subpkg2/mod3.py

@ -7,5 +7,5 @@ print(bar)
# whereas CPython raises a ValueError
try:
from .... import mod1
except (ImportError, ValueError):
print('Error')
except ValueError:
print('ValueError')

Loading…
Cancel
Save