[Python-3000-checkins] r55665 - in python/branches/p3yk: Grammar/Grammar Python/ast.c

guido.van.rossum python-3000-checkins at python.org
Wed May 30 04:45:46 CEST 2007


Author: guido.van.rossum
Date: Wed May 30 04:45:43 2007
New Revision: 55665

Modified:
   python/branches/p3yk/Grammar/Grammar
   python/branches/p3yk/Python/ast.c
Log:
Make None, True, False keywords.
We can now also delete all the other places that explicitly forbid
assignment to None, but I'm not going to bother right now.


Modified: python/branches/p3yk/Grammar/Grammar
==============================================================================
--- python/branches/p3yk/Grammar/Grammar	(original)
+++ python/branches/p3yk/Grammar/Grammar	Wed May 30 04:45:43 2007
@@ -100,7 +100,7 @@
 atom: ('(' [yield_expr|testlist_comp] ')' |
        '[' [testlist_comp] ']' |
        '{' [dictorsetmaker] '}' |
-       NAME | NUMBER | STRING+ | '...')
+       NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')
 testlist_comp: test ( comp_for | (',' test)* [','] )
 trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
 subscriptlist: subscript (',' subscript)* [',']

Modified: python/branches/p3yk/Python/ast.c
==============================================================================
--- python/branches/p3yk/Python/ast.c	(original)
+++ python/branches/p3yk/Python/ast.c	Wed May 30 04:45:43 2007
@@ -324,6 +324,29 @@
     }
 }
 
+static const char* FORBIDDEN[] = {
+    "None",
+    "True",
+    "False",
+    NULL,
+};
+
+static int
+forbidden_name(expr_ty e, const node *n)
+{
+    const char *id;
+    const char **p;
+    assert(PyString_Check(e->v.Name.id));
+    id = PyString_AS_STRING(e->v.Name.id);
+    for (p = FORBIDDEN; *p; p++) {
+        if (strcmp(*p, id) == 0) {
+            ast_error(n, "assignment to keyword");
+            return 1;
+        }
+    }
+    return 0;
+}
+
 /* Set the context ctx for expr_ty e, recursively traversing e.
 
    Only sets context for expr kinds that "can appear in assignment context"
@@ -366,9 +389,9 @@
                 return 0;
             break;
         case Name_kind:
-            if (ctx == Store &&
-                !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
-                    return ast_error(n, "assignment to None");
+            if (ctx == Store) {
+                if (forbidden_name(e, n))
+                    return 0; /* forbidden_name() calls ast_error() */
             }
             e->v.Name.ctx = ctx;
             break;
@@ -1227,6 +1250,7 @@
 {
     /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
        | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
+       | '...' | 'None' | 'True' | 'False'
     */
     node *ch = CHILD(n, 0);
     int bytesmode = 0;


More information about the Python-3000-checkins mailing list