py/builtinimport: Change relative import's ValueError to ImportError.

Following CPython change, see https://bugs.python.org/issue37444.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2021-05-30 19:25:53 +10:00
parent c3199f5649
commit 53519e322a
3 changed files with 11 additions and 3 deletions

View File

@ -313,7 +313,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
// We must have some component left over to import from
if (p == this_name) {
mp_raise_ValueError(MP_ERROR_TEXT("can't perform relative import"));
mp_raise_msg(&mp_type_ImportError, MP_ERROR_TEXT("can't perform relative import"));
}
uint new_mod_l = (mod_len == 0 ? (size_t)(p - this_name) : (size_t)(p - this_name) + 1 + mod_len);

View File

@ -0,0 +1,8 @@
pkg __name__: pkg7
pkg __name__: pkg7.subpkg1
pkg __name__: pkg7.subpkg1.subpkg2
mod1
mod2
mod1.foo
mod2.bar
ImportError

View File

@ -7,5 +7,5 @@ print(bar)
# attempted relative import beyond top-level package
try:
from .... import mod1
except ValueError:
print("ValueError")
except ImportError:
print("ImportError")