py/compile: Disallow 'import *' outside module level.

This check follows CPython's behaviour, because 'import *' always populates
the globals with the imported names, not locals.

Since it's safe to do this (doesn't lead to a crash or undefined behaviour)
the check is only enabled for MICROPY_CPYTHON_COMPAT.

Fixes issue #5121.
This commit is contained in:
Petr Viktorin
2019-09-20 09:16:34 +02:00
committed by Damien George
parent 26e90a0514
commit 25a9bccdee
4 changed files with 30 additions and 7 deletions

View File

@@ -1196,6 +1196,13 @@ STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
} while (0);
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
#if MICROPY_CPYTHON_COMPAT
if (comp->scope_cur->kind != SCOPE_MODULE) {
compile_syntax_error(comp, (mp_parse_node_t)pns, "import * not at module level");
return;
}
#endif
EMIT_ARG(load_const_small_int, import_level);
// build the "fromlist" tuple