[Python-checkins] r62825 - in python/branches/tlee-ast-optimize: Parser/asdl_c.py Python/Python-ast.c Python/optimize.c

thomas.lee python-checkins at python.org
Wed May 7 16:03:03 CEST 2008


Author: thomas.lee
Date: Wed May  7 16:03:02 2008
New Revision: 62825

Log:
Handle NULL values passed to Const a little nicer. Plays nice with more tests. Again added the code for optimizing "yield None" and "return None" to "yield" and "return", respectively.

Modified:
   python/branches/tlee-ast-optimize/Parser/asdl_c.py
   python/branches/tlee-ast-optimize/Python/Python-ast.c
   python/branches/tlee-ast-optimize/Python/optimize.c

Modified: python/branches/tlee-ast-optimize/Parser/asdl_c.py
==============================================================================
--- python/branches/tlee-ast-optimize/Parser/asdl_c.py	(original)
+++ python/branches/tlee-ast-optimize/Parser/asdl_c.py	Wed May  7 16:03:02 2008
@@ -292,14 +292,8 @@
         emit("{")
         emit("%s p;" % ctype, 1)
         for argtype, argname, opt in args:
-            # XXX: Const() hack to force Py_None if NULL given to ctor
-            if str(argname) == 'value' and str(name) == 'Const':
-                emit("if (!%s) {" % argname, 1)
-                emit("Py_INCREF(Py_None);", 2)
-                emit("%s = Py_None;" % argname, 2)
-                emit("}", 1)
             # XXX hack alert: false is allowed for a bool
-            elif not opt and not (argtype == "bool" or argtype == "int"):
+            if not opt and not (argtype == "bool" or argtype == "int"):
                 emit("if (!%s) {" % argname, 1)
                 emit("PyErr_SetString(PyExc_ValueError,", 2)
                 msg = "field %s is required for %s" % (argname, name)
@@ -421,6 +415,12 @@
             for f in t.fields:
                 self.visitField(f, t.name, sum=sum, depth=2)
             args = [f.name.value for f in t.fields] + [a.name.value for a in sum.attributes]
+            # XXX: hack for the Const() node: Py_None constants are valid here
+            if name.value == 'expr' and t.name.value == 'Const':
+                self.emit("if (%s == NULL) {" % args[0], 2)
+                self.emit("%s = Py_None;" % args[0], 3)
+                self.emit("Py_INCREF(%s);" % args[0], 3)
+                self.emit("}", 2)
             self.emit("*out = %s(%s);" % (t.name, self.buildArgs(args)), 2)
             self.emit("if (*out == NULL) goto failed;", 2)
             self.emit("return 0;", 2)

Modified: python/branches/tlee-ast-optimize/Python/Python-ast.c
==============================================================================
--- python/branches/tlee-ast-optimize/Python/Python-ast.c	(original)
+++ python/branches/tlee-ast-optimize/Python/Python-ast.c	Wed May  7 16:03:02 2008
@@ -1746,8 +1746,9 @@
 {
         expr_ty p;
         if (!value) {
-                Py_INCREF(Py_None);
-                value = Py_None;
+                PyErr_SetString(PyExc_ValueError,
+                                "field value is required for Const");
+                return NULL;
         }
         p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
         if (!p)
@@ -5094,6 +5095,10 @@
                         PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Const");
                         return 1;
                 }
+                if (value == NULL) {
+                        value = Py_None;
+                        Py_INCREF(value);
+                }
                 *out = Const(value, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
                 return 0;

Modified: python/branches/tlee-ast-optimize/Python/optimize.c
==============================================================================
--- python/branches/tlee-ast-optimize/Python/optimize.c	(original)
+++ python/branches/tlee-ast-optimize/Python/optimize.c	Wed May  7 16:03:02 2008
@@ -552,8 +552,12 @@
 {
     expr_ty expr = *expr_ptr;
     if (expr->v.Yield.value != NULL) {
+        expr_ty value;
         if (!optimize_expr(&expr->v.Yield.value, arena))
             return 0;
+        value = expr->v.Yield.value;
+        if (value->kind == Const_kind && value->v.Const.value == Py_None)
+            expr->v.Yield.value = NULL;
     }
     return 1;
 }
@@ -817,8 +821,12 @@
 {
     stmt_ty stmt = *stmt_ptr;
     if (stmt->v.Return.value != NULL) {
+        expr_ty value;
         if (!optimize_expr(&stmt->v.Return.value, arena))
             return 0;
+        value = stmt->v.Return.value;
+        if (value->kind == Const_kind && value->v.Const.value == Py_None)
+            stmt->v.Return.value = NULL;
     }
     return 1;
 }


More information about the Python-checkins mailing list