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

thomas.lee python-checkins at python.org
Wed Apr 23 17:48:31 CEST 2008


Author: thomas.lee
Date: Wed Apr 23 17:48:31 2008
New Revision: 62467

Log:
Optimize 'yield None' away to a plain old 'yield'

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	Wed Apr 23 17:48:31 2008
@@ -138,6 +138,16 @@
         self.assertEqual(_ast.Return, ast.body[0].body[0].__class__)
         self.assertEqual(None, ast.body[0].body[0].value)
 
+    def test_yield_none_becomes_yield(self):
+        code = """
+def foo():
+    yield None
+"""
+        ast = self.compileast(code)
+        self.assertEqual(_ast.Expr, ast.body[0].body[0].__class__)
+        self.assertEqual(_ast.Yield, ast.body[0].body[0].value.__class__)
+        self.assertEqual(None, ast.body[0].body[0].value.value)
+
     def test_eliminate_code_after_return(self):
         # ensure code following a "return" is erased from the AST
         code = """

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 Apr 23 17:48:31 2008
@@ -626,6 +626,12 @@
     if (expr->v.Yield.value != NULL)
         if (!optimize_expr(&expr->v.Yield.value, arena))
             return 0;
+    /* "yield None" becomes "yield" */
+    if (expr->v.Yield.value->kind == Name_kind) {
+        PyObject* id = expr->v.Yield.value->v.Name.id;
+        if (strcmp(PyString_AS_STRING(id), "None") == 0)
+                expr->v.Yield.value = NULL;
+    }
     return 1;
 }
 


More information about the Python-checkins mailing list