[Python-checkins] r62815 - in python/branches/tlee-ast-optimize: Include/Python-ast.h Lib/test/test_optimizer.py Parser/asdl_c.py Python/Python-ast.c Python/compile.c Python/optimize.c Python/peephole.c

thomas.lee python-checkins at python.org
Wed May 7 10:33:07 CEST 2008


Author: thomas.lee
Date: Wed May  7 10:33:06 2008
New Revision: 62815

Log:
Roll back r62706. Annotations get complicated.

Modified:
   python/branches/tlee-ast-optimize/Include/Python-ast.h
   python/branches/tlee-ast-optimize/Lib/test/test_optimizer.py
   python/branches/tlee-ast-optimize/Parser/asdl_c.py
   python/branches/tlee-ast-optimize/Python/Python-ast.c
   python/branches/tlee-ast-optimize/Python/compile.c
   python/branches/tlee-ast-optimize/Python/optimize.c
   python/branches/tlee-ast-optimize/Python/peephole.c

Modified: python/branches/tlee-ast-optimize/Include/Python-ast.h
==============================================================================
--- python/branches/tlee-ast-optimize/Include/Python-ast.h	(original)
+++ python/branches/tlee-ast-optimize/Include/Python-ast.h	Wed May  7 10:33:06 2008
@@ -57,7 +57,6 @@
                 } Suite;
                 
         } v;
-        PyObject* annotations;
 };
 
 enum _stmt_kind {FunctionDef_kind=1, ClassDef_kind=2, Return_kind=3,
@@ -183,7 +182,6 @@
         } v;
         int lineno;
         int col_offset;
-        PyObject* annotations;
 };
 
 enum _expr_kind {BoolOp_kind=1, BinOp_kind=2, UnaryOp_kind=3, Lambda_kind=4,
@@ -297,7 +295,6 @@
         } v;
         int lineno;
         int col_offset;
-        PyObject* annotations;
 };
 
 enum _slice_kind {Ellipsis_kind=1, Slice_kind=2, ExtSlice_kind=3, Index_kind=4};
@@ -319,7 +316,6 @@
                 } Index;
                 
         } v;
-        PyObject* annotations;
 };
 
 struct _comprehension {
@@ -341,7 +337,6 @@
         } v;
         int lineno;
         int col_offset;
