]> granicus.if.org Git - python/commitdiff
Fix SF bug #1519018: 'as' is now validated properly in import statements
authorNeal Norwitz <nnorwitz@gmail.com>
Sat, 8 Jul 2006 05:31:37 +0000 (05:31 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Sat, 8 Jul 2006 05:31:37 +0000 (05:31 +0000)
Lib/test/test_compile.py
Misc/NEWS
Python/ast.c

index 72c4f7e9286bfd03a2ab752333d0546467ac0cdb..814bc192956283de92a9504f46dc73a52493e38f 100644 (file)
@@ -238,6 +238,8 @@ if 1:
         succeed = [
             'import sys',
             'import os, sys',
+            'import os as bar',
+            'import os.path as bar',
             'from __future__ import nested_scopes, generators',
             'from __future__ import (nested_scopes,\ngenerators)',
             'from __future__ import (nested_scopes,\ngenerators,)',
@@ -257,6 +259,8 @@ if 1:
             'import (sys',
             'import sys)',
             'import (os,)',
+            'import os As bar',
+            'import os.path a bar',
             'from (sys) import stdin',
             'from __future__ import (nested_scopes',
             'from __future__ import nested_scopes)',
index 67c49a64ec474ba0abd48357c2660319183123f9..b31023d12ddf672fd70f69a5fdb278d2c1a06798 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,8 @@ Core and builtins
   omit a default "error" argument for NULL pointer.  This allows
   the parser to take a codec from cjkcodecs again.
 
+- Bug #1519018: 'as' is now validated properly in import statements.
+
 Library
 -------
 
index f3e611b8e375576feeea115420df2dd4cceb0bf2..4c78b004f3e804d0e737bbe1b91e0ac32a0de32f 100644 (file)
@@ -2142,7 +2142,14 @@ alias_for_import_name(struct compiling *c, const node *n)
  loop:
     switch (TYPE(n)) {
         case import_as_name:
-            str = (NCH(n) == 3) ? NEW_IDENTIFIER(CHILD(n, 2)) : NULL;
+            str = NULL;
+            if (NCH(n) == 3) {
+                if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
+                    ast_error(n, "must use 'as' in import");
+                    return NULL;
+                }
+                str = NEW_IDENTIFIER(CHILD(n, 2));
+            }
             return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena);
         case dotted_as_name:
             if (NCH(n) == 1) {
@@ -2151,6 +2158,10 @@ alias_for_import_name(struct compiling *c, const node *n)
             }
             else {
                 alias_ty a = alias_for_import_name(c, CHILD(n, 0));
+                if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
+                    ast_error(n, "must use 'as' in import");
+                    return NULL;
+                }
                 assert(!a->asname);
                 a->asname = NEW_IDENTIFIER(CHILD(n, 2));
                 return a;