[Python-checkins] r62508 - in python/branches/tlee-ast-optimize: Lib/test/test_optimizer.py Python/optimize.c

thomas.lee python-checkins at python.org
Sat Apr 26 02:15:42 CEST 2008


Author: thomas.lee
Date: Sat Apr 26 02:15:32 2008
New Revision: 62508

Log:
Handle errors in optimize_unary_op a little more gracefully. Add some tests to ensure that stuff which can't be optimized at compile time are not optimized, with errors left until runtime. Remove a chunk of unused code.

Modified:
   python/branches/tlee-ast-optimize/Lib/test/test_optimizer.py
   python/branches/tlee-ast-optimize/Python/optimize.c

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	Sat Apr 26 02:15:32 2008
@@ -121,6 +121,19 @@
         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'")
+
+        # ensure no optimization has taken place
+        self.assertEqual(_ast.Expr, ast.body[0].__class__)
+        self.assertEqual(_ast.BinOp, ast.body[0].value.__class__)
+
+        try:
+            exec compile(ast)
+            self.fail("expected this to raise a TypeError")
+        except TypeError:
+            pass
+
     def test_unary_fold_num(self):
         # check unary constant folding for numeric values
         self.assertNum(-5, "-5")
@@ -129,6 +142,19 @@
         self.assertNum(False, "not True")
         self.assertNum(True, "not None")
 
+    def test_unary_failure_left_until_runtime(self):
+        ast = self.compileast("~'bad!'")
+
+        # ensure no optimization has taken place
+        self.assertEqual(_ast.Expr, ast.body[0].__class__)
+        self.assertEqual(_ast.UnaryOp, ast.body[0].value.__class__)
+
+        try:
+            exec compile(ast)
+            self.fail("expected this to raise a TypeError")
+        except TypeError:
+            pass
+
     def test_return_none_becomes_return(self):
         code = """
 def foo():

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	Sat Apr 26 02:15:32 2008
@@ -135,24 +135,6 @@
     return 1;
 }
 
-#if 0
-/**
- * Shrink an ASDL sequence by trimming off the last few elements.
- */
-static asdl_seq*
-_asdl_seq_shrink(asdl_seq* seq, int newlen, PyArena* arena)
-{
-    asdl_seq* new;
-    int n;
-    new = asdl_seq_new(newlen, arena);
-    if (new == NULL)
-        return NULL;
-    for (n = 0; n < newlen; n++)
-        asdl_seq_SET(new, n, asdl_seq_GET(seq, n));
-    return new;
-}
-#endif
-
 /**
  * Replace an AST node at position `n' with the node(s) in `replacement'.
  */
@@ -456,18 +438,17 @@
         
         if (res == NULL) {
             if (PyErr_Occurred()) {
-                /* if we're out of memory, we need to complain about it now! */
-                if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
+                /* complain about out of memory errors right away */
+                if (PyErr_ExceptionMatches(PyExc_MemoryError))
                     return 0;
-                }
-                /* otherwise, the binop failed: clear the error */
+                /* leave all other errors for runtime */
                 else
                     PyErr_Clear();
             }
-            /* do not optimize this expression */
             return 1;
         }
 
+        /* XXX: is this check still necessary? */
         size = PyObject_Size(res);
         if (size == -1) {
             PyErr_Clear();
@@ -540,8 +521,17 @@
                 return 0;
         }
 
-        if (res == NULL)
-            return 0;
+        if (res == NULL) {
+            if (PyErr_Occurred()) {
+                /* complain about out of memory errors right away */
+                if (PyErr_ExceptionMatches(PyExc_MemoryError))
+                    return 0;
+                /* leave all other errors for runtime */
+                else
+                    PyErr_Clear();
+            }
+            return 1;
+        }
 
         expr = _expr_from_object(res, expr->lineno, expr->col_offset, arena);
         if (!expr) {


More information about the Python-checkins mailing list