diff options
author | Thomas Wouters <thomas@python.org> | 2000-08-17 22:55:00 (GMT) |
---|---|---|
committer | Thomas Wouters <thomas@python.org> | 2000-08-17 22:55:00 (GMT) |
commit | 5215225ea1e3200b572775639d567f5e3f489a15 (patch) | |
tree | 8202d4aebf513dc8c8a80adf018f13270386e658 /Python/compile.c | |
parent | 1d75a79c009e500923128716a02efbe86135e64e (diff) | |
download | cpython-5215225ea1e3200b572775639d567f5e3f489a15.zip cpython-5215225ea1e3200b572775639d567f5e3f489a15.tar.gz cpython-5215225ea1e3200b572775639d567f5e3f489a15.tar.bz2 |
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 7065647..e18f627 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; |