mirror of https://github.com/python/cpython
Disallow "import mod.submod as m", because the result is ambiguous. Does it
load mod.submod as m, or mod as m ? Both can be achieved differently, and unambiguously. Also attempt to document this restriction (editor appreciated!) Note that this is an artificial check during compile, because incorporating this in the grammar is hard, and then adjusting the compiler to do the right thing with the right nodes is harder.
This commit is contained in:
parent
15446d344d
commit
8bad612881
|
@ -498,7 +498,12 @@ begin.
|
||||||
The first form of \keyword{import} statement binds the module name in the
|
The first form of \keyword{import} statement binds the module name in the
|
||||||
local namespace to the module object, and then goes on to import the
|
local namespace to the module object, and then goes on to import the
|
||||||
next identifier, if any. If the module name is followed by \keyword{as},
|
next identifier, if any. If the module name is followed by \keyword{as},
|
||||||
the name following \keyword{as} is used as the local name for the module.
|
the name following \keyword{as} is used as the local name for the module. To
|
||||||
|
avoid confusion, you cannot import sub-modules 'as' a different
|
||||||
|
local name. So 'import module as m' is legal, but 'import module.submod as
|
||||||
|
s' is not. The latter should be written as 'from module import submod as s',
|
||||||
|
see below.
|
||||||
|
|
||||||
The \keyword{from} form does not bind the module name: it goes through the
|
The \keyword{from} form does not bind the module name: it goes through the
|
||||||
list of identifiers, looks each one of them up in the module found in step
|
list of identifiers, looks each one of them up in the module found in step
|
||||||
(1), and binds the name in the local namespace to the object thus found.
|
(1), and binds the name in the local namespace to the object thus found.
|
||||||
|
|
|
@ -2139,7 +2139,8 @@ com_import_stmt(struct compiling *c, node *n)
|
||||||
com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
|
com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
|
||||||
com_push(c, 1);
|
com_push(c, 1);
|
||||||
if (NCH(subn) > 1) {
|
if (NCH(subn) > 1) {
|
||||||
if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
|
if (strcmp(STR(CHILD(subn, 1)), "as") != 0 ||
|
||||||
|
NCH(CHILD(subn, 0)) > 1) {
|
||||||
com_error(c, PyExc_SyntaxError,
|
com_error(c, PyExc_SyntaxError,
|
||||||
"invalid syntax");
|
"invalid syntax");
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in New Issue