diff options
| author | Thomas Wouters <thomas@python.org> | 2000-08-17 22:55:00 +0000 |
|---|---|---|
| committer | Thomas Wouters <thomas@python.org> | 2000-08-17 22:55:00 +0000 |
| commit | e6ba0ed982fd756f94833696e493544e4d20ac8b (patch) | |
| tree | 5006429bc5a5b4d14488ba32d2eba64faf679d79 /Python/compile.c | |
| parent | 30c677960e18416d1e0d8336d4bd48a0fcd51e1b (diff) | |
| download | cpython-e6ba0ed982fd756f94833696e493544e4d20ac8b.tar.gz | |
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.
Diffstat (limited to 'Python/compile.c')
| -rw-r--r-- | Python/compile.c | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/Python/compile.c b/Python/compile.c index 706564780d..e18f627dd8 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2096,6 +2096,22 @@ com_raise_stmt(struct compiling *c, node *n) } 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) { int i; @@ -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; |
