Apply SF patch #101135, adding 'import module as m' and 'from module import

name as n'. By doing some twists and turns, "as" is not a reserved word.

There is a slight change in semantics for 'from module import name' (it will
now honour the 'global' keyword) but only in cases that are explicitly
undocumented.
This commit is contained in:
Thomas Wouters 2000-08-17 22:55:00 +00:00
parent 1d75a79c00
commit 5215225ea1
12 changed files with 970 additions and 818 deletions

View file

@ -2095,6 +2095,22 @@ com_raise_stmt(struct compiling *c, node *n)
com_pop(c, i);
}
static void
com_from_import(struct compiling *c, node *n)
{
com_addopname(c, IMPORT_FROM, CHILD(n, 0));
com_push(c, 1);
if (NCH(n) > 1) {
if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
com_error(c, PyExc_SyntaxError, "invalid syntax");
return;
}
com_addopname(c, STORE_NAME, CHILD(n, 2));
} else
com_addopname(c, STORE_NAME, CHILD(n, 0));
com_pop(c, 1);
}
static void
com_import_stmt(struct compiling *c, node *n)
{
@ -2107,18 +2123,32 @@ com_import_stmt(struct compiling *c, node *n)
REQ(CHILD(n, 1), dotted_name);
com_addopname(c, IMPORT_NAME, CHILD(n, 1));
com_push(c, 1);
for (i = 3; i < NCH(n); i += 2)
com_addopname(c, IMPORT_FROM, CHILD(n, i));
com_addbyte(c, POP_TOP);
if (TYPE(CHILD(n, 3)) == STAR)
com_addbyte(c, IMPORT_STAR);
else {
for (i = 3; i < NCH(n); i += 2)
com_from_import(c, CHILD(n, i));
com_addbyte(c, POP_TOP);
}
com_pop(c, 1);
}
else {
/* 'import' ... */
for (i = 1; i < NCH(n); i += 2) {
REQ(CHILD(n, i), dotted_name);
com_addopname(c, IMPORT_NAME, CHILD(n, i));
node *subn = CHILD(n, i);
REQ(subn, dotted_as_name);
com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
com_push(c, 1);
com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0));
if (NCH(subn) > 1) {
if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
com_error(c, PyExc_SyntaxError,
"invalid syntax");
return;
}
com_addopname(c, STORE_NAME, CHILD(subn, 2));
} else
com_addopname(c, STORE_NAME,
CHILD(CHILD(subn, 0),0));
com_pop(c, 1);
}
}
@ -3295,12 +3325,14 @@ optimize(struct compiling *c)
case IMPORT_FROM:
com_addlocal_o(c, GETNAMEOBJ(oparg));
break;
case IMPORT_STAR:
case EXEC_STMT:
c->c_flags &= ~CO_OPTIMIZED;
break;
}
}
/* TBD: Is this still necessary ? */
if (PyDict_GetItemString(c->c_locals, "*") != NULL)
c->c_flags &= ~CO_OPTIMIZED;