-        PyObject* annotations;
 };
 
 struct _arguments {

Modified: python/branches/tlee-ast-optimize/Lib/test/test_optimizer.py
==============================================================================
--- python/branches/tlee-ast-optimize/Lib/test/test_optimizer.py	(original)
+++ python/branches/tlee-ast-optimize/Lib/test/test_optimizer.py	Wed May  7 10:33:06 2008
@@ -97,7 +97,7 @@
 
         for code, expected in tests:
             ast = self.compileast(code)
-            actual = ast.body[0].value.annotations['const']
+            actual = getattr(ast.body[0].value, attrmap[type(expected)])
             self.assertEqual(expected, actual)
 
     def test_binop_fold_num_with_variable(self):
@@ -118,8 +118,8 @@
         self.assertEqual(_ast.Expr, ast.body[1].__class__)
         self.assertEqual(_ast.BinOp, ast.body[1].value.__class__)
         self.assertEqual(_ast.Name, ast.body[1].value.left.__class__)
-        self.assertEqual(_ast.BinOp, ast.body[1].value.right.__class__)
-        self.assertEqual(6, ast.body[1].value.right.annotations['const'])
+        self.assertEqual(_ast.Num, ast.body[1].value.right.__class__)
+        self.assertEqual(6, ast.body[1].value.right.n)
 
     def test_binop_failure_left_until_runtime(self):
         ast = self.compileast("5 + '3'")
@@ -134,22 +134,13 @@
         except TypeError:
             pass
 
-    def assertConstant(self, expected, code):
-        ast = self.compileast(code).body[0].value
-        if type(ast) is _ast.Str:
-            self.assertEqual(expected, ast.s)
-        elif type(ast) is _ast.Num:
-            self.assertEqual(expected, ast.n)
-        else:
-            self.assertEqual(expected, ast.annotations['const'])
-
     def test_unary_fold_num(self):
         # check unary constant folding for numeric values
-        self.assertConstant(-5, '-5')
-        self.assertConstant(True, "not 0")
-        self.assertConstant(-3, "-+3")
-        self.assertConstant(False, "not True")
-        self.assertConstant(True, "not None")
+        self.assertNum(-5, "-5")
+        self.assertNum(True, "not 0")
+        self.assertNum(-3, "-+3")
+        self.assertNum(False, "not True")
+        self.assertNum(True, "not None")
 
     def test_unary_failure_left_until_runtime(self):
         ast = self.compileast("~'bad!'")
@@ -197,13 +188,6 @@
         self.assertEqual(3, len(ast.body[0].body))
         self.assertEqual(_ast.Pass, ast.body[0].body[2].__class__)
 
-    def test_fold_tuple_of_constants(self):
-        ast = self.compileast('(1, 2, 3)')
-        self.assertEqual((1, 2, 3), ast.body[0].value.annotations['const'])
-
-        ast = self.compileast('((1, 2), 3)')
-        self.assertEqual(((1, 2), 3), ast.body[0].value.annotations['const'])
-
 def test_main():
     test_support.run_unittest(AstOptimizerTest)
 

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 10:33:06 2008
@@ -170,7 +170,6 @@
             type = str(field.type)
             assert type in asdl.builtin_types, type
             emit("%s %s;" % (type, field.name), depth + 1);
-        emit("PyObject* annotations;", depth + 1)
         emit("};")
         emit("")
 
@@ -322,7 +321,6 @@
             emit("p->v.%s.%s = %s;" % (name, argname, argname), 1)
         for argtype, argname, opt in attrs:
             emit("p->%s = %s;" % (argname, argname), 1)
-        emit("p->annotations = NULL;", 1)
 
     def emit_body_struct(self, name, args, attrs):
         def emit(s, depth=0, reflow=1):
@@ -419,9 +417,6 @@
             args = [f.name.value for f in t.fields] + [a.name.value for a in sum.attributes]
             self.emit("*out = %s(%s);" % (t.name, self.buildArgs(args)), 2)
             self.emit("if (*out == NULL) goto failed;", 2)
-            self.emit('(*out)->annotations = PyObject_GetAttrString(obj, "annotations");', 2)
-            self.emit('if ((*out)->annotations == NULL)', 2)
-            self.emit('PyErr_Clear();', 3)
             self.emit("return 0;", 2)
             self.emit("}", 1)
         self.sumTrailer(name)
@@ -985,9 +980,6 @@
             self.emit('if (PyObject_SetAttrString(result, "%s", value) < 0)' % a.name, 1)
             self.emit('goto failed;', 2)
             self.emit('Py_DECREF(value);', 1)
-        if not is_simple(sum):
-            self.emit('if (o->annotations && PyObject_SetAttrString(result, "annotations", o->annotations) < 0)', 1)
-            self.emit('goto failed;', 2)
         self.func_end()
 
     def simpleSum(self, sum, name):

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 10:33:06 2008
@@ -936,7 +936,6 @@
                 return NULL;
         p->kind = Module_kind;
         p->v.Module.body = body;
-        p->annotations = NULL;
         return p;
 }
 
@@ -949,7 +948,6 @@
                 return NULL;
         p->kind = Interactive_kind;
         p->v.Interactive.body = body;
-        p->annotations = NULL;
         return p;
 }
 
@@ -967,7 +965,6 @@
                 return NULL;
         p->kind = Expression_kind;
         p->v.Expression.body = body;
-        p->annotations = NULL;
         return p;
 }
 
@@ -980,7 +977,6 @@
                 return NULL;
         p->kind = Suite_kind;
         p->v.Suite.body = body;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1009,7 +1005,6 @@
         p->v.FunctionDef.decorator_list = decorator_list;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1033,7 +1028,6 @@
         p->v.ClassDef.decorator_list = decorator_list;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1048,7 +1042,6 @@
         p->v.Return.value = value;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1063,7 +1056,6 @@
         p->v.Delete.targets = targets;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1085,7 +1077,6 @@
         p->v.Assign.value = value;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1118,7 +1109,6 @@
         p->v.AugAssign.value = value;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1136,7 +1126,6 @@
         p->v.Print.nl = nl;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1165,7 +1154,6 @@
         p->v.For.orelse = orelse;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1188,7 +1176,6 @@
         p->v.While.orelse = orelse;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1211,7 +1198,6 @@
         p->v.If.orelse = orelse;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1234,7 +1220,6 @@
         p->v.With.body = body;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1252,7 +1237,6 @@
         p->v.Raise.tback = tback;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1270,7 +1254,6 @@
         p->v.TryExcept.orelse = orelse;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1287,7 +1270,6 @@
         p->v.TryFinally.finalbody = finalbody;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1308,7 +1290,6 @@
         p->v.Assert.msg = msg;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1323,7 +1304,6 @@
         p->v.Import.names = names;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1346,7 +1326,6 @@
         p->v.ImportFrom.level = level;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1369,7 +1348,6 @@
         p->v.Exec.locals = locals;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1384,7 +1362,6 @@
         p->v.Global.names = names;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1404,7 +1381,6 @@
         p->v.Expr.value = value;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1418,7 +1394,6 @@
         p->kind = Pass_kind;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1432,7 +1407,6 @@
         p->kind = Break_kind;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1446,7 +1420,6 @@
         p->kind = Continue_kind;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1468,7 +1441,6 @@
         p->v.BoolOp.values = values;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1501,7 +1473,6 @@
         p->v.BinOp.right = right;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1528,7 +1499,6 @@
         p->v.UnaryOp.operand = operand;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1555,7 +1525,6 @@
         p->v.Lambda.body = body;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1588,7 +1557,6 @@
         p->v.IfExp.orelse = orelse;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1605,7 +1573,6 @@
         p->v.Dict.values = values;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1627,7 +1594,6 @@
         p->v.ListComp.generators = generators;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1649,7 +1615,6 @@
         p->v.GeneratorExp.generators = generators;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1664,7 +1629,6 @@
         p->v.Yield.value = value;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1687,7 +1651,6 @@
         p->v.Compare.comparators = comparators;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1712,7 +1675,6 @@
         p->v.Call.kwargs = kwargs;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1732,7 +1694,6 @@
         p->v.Repr.value = value;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1752,7 +1713,6 @@
         p->v.Num.n = n;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1772,7 +1732,6 @@
         p->v.Str.s = s;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1805,7 +1764,6 @@
         p->v.Attribute.ctx = ctx;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1838,7 +1796,6 @@
         p->v.Subscript.ctx = ctx;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1865,7 +1822,6 @@
         p->v.Name.ctx = ctx;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1887,7 +1843,6 @@
         p->v.List.ctx = ctx;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1909,7 +1864,6 @@
         p->v.Tuple.ctx = ctx;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1921,7 +1875,6 @@
         if (!p)
                 return NULL;
         p->kind = Ellipsis_kind;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1936,7 +1889,6 @@
         p->v.Slice.lower = lower;
         p->v.Slice.upper = upper;
         p->v.Slice.step = step;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1949,7 +1901,6 @@
                 return NULL;
         p->kind = ExtSlice_kind;
         p->v.ExtSlice.dims = dims;
-        p->annotations = NULL;
         return p;
 }
 
@@ -1967,7 +1918,6 @@
                 return NULL;
         p->kind = Index_kind;
         p->v.Index.value = value;
-        p->annotations = NULL;
         return p;
 }
 
@@ -2008,7 +1958,6 @@
         p->v.ExceptHandler.body = body;
         p->lineno = lineno;
         p->col_offset = col_offset;
-        p->annotations = NULL;
         return p;
 }
 
@@ -2115,9 +2064,6 @@
                 Py_DECREF(value);
                 break;
         }
-        if (o->annotations && PyObject_SetAttrString(result, "annotations",
-            o->annotations) < 0)
-                goto failed;
         return result;
 failed:
         Py_XDECREF(value);
@@ -2495,9 +2441,6 @@
         if (PyObject_SetAttrString(result, "col_offset", value) < 0)
                 goto failed;
         Py_DECREF(value);
-        if (o->annotations && PyObject_SetAttrString(result, "annotations",
-            o->annotations) < 0)
-                goto failed;
         return result;
 failed:
         Py_XDECREF(value);
@@ -2821,9 +2764,6 @@
         if (PyObject_SetAttrString(result, "col_offset", value) < 0)
                 goto failed;
         Py_DECREF(value);
-        if (o->annotations && PyObject_SetAttrString(result, "annotations",
-            o->annotations) < 0)
-                goto failed;
         return result;
 failed:
         Py_XDECREF(value);
@@ -2911,9 +2851,6 @@
                 Py_DECREF(value);
                 break;
         }
-        if (o->annotations && PyObject_SetAttrString(result, "annotations",
-            o->annotations) < 0)
-                goto failed;
         return result;
 failed:
         Py_XDECREF(value);
@@ -3116,9 +3053,6 @@
         if (PyObject_SetAttrString(result, "col_offset", value) < 0)
                 goto failed;
         Py_DECREF(value);
-        if (o->annotations && PyObject_SetAttrString(result, "annotations",
-            o->annotations) < 0)
-                goto failed;
         return result;
 failed:
         Py_XDECREF(value);
@@ -3264,10 +3198,6 @@
                 }
                 *out = Module(body, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Interactive_type)) {
@@ -3300,10 +3230,6 @@
                 }
                 *out = Interactive(body, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Expression_type)) {
@@ -3323,10 +3249,6 @@
                 }
                 *out = Expression(body, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Suite_type)) {
@@ -3359,10 +3281,6 @@
                 }
                 *out = Suite(body, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
 
@@ -3493,10 +3411,6 @@
                 *out = FunctionDef(name, args, body, decorator_list, lineno,
                                    col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)ClassDef_type)) {
@@ -3595,10 +3509,6 @@
                 *out = ClassDef(name, bases, body, decorator_list, lineno,
                                 col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Return_type)) {
@@ -3617,10 +3527,6 @@
                 }
                 *out = Return(value, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Delete_type)) {
@@ -3653,10 +3559,6 @@
                 }
                 *out = Delete(targets, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Assign_type)) {
@@ -3702,10 +3604,6 @@
                 }
                 *out = Assign(targets, value, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)AugAssign_type)) {
@@ -3751,10 +3649,6 @@
                 }
                 *out = AugAssign(target, op, value, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Print_type)) {
@@ -3812,10 +3706,6 @@
                 }
                 *out = Print(dest, values, nl, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)For_type)) {
@@ -3901,10 +3791,6 @@
                 *out = For(target, iter, body, orelse, lineno, col_offset,
                            arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)While_type)) {
@@ -3976,10 +3862,6 @@
                 }
                 *out = While(test, body, orelse, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)If_type)) {
@@ -4051,10 +3933,6 @@
                 }
                 *out = If(test, body, orelse, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)With_type)) {
@@ -4113,10 +3991,6 @@
                 *out = With(context_expr, optional_vars, body, lineno,
                             col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Raise_type)) {
@@ -4159,10 +4033,6 @@
                 }
                 *out = Raise(type, inst, tback, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)TryExcept_type)) {
@@ -4248,10 +4118,6 @@
                 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
                                  arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)TryFinally_type)) {
@@ -4310,10 +4176,6 @@
                 }
                 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Assert_type)) {
@@ -4345,10 +4207,6 @@
                 }
                 *out = Assert(test, msg, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Import_type)) {
@@ -4381,10 +4239,6 @@
                 }
                 *out = Import(names, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)ImportFrom_type)) {
@@ -4443,10 +4297,6 @@
                 *out = ImportFrom(module, names, level, lineno, col_offset,
                                   arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Exec_type)) {
@@ -4490,10 +4340,6 @@
                 }
                 *out = Exec(body, globals, locals, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Global_type)) {
@@ -4526,10 +4372,6 @@
                 }
                 *out = Global(names, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Expr_type)) {
@@ -4549,40 +4391,24 @@
                 }
                 *out = Expr(value, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Pass_type)) {
 
                 *out = Pass(lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Break_type)) {
 
                 *out = Break(lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Continue_type)) {
 
                 *out = Continue(lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
 
@@ -4673,10 +4499,6 @@
                 }
                 *out = BoolOp(op, values, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)BinOp_type)) {
@@ -4722,10 +4544,6 @@
                 }
                 *out = BinOp(left, op, right, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)UnaryOp_type)) {
@@ -4758,10 +4576,6 @@
                 }
                 *out = UnaryOp(op, operand, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Lambda_type)) {
@@ -4794,10 +4608,6 @@
                 }
                 *out = Lambda(args, body, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)IfExp_type)) {
@@ -4843,10 +4653,6 @@
                 }
                 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Dict_type)) {
@@ -4905,10 +4711,6 @@
                 }
                 *out = Dict(keys, values, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)ListComp_type)) {
@@ -4954,10 +4756,6 @@
                 }
                 *out = ListComp(elt, generators, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type)) {
@@ -5003,10 +4801,6 @@
                 }
                 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Yield_type)) {
@@ -5025,10 +4819,6 @@
                 }
                 *out = Yield(value, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Compare_type)) {
@@ -5101,10 +4891,6 @@
                 *out = Compare(left, ops, comparators, lineno, col_offset,
                                arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Call_type)) {
@@ -5201,10 +4987,6 @@
                 *out = Call(func, args, keywords, starargs, kwargs, lineno,
                             col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Repr_type)) {
@@ -5224,10 +5006,6 @@
                 }
                 *out = Repr(value, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Num_type)) {
@@ -5247,10 +5025,6 @@
                 }
                 *out = Num(n, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Str_type)) {
@@ -5270,10 +5044,6 @@
                 }
                 *out = Str(s, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Attribute_type)) {
@@ -5319,10 +5089,6 @@
                 }
                 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Subscript_type)) {
@@ -5368,10 +5134,6 @@
                 }
                 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Name_type)) {
@@ -5404,10 +5166,6 @@
                 }
                 *out = Name(id, ctx, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)List_type)) {
@@ -5453,10 +5211,6 @@
                 }
                 *out = List(elts, ctx, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Tuple_type)) {
@@ -5502,10 +5256,6 @@
                 }
                 *out = Tuple(elts, ctx, lineno, col_offset, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
 
@@ -5569,10 +5319,6 @@
 
                 *out = Ellipsis(arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Slice_type)) {
@@ -5615,10 +5361,6 @@
                 }
                 *out = Slice(lower, upper, step, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)ExtSlice_type)) {
@@ -5651,10 +5393,6 @@
                 }
                 *out = ExtSlice(dims, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
         if (PyObject_IsInstance(obj, (PyObject*)Index_type)) {
@@ -5674,10 +5412,6 @@
                 }
                 *out = Index(value, arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
 
@@ -6012,10 +5746,6 @@
                 *out = ExceptHandler(type, name, body, lineno, col_offset,
                                      arena);
                 if (*out == NULL) goto failed;
-                (*out)->annotations = PyObject_GetAttrString(obj,
-                 "annotations");
-                if ((*out)->annotations == NULL)
-                        PyErr_Clear();
                 return 0;
         }
 

Modified: python/branches/tlee-ast-optimize/Python/compile.c
==============================================================================
--- python/branches/tlee-ast-optimize/Python/compile.c	(original)
+++ python/branches/tlee-ast-optimize/Python/compile.c	Wed May  7 10:33:06 2008
@@ -2831,11 +2831,6 @@
 			   return ! Py_OptimizeFlag;
 		/* fall through */
 	default:
-        if (e->annotations != NULL) {
-            PyObject* constant = PyDict_GetItemString(e->annotations, "const");
-            if (constant != NULL)
-                return PyObject_IsTrue(constant);
-        }
 		return -1;
 	}
 }
@@ -2977,16 +2972,6 @@
 		c->u->u_lineno = e->lineno;
 		c->u->u_lineno_set = false;
 	}
-
-    /* if the expression is annotated with a constant value, use that instead */
-    if (e->annotations != NULL) {
-        PyObject* constant = PyDict_GetItemString(e->annotations, "const");
-        if (constant != NULL) {
-            ADDOP_O(c, LOAD_CONST, constant, consts);
-            return 1;
-        }
-    }
-
 	switch (e->kind) {
 	case BoolOp_kind:
 		return compiler_boolop(c, e);
@@ -3595,9 +3580,7 @@
 	d_lineno = i->i_lineno - a->a_lineno;
 
 	assert(d_bytecode >= 0);
-#if 0
-	 assert(d_lineno >= 0);
-#endif
+	assert(d_lineno >= 0);
 
 	if(d_bytecode == 0 && d_lineno == 0)
 		return 1;

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 10:33:06 2008
@@ -14,51 +14,111 @@
 static int optimize_slice(slice_ty* slice_ptr, PyArena* arena);
 
 /**
- * Annotate a constant expression with its value.
- */
-static int
-_annotate_constant(expr_ty expr, PyObject* v, PyArena* arena)
-{
-    if (expr->annotations == NULL) {
-        expr->annotations = PyDict_New();
-        if (expr->annotations == NULL)
-            return 0;
-        if (PyArena_AddPyObject(arena, expr->annotations) == -1)
-            return 0;
-    }
-
-    if (PyDict_SetItemString(expr->annotations, "const", v) == -1)
-        return 0;
-
-    return 1;
-}
-
-/**
  * Determine the constant value of a given expression. It's assumed that
  * constants have been folded.
  */
 static PyObject*
 _expr_constant_value(expr_ty expr)
 {
-    PyObject* obj;
-    
     if (expr->kind == Str_kind) {
         return expr->v.Str.s;
     }
     else if (expr->kind == Num_kind) {
         return expr->v.Num.n;
     }
-    else {
-        if (expr->annotations == NULL)
-            return NULL;
+    else if (expr->kind == Name_kind) {
+        const char* name = PyString_AS_STRING(expr->v.Name.id);
+        if (strcmp(name, "True") == 0)
+            return Py_True;
+        else if (strcmp(name, "False") == 0)
+            return Py_False;
+        else if (strcmp(name, "None") == 0)
+            return Py_None;
+    }
+    return NULL;
+}
 
-        obj = PyDict_GetItemString(expr->annotations, "const");
+/**
+ * Determine whether or not the given expression represents a constant value.
+ * This makes the assumption that constants have already been folded.
+ */
+static int
+_expr_is_constant(expr_ty expr)
+{
+    return _expr_constant_value(expr) != NULL;
+}
 
-        if (obj == NULL)
-            PyErr_Clear();
+/*
+ * builds a Name() node with a Load context from the given id.
+ */
+static expr_ty
+_make_name(PyObject* id, int lineno, int col_offset, PyArena* arena)
+{
+    expr_ty expr;
+    if (id == NULL)
+        return NULL;
+    expr = Name(id, Load, lineno, col_offset, arena);
+    if (expr == NULL)
+        return NULL;
+    return expr;
+}
+
+/**
+ * Builds an expr from the given constant value. Constant values can be
+ * any Str or Num, or any one of True/False/None.
+ */
+static expr_ty
+_expr_from_object(PyObject* object, int lineno, int col_offset, PyArena* arena)
+{
+    expr_ty expr = NULL;
 
-        return obj;
+    if (PyString_Check(object) || PyUnicode_Check(object)) {
+        Py_INCREF(object);
+        expr = Str(object, lineno, col_offset, arena);
     }
+    else if (PyNumber_Check(object)) {
+        Py_INCREF(object);
+        expr = Num(object, lineno, col_offset, arena);
+    }
+    else if (object == Py_None) {
+        object = PyString_FromString("None");
+        expr = _make_name(object, lineno, col_offset, arena);
+    }
+    else if (object == Py_True) {
+        object = PyString_FromString("True");
+        expr = _make_name(object, lineno, col_offset, arena);
+    }
+    else if (object == Py_False) {
+        object = PyString_FromString("False");
+        expr = _make_name(object, lineno, col_offset, arena);
+    }
+    else {
+        PyErr_Format(PyExc_TypeError, "unknown constant value");
+        return NULL;
+    }
+
+    if (expr == NULL)
+        return NULL;
+
+    if (PyArena_AddPyObject(arena, object) == -1) {
+        /* exception will be set in PyArena_AddPyObject */
+        Py_DECREF(object);
+        return NULL;
+    }
+
+    /* PyArena_AddPyObject decrements the refcount for us */
+    return expr;
+}
+
+/**
+ * Returns 1 if the given expression evaluates to a true value. Otherwise,
+ * returns 0. This function assumes that the given expr is constant.
+ */
+static int
+_expr_is_true(expr_ty expr)
+{
+    assert(_expr_is_constant(expr));
+    return PyObject_IsTrue(_expr_constant_value(expr));
 }
 
 /**
@@ -121,13 +181,17 @@
 _asdl_seq_replace_with_pass(asdl_seq* seq, int n, int lineno, int col_offset, PyArena* arena)
 {
     stmt_ty pass;
+    asdl_seq* new;
 
     pass = Pass(lineno, col_offset, arena);
     if (pass == NULL)
         return NULL;
-    asdl_seq_SET(seq, n, pass);
-
-    return seq;
+    new = asdl_seq_new(1, arena);
+    if (new == NULL)
+        return NULL;
+    asdl_seq_SET(new, 0, pass);
+    
+    return _asdl_seq_replace(seq, n, new, arena);
 }
 
 /**
@@ -139,17 +203,14 @@
     int n;
     asdl_seq* seq = *seq_ptr;
     for (n = 0; n < asdl_seq_LEN(seq); n++) {
-        stmt_ty stmt;
+        stmt_ty stmt = asdl_seq_GET(seq, n);
         if (!optimize_stmt((stmt_ty*)&asdl_seq_GET(seq, n), arena))
             return 0;
 
-        stmt = asdl_seq_GET(seq, n);
-
         if (stmt->kind == If_kind) {
-            PyObject* test = _expr_constant_value(stmt->v.If.test);
             /* eliminate branches that can never be reached */
-            if (test != NULL) {
-                if (PyObject_IsTrue(test))
+            if (_expr_is_constant(stmt->v.If.test)) {
+                if (_expr_is_true(stmt->v.If.test))
                     seq = _asdl_seq_replace(seq, n, stmt->v.If.body, arena);
                 else {
                     if (stmt->v.If.orelse == NULL) {
@@ -167,7 +228,7 @@
                 *seq_ptr = seq;
             }
         }
-        else if (stmt->kind == Return_kind && n < (asdl_seq_LEN(seq) - 1)) {
+        else if (stmt->kind == Return_kind) {
             /* eliminate all nodes after a return */
             seq = _asdl_seq_replace_with_pass(seq, n + 1,
                     stmt->lineno, stmt->col_offset, arena);
@@ -397,13 +458,14 @@
             return 1;
         }
         
-        if (!_annotate_constant(expr, res, arena)) {
+        expr = _expr_from_object(res, expr->lineno, expr->col_offset, arena);
+        if (expr == NULL) {
             Py_DECREF(res);
             return 0;
         }
+        *expr_ptr = expr;
 
-        /* XXX: is this necessary? */
-        /* Py_DECREF(res); */
+        Py_DECREF(res);
     }
 
     return 1;
@@ -412,15 +474,21 @@
 static int
 optimize_unary_op(expr_ty* expr_ptr, PyArena* arena)
 {
-    PyObject* operand;
     expr_ty expr = *expr_ptr;
     if (!optimize_expr(&expr->v.UnaryOp.operand, arena))
         return 0;
-
-    operand = _expr_constant_value(expr->v.UnaryOp.operand);
-    if (operand != NULL) {
+    if (_expr_is_constant(expr->v.UnaryOp.operand)) {
+        PyObject* operand;
         PyObject* res;
 
+        operand = _expr_constant_value(expr->v.UnaryOp.operand);
+        if (operand == NULL) {
+            /* XXX this should never happen ... */
+            PyErr_Format(PyExc_ValueError, "unknown constant type: %d",
+                    expr->v.UnaryOp.operand->kind);
+            return 0;
+        }
+
         switch (expr->v.UnaryOp.op) {
             case Invert:
                 {
@@ -465,13 +533,14 @@
             return 1;
         }
 
-        if (!_annotate_constant(expr, res, arena)) {
+        expr = _expr_from_object(res, expr->lineno, expr->col_offset, arena);
+        if (!expr) {
             Py_DECREF(res);
             return 0;
         }
+        *expr_ptr = expr;
 
-        /* XXX: is this necessary? */
-        /* Py_DECREF(res); */
+        Py_DECREF(res);
     }
     return 1;
 }
@@ -678,50 +747,6 @@
     return 1;
 }
 
-static PyObject*
-_make_constant_tuple(asdl_seq* values, PyArena* arena)
-{
-    expr_ty expr;
-    int i;
-    PyObject* tuple;
-    
-    tuple = PyTuple_New(asdl_seq_LEN(values));
-    if (tuple == NULL)
-        return NULL;
-
-    if (PyArena_AddPyObject(arena, tuple) == -1) {
-        Py_DECREF(tuple);
-        return NULL;
-    }
-
-    /* don't *think* we need to add the constant values to the arena */
-    for (i = 0; i < asdl_seq_LEN(values); i++) {
-        PyObject* value;
-        expr = (expr_ty)asdl_seq_GET(values, i);
-        value = _expr_constant_value(expr);
-        Py_INCREF(value);
-        if (PyTuple_SetItem(tuple, i, value) == -1)
-            return NULL;
-    }
-
-    return tuple;
-}
-
-static int
-_is_constant_seq(asdl_seq* values)
-{
-    expr_ty expr;
-    int i;
-
-    for (i = 0; i < asdl_seq_LEN(values); i++) {
-        expr = (expr_ty)asdl_seq_GET(values, i);
-        if (_expr_constant_value(expr) == NULL)
-            return 0;
-    }
-
-    return 1;
-}
-
 static int
 optimize_expr(expr_ty* expr_ptr, PyArena* arena)
 {
@@ -785,56 +810,21 @@
             }
         case List_kind:
             {
-                /* TODO: optimize lists used in "in"/"not in" as per original
-                 *       peephole optimization code */
                 return optimize_expr_seq(&expr->v.List.elts, arena);
             }
         case Tuple_kind:
             {
-                if (!optimize_expr_seq(&expr->v.Tuple.elts, arena))
-                    return 0;
-                
-                if (_is_constant_seq(expr->v.Tuple.elts)) {
-                    PyObject* value = _make_constant_tuple(expr->v.Tuple.elts,
-                                                            arena);
-                    if (value == NULL)
-                        return 0;
-                    if (!_annotate_constant(expr, value, arena))
-                        return 0;
-                }
-
-                return 1;
+                return optimize_expr_seq(&expr->v.Tuple.elts, arena);
             }
         case Num_kind:
         case Str_kind:
-            {
-                return 1;
-            }
         case Name_kind:
             {
-                PyObject* v;
-                const char* id;
-                
-                id = PyString_AS_STRING(expr->v.Name.id);
-                v  = NULL;
-
-                if (strcmp(id, "None") == 0)
-                    v = Py_None;
-                else if (strcmp(id, "True") == 0)
-                    v = Py_True;
-                else if (strcmp(id, "False") == 0)
-                    v = Py_False;
-
-                if (v != NULL) {
-                    if (!_annotate_constant(expr, v, arena))
-                        return 0;
-                }
-
                 return 1;
             }
         default:
             PyErr_Format(PyExc_ValueError, "unknown expr_ty kind: %d",
-                            expr->kind);
+                    expr->kind);
             return 0;
     }
 }
@@ -935,16 +925,6 @@
         return 0;
     if (!optimize_expr(&stmt->v.For.iter, arena))
         return 0;
-    if (stmt->v.For.iter->kind == List_kind) {
-        if (_is_constant_seq(stmt->v.For.iter->v.List.elts)) {
-            PyObject* constant = _make_constant_tuple(
-                    stmt->v.For.iter->v.List.elts, arena);
-            if (constant == NULL)
-                return 0;
-            if (!_annotate_constant(stmt->v.For.iter, constant, arena))
-                return 0;
-        }
-    }
     if (!optimize_stmt_seq(&stmt->v.For.body, arena))
         return 0;
     if (!optimize_stmt_seq(&stmt->v.For.orelse, arena))
@@ -1224,8 +1204,6 @@
 int
 PyAST_Optimize(mod_ty* mod_ptr, PyArena* arena)
 {
-    int res;
-    res = optimize_mod(mod_ptr, arena);
-    return res;
+    return optimize_mod(mod_ptr, arena);
 }
 

Modified: python/branches/tlee-ast-optimize/Python/peephole.c
==============================================================================
--- python/branches/tlee-ast-optimize/Python/peephole.c	(original)
+++ python/branches/tlee-ast-optimize/Python/peephole.c	Wed May  7 10:33:06 2008
@@ -201,11 +201,34 @@
 				codestr[i+3] = NOP;
 				break;
 
+				/* Replace LOAD_GLOBAL/LOAD_NAME None
+                                   with LOAD_CONST None */
+			case LOAD_NAME:
+			case LOAD_GLOBAL:
+				j = GETARG(codestr, i);
+				name = PyString_AsString(PyTuple_GET_ITEM(names, j));
+				if (name == NULL  ||  strcmp(name, "None") != 0)
+					continue;
+				for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
+					if (PyList_GET_ITEM(consts, j) == Py_None)
+						break;
+				}
+				if (j == PyList_GET_SIZE(consts)) {
+					if (PyList_Append(consts, Py_None) == -1)
+					        goto exitUnchanged;                                        
+				}
+				assert(PyList_GET_ITEM(consts, j) == Py_None);
+				codestr[i] = LOAD_CONST;
+				SETARG(codestr, i, j);
+				cumlc = lastlc + 1;
+				break;
+
 				/* Try to fold tuples of constants (includes a case for lists
 				   which are only used for "in" and "not in" tests).
 				   Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
 				   Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
 				   Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
+			case BUILD_TUPLE:
 			case BUILD_LIST:
 				j = GETARG(codestr, i);
 				h = i - 3 * j;


More information about the Python-checkins mailing list