[Python-checkins] r73405 - in python/trunk: Lib/test/test_compile.py Misc/NEWS Python/ast.c

benjamin.peterson python-checkins at python.org
Sat Jun 13 05:46:31 CEST 2009


Author: benjamin.peterson
Date: Sat Jun 13 05:46:30 2009
New Revision: 73405

Log:
prevent import statements from assigning to None

Modified:
   python/trunk/Lib/test/test_compile.py
   python/trunk/Misc/NEWS
   python/trunk/Python/ast.c

Modified: python/trunk/Lib/test/test_compile.py
==============================================================================
--- python/trunk/Lib/test/test_compile.py	(original)
+++ python/trunk/Lib/test/test_compile.py	Sat Jun 13 05:46:30 2009
@@ -270,11 +270,17 @@
             '(a, None) = 0, 0',
             'for None in range(10): pass',
             'def f(None): pass',
+            'import None',
+            'import x as None',
+            'from x import None',
+            'from x import y as None'
         ]
         for stmt in stmts:
             stmt += "\n"
             self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'single')
             self.assertRaises(SyntaxError, compile, stmt, 'tmp', 'exec')
+        # This is ok.
+        compile("from None import x", "tmp", "exec")
 
     def test_import(self):
         succeed = [

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sat Jun 13 05:46:30 2009
@@ -12,6 +12,8 @@
 Core and Builtins
 -----------------
 
+- Assignment to None using import statements now raises a SyntaxError.
+
 - In the slice AST type, the step field will always be None if a step expression
   is not specified.
 

Modified: python/trunk/Python/ast.c
==============================================================================
--- python/trunk/Python/ast.c	(original)
+++ python/trunk/Python/ast.c	Sat Jun 13 05:46:30 2009
@@ -2294,7 +2294,7 @@
 }
 
 static alias_ty
-alias_for_import_name(struct compiling *c, const node *n)
+alias_for_import_name(struct compiling *c, const node *n, int store)
 {
     /*
       import_as_name: NAME ['as' NAME]
@@ -2305,28 +2305,38 @@
 
  loop:
     switch (TYPE(n)) {
-        case import_as_name:
+         case import_as_name: {
+            node *name_node = CHILD(n, 0);
             str = NULL;
             if (NCH(n) == 3) {
-                str = NEW_IDENTIFIER(CHILD(n, 2));
+                node *str_node = CHILD(n, 2);
+                if (store && !forbidden_check(c, str_node, STR(str_node)))
+                    return NULL;
+                str = NEW_IDENTIFIER(str_node);
                 if (!str)
                     return NULL;
             }
-            name = NEW_IDENTIFIER(CHILD(n, 0));
+            if (!forbidden_check(c, name_node, STR(name_node)))
+                return NULL;
+            name = NEW_IDENTIFIER(name_node);
             if (!name)
                 return NULL;
             return alias(name, str, c->c_arena);
+        }
         case dotted_as_name:
             if (NCH(n) == 1) {
                 n = CHILD(n, 0);
                 goto loop;
             }
             else {
-                alias_ty a = alias_for_import_name(c, CHILD(n, 0));
+                node *asname_node = CHILD(n, 2);
+                alias_ty a = alias_for_import_name(c, CHILD(n, 0), store);
                 if (!a)
                     return NULL;
                 assert(!a->asname);
-                a->asname = NEW_IDENTIFIER(CHILD(n, 2));
+                if (store && !forbidden_check(c, asname_node, STR(asname_node)))
+                    return NULL;
+                a->asname = NEW_IDENTIFIER(asname_node);
                 if (!a->asname)
                     return NULL;
                 return a;
@@ -2334,7 +2344,10 @@
             break;
         case dotted_name:
             if (NCH(n) == 1) {
-                name = NEW_IDENTIFIER(CHILD(n, 0));
+                node *name_node = CHILD(n, 0);
+                if (store && !forbidden_check(c, name_node, STR(name_node)))
+                    return NULL;
+                name = NEW_IDENTIFIER(name_node);
                 if (!name)
                     return NULL;
                 return alias(name, NULL, c->c_arena);
@@ -2408,7 +2421,7 @@
         if (!aliases)
             return NULL;
         for (i = 0; i < NCH(n); i += 2) {
-            alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
+            alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
             if (!import_alias)
                 return NULL;
             asdl_seq_SET(aliases, i / 2, import_alias);
@@ -2425,7 +2438,9 @@
           optional module name */
         for (idx = 1; idx < NCH(n); idx++) {
             if (TYPE(CHILD(n, idx)) == dotted_name) {
-                mod = alias_for_import_name(c, CHILD(n, idx));
+                mod = alias_for_import_name(c, CHILD(n, idx), 0);
+                if (!mod)
+                    return NULL;
                 idx++;
                 break;
             } else if (TYPE(CHILD(n, idx)) != DOT) {
@@ -2466,14 +2481,14 @@
 
         /* handle "from ... import *" special b/c there's no children */
         if (TYPE(n) == STAR) {
-            alias_ty import_alias = alias_for_import_name(c, n);
+            alias_ty import_alias = alias_for_import_name(c, n, 1);
             if (!import_alias)
                 return NULL;
                 asdl_seq_SET(aliases, 0, import_alias);
         }
         else {
             for (i = 0; i < NCH(n); i += 2) {
-                alias_ty import_alias = alias_for_import_name(c, CHILD(n, i));
+                alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
                 if (!import_alias)
                     return NULL;
                     asdl_seq_SET(aliases, i / 2, import_alias);


More information about the Python-checkins mailing